In [3]:
import requests
api=('ceabb939ee5542d3adeab7e2cdc529ef')

In [3]:
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2009-05-10/hardcover-fiction.json?api-key='+api)
mothers_day_09_raw=response.json()
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2010-05-09/hardcover-fiction.json?api-key='+api)
mothers_day_10_raw=response.json()
mothers_day_09_books=mothers_day_09_raw['results']['books']
mothers_day_10_books=mothers_day_10_raw['results']['books']

In [4]:
print("Here's the Mother's Day '09 best seller list for hardcover fiction:")
for book in mothers_day_09_books:
    print(book['rank'], book['title'], '-', book['description'])
print("\nAnd here's the same, for '10:")
for book in mothers_day_10_books:
    print(book['rank'], book['title'], '-', book['description'])


Here's the Mother's Day '09 best seller list for hardcover fiction:
1 FIRST FAMILY - Former Secret Service agents, now P.I.’s, search for a child abducted from a party at Camp David.
2 TEA TIME FOR THE TRADITIONALLY BUILT - The 10th novel in the No. 1 Ladies’ Detective Agency series.
3 LOITERING WITH INTENT - Stone Barrington, the New York cop turned lawyer, takes a case in Key West.
4 JUST TAKE MY HEART - An assistant prosecutor  who has had a heart transplant discovers that her life is at risk when she tries a murder case.
5 THE PERFECT POISON - A Victorian botanist investigates a poisoning; an Arcane Society novel.
6 THE HOST - Aliens have taken control of the minds and bodies of most humans, but one woman won’t surrender.
7 LOOK AGAIN - A reporter learns that her adopted son may have been abducted from his birth mother.
8 DEADLOCK - A U.N. expert searching for Romanov treasure is kidnapped.
9 LONG LOST - Myron Bolitar helps an ex-lover search for her daughter.
10 TURN COAT - Book 11 of the Dresden Files series about a wizard detective in Chicago.
11 THE ASSOCIATE - An idealistic law-school graduate is forced to take a job at a large, brutalizing law firm.
12 HANDLE WITH CARE - A woman whose daughter has a dangerous birth defect must decide whether to sue her obstetrician, an old friend.
13 THE HELP - A young white woman and two black maids in 1960s ­Mississippi.
14 THE GUERNSEY LITERARY AND POTATO PEEL PIE SOCIETY - A journalist meets the island’s old Nazi resisters.
15 FATALLY FLAKY - The caterer Goldy Schulz tries to outwit a killer on the grounds of an Aspen spa.
16 ARTHAS - A World of Warcraft fantasy novel.
17 A RELIABLE WIFE - Complications ensue when a wealthy Wisconsin widower in 1907 advertises for a spouse.
18 BORDERLINE - Off duty and on vacation in Big Bend National Park, Anna Pigeon rescues a baby and is drawn into cross-border intrigue.
19 ONE SECOND AFTER - A retired Army colonel in North Carolina protects his family when a nuclear blast disables the power grid.
20 BONEMAN'S DAUGHTERS - Ryan Evans faces a serial killer who murders his young victims in a highly idiosyncratic way.

And here's the same, for '10:
1 DELIVER US FROM EVIL - Two agents are tracking the same man, a human trafficker who is now dealing in nuclear arms.
2 THE HELP - A young white woman and two black maids in 1960s ­Mississippi.
3 THE DOUBLE COMFORT SAFARI CLUB - 
The 11th novel in the No. 1 Ladies’ Detective Agency series.
4 THIS BODY OF DEATH - Detective Thomas Lynley becomes involved when a woman’s body is found in a London cemetery.
5 LUCID INTERVALS - Stone Barrington, the New York cop turned lawyer, helps search for a former British intelligence operative.
6 THE SHADOW OF YOUR SMILE - An elderly woman must decide whether to reveal a family secret.
7 BURNING LAMP - A Victorian crime lord fears he has fallen prey to an ancestral curse, and only a magical artifact can save him; an Arcane Society novel and the second book in the Dreamlight trilogy.
8 EVERY LAST ONE - After a shocking assault on her family, a woman must discover how to live the rest of her life.
9 EIGHT DAYS TO LIVE - 
A painting by Eve Duncan’s adopted daughter draws the ire of a religious cult.
10 CHANGES - Book 12 of the Dresden Files series about a wizard detective in Chicago.
11 CAUGHT - A suburban girl goes missing.
12 HOUSE RULES - A teenage boy with Asperger’s syndrome is accused of murder.
13 MATTERHORN - Marines in Vietnam in 1969.
14 THE WALK - A Seattle ad executive loses everything and starts walking to Key West.
15 DECEPTION - The Los Angeles psychologist-detective Alex Delaware and the detective Milo Sturgis work on a prep-school murder case.
16 BEATRICE AND VIRGIL - The fate of stuffed animals in a taxidermy shop is an allegory for the Holocaust; from the author of “The Life of Pi.”
17 WRECKED - In the 13th mystery in this series, the suspicious disappearance of a neighbor interrupts a romantic weekend on Cape Cod for the P.I. Regan Reilly and her husband.
18 SILVER BORNE - The shapeshifter Mercy Thompson works on her relationship with the leader of the werewolf pack and helps a suicidal friend.
19 ABRAHAM LINCOLN: VAMPIRE HUNTER - A journal reveals Lincoln’s fight against the undead; by the author of “Pride and Prejudice and Zombies."
20 A RIVER IN THE SKY - In 1910, the Peabody-Emerson clan tries to head off a holy war in Palestine; an Amelia Peabody mystery.

In [5]:
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2009-06-21/hardcover-fiction.json?api-key='+api)
fathers_day_09_raw=response.json()
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2010-06-20/hardcover-fiction.json?api-key='+api)
fathers_day_10_raw=response.json()
fathers_day_09_books=fathers_day_09_raw['results']['books']
fathers_day_10_books=fathers_day_10_raw['results']['books']

In [6]:
print("Let's contrast that to Father's Day '09:")
for book in fathers_day_09_books:
    print(book['rank'], book['title'], '-', book['description'])
print("\nAnd the same, for '10:")
for book in fathers_day_10_books:
    print(book['rank'], book['title'], '-', book['description'])


Let's contrast that to Father's Day '09:
1 SKIN TRADE - Investigating some killings in Las Vegas, the vampire hunter Anita Blake must contend with the power of the weretigers.
2 MEDUSA - In the eighth NUMA Files novel, Kurt Austin and his team confront a rare jellyfish, a Chinese crime syndicate and a deadly virus.
3 THE SCARECROW - A Los Angeles Times reporter tracks a devious killer.
4 SHANGHAI GIRLS - Two Chinese sisters in the 1930s are sold as wives to men from California, and leave their war-torn country to join them.
5 MATTERS OF THE HEART - A New York photographer falls in love with a sociopathic novelist who lures her to his isolated Irish estate.
6 GONE TOMORROW - Jack Reacher discovers a conspiracy dating back to the Soviet war in Afghanistan.
7 DEAD AND GONE - Sookie Stackhouse searches for the killer of a werepanther.
8 THE 8TH CONFESSION - Detective Lindsay Boxer and the Women’s Murder Club investigate a pair of killings.
9 THE STRAIN - An epidemiologist and others struggle to contain a vampiric virus that has infected New York; the first book of a trilogy.
10 WICKED PREY - The Minneapolis detective Lucas Davenport deals with mayhem occasioned by the Republican convention.
11 THE HOST - Aliens have taken control of the minds and bodies of most humans, but one woman won’t surrender.
12 FIRST FAMILY - Former Secret Service agents, now P.I.’s, search for a child abducted after a party at Camp David.
13 CEMETERY DANCE - 

The F.B.I. agent Aloysius Pendergast investigates the murder of a Times reporter.
14 UNDEAD AND UNWELCOME - Betsy Taylor, a suburban vampire queen, tangles with a pack of werewolves.
15 THE HELP - A young white woman and two black maids in 1960s ­Mississippi.
16 PYGMY - Terrorists from a totalitarian country enter the Midwest disguised as exchange students in this satire.
17 MY FATHER'S TEARS AND OTHER STORIES - 
18 ROAD DOGS - Cundo Rey and Jack Foley, characters from earlier Leonard novels, are out of prison, living with Dawn Navarro in Venice Beach, and possibly trying to go straight.
19 THE STORY SISTERS - 
20 HEARTLESS - A woman‘s secret makes it hard for her to accept her stepbrother‘s love.

And the same, for '10:
1 THE GIRL WHO KICKED THE HORNET’S NEST - The third volume of a trilogy about a Swedish hacker and a journalist.
2 BULLET - Anita Blake, vampire hunter, is menaced by the Mother of All Darkness, who wants to take over her body.
3 THE SPY - In 1908, a murder leads the detective Isaac Bell to investigate international spies who are trying to keep America from developing a fleet of dreadnought battleships.
4 THE HELP - A young white woman and two black maids in 1960s ­Mississippi.
5 DEAD IN THE FAMILY - Sookie Stackhouse is exhausted in the aftermath of a Fae war.
6 61 HOURS - Jack Reacher helps the police in a small South Dakota town protect a witness in a drug trial.
7 THE BURNING WIRE - The forensic detective Lincoln Rhyme investigates a series of murderous electrical explosions and tracks the killer known as the Watchmaker to Mexico.
8 STORM PREY - Lucas Davenport’s wife is a witness to a botched robbery and murder in the 20th novel in the “Prey” series.
9 THE BOURNE OBJECTIVE - Robert Ludlum’s character Jason Bourne matches wits with a vicious Russian mercenary.
10 INNOCENT - Turow revisits the characters from “Presumed Innocent.”
11 HEART OF THE MATTER - The lives of two women — one married to a pediatric plastic surgeon, the other a lawyer and single mother — converge after an accident involving the lawyer’s son.
12 THE 9TH JUDGMENT - Detective Lindsay Boxer pursues a killer who’s preying on women and children.
13 BLOCKADE BILLY - A tale about the dark side of baseball, circa 1957.
14 ALLIES - The Skywalkers and the Sith undertake an unprecedented joint mission; a “Star Wars: Fate of the Jedi” book.
15 THE RULE OF NINE - The lawyer Paul Madriani pursues the hired assassin Liquida, who is out for revenge, while a terrorist plots an attack on the United States.
16 FEVER DREAM -  The F.B.I. agent Aloysius Pendergast, who has long thought that his wife was killed in an accident, learns that she was actually murdered; originally published in 2010.
17 DELIVER US FROM EVIL - Two agents are tracking the same man, a human trafficker who is now dealing in nuclear arms.
18 MATTERHORN - Bloody folly envelops a Marine company’s construction, abandonment and retaking of a remote hilltop outpost in Vietnam.
19 THE PARTICULAR SADNESS OF LEMON CAKE - A girl has insights into other people’s secret emotions.
20 DANGEROUS - A Texas F.B.I. agent falls in love with a woman with a past.

In [7]:
response=requests.get('https://api.nytimes.com/svc/books/v3/lists/names.json?api-key='+api)
raw_list_names=response.json()
#raw_list_names.keys()

In [8]:
list_names=raw_list_names['results']
#type(list_names)
june_06_2009_list=[]
june_06_2015_list=[]
for name in list_names:
    if name['oldest_published_date']<'2009-06-06':
        june_06_2009_list.append(name['list_name'])
    if name['oldest_published_date']<'2015-06-06':
        june_06_2015_list.append(name['list_name'])
print("Here's a big list of all the book categories ranked on June 06, 2009:", ", ".join(june_06_2009_list))
print("\nAnd here's the same list but for 2015:", ", ".join(june_06_2015_list))


Here's a big list of all the book categories ranked on June 06, 2009: Hardcover Fiction, Hardcover Nonfiction, Trade Fiction Paperback, Mass Market Paperback, Paperback Nonfiction, Hardcover Advice, Paperback Advice, Chapter Books, Paperback Books, Picture Books, Series Books, Hardcover Graphic Books, Paperback Graphic Books, Manga

And here's the same list but for 2015: Combined Print and E-Book Fiction, Combined Print and E-Book Nonfiction, Hardcover Fiction, Hardcover Nonfiction, Trade Fiction Paperback, Mass Market Paperback, Paperback Nonfiction, E-Book Fiction, E-Book Nonfiction, Hardcover Advice, Paperback Advice, Advice How-To and Miscellaneous, Chapter Books, Childrens Middle Grade, Paperback Books, Picture Books, Series Books, Young Adult, Hardcover Graphic Books, Paperback Graphic Books, Manga, Combined Print Fiction, Combined Print Nonfiction, Animals, Business Books, Celebrities, Crime and Punishment, Culture, Education, Espionage, Expeditions Disasters and Adventures, Fashion Manners and Customs, Food and Fitness, Games and Activities, Hardcover Business Books, Health, Humor, Indigenous Americans, Relationships, Paperback Business Books, Family, Hardcover Political Books, Race and Civil Rights, Religion Spirituality and Faith, Science, Sports, Travel

In [9]:
muammar_list=['Gadafi', 'Gaddafi', 'Kadafi', 'Qaddafi']
for muammar in muammar_list:
    response=requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q='+muammar+'&fq=Libya&api-key='+api)
    muammar_data=response.json()
    print(muammar, "returns", muammar_data['response']['meta']['hits'], "hits")


Gadafi returns 0 hits
Gaddafi returns 1020 hits
Kadafi returns 3 hits
Qaddafi returns 5253 hits

In [10]:
response=requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19951231&api-key='+api)
hipster_data=response.json()
earliest_hipster_doc={'title': '???', 'pub_date': '???', 'lede': '???'}
earliest_date='1995-12-31'
for doc in hipster_data['response']['docs']:
    pub_date=doc['pub_date'][:-10]
    if pub_date<earliest_date:
        earliest_date=pub_date
        earliest_hipster_doc['title']=doc['headline']['main']
        earliest_hipster_doc['pub_date']=pub_date
        earliest_hipster_doc['lede']=doc['lead_paragraph']
print("The first article to mention the word hipster in 1995 was titled", earliest_hipster_doc['title'], "and had the publication date", earliest_hipster_doc['pub_date'])
print("And here is the first paragraph of that amazing article:\n", earliest_hipster_doc['lede'])


The first article to mention the word hipster in 1995 was titled SOUND and had the publication date 1995-02-05
And here is the first paragraph of that amazing article:
 Portable record players with built-in speakers, from the 1960's, are the latest points on hipster score cards. In some cases, they are the only way to listen to many of the old LP or 45-r.p.m. recordings still around but not released on cassette or CD. Usually available in white plastic or metal, they can be found in flea markets and secondhand stores. One style has the arm cast in the shape of a cobra. (Don Hogan Charles/The New York Times)

In [11]:
url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"'
begin_date=[19500101, 19600101, 19700101, 19800101, 19900101, 20000101, 20100101]
end_date=[19591231, 19691231, 19791231, 19891231, 19991231, 20091231, 20161231]
decade_counter=1950
for a in range(0, 7):
    response=requests.get(url+'&begin_date='+str(begin_date[a])+'&end_date='+str(end_date[a])+'&api-key='+api)
    raw_gay_marriage_doc=response.json()
    print("There were", raw_gay_marriage_doc['response']['meta']['hits'], "mentions of gay marriange in the", str(decade_counter)+"'s.")
    decade_counter+=10


There were 0 mentions of gay marriange in the 1950's.
There were 0 mentions of gay marriange in the 1960's.
There were 0 mentions of gay marriange in the 1970's.
There were 3 mentions of gay marriange in the 1980's.
There were 137 mentions of gay marriange in the 1990's.
There were 2510 mentions of gay marriange in the 2000's.
There were 4758 mentions of gay marriange in the 2010's.

In [18]:
# This whole section was originally to get the number of hits so we can iterate through all of the pages. Unfortunately,
# you can't actually request beyond page 100 in the nyt api. So instead, I commented this out and said 0 to 101.

# url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycle'
# response=requests.get(url+'&api-key='+api)
# raw_motorcycle_doc=response.json()
# hits=raw_motorcycle_doc['response']['meta']['hits']
url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycle'
motorcycle_sections=[]
for page in range(0, 101):
    response=requests.get(url+'&page='+str(page)+'&api-key='+api)
    raw_motorcycle_doc=response.json()
    for doc in raw_motorcycle_doc['response']['docs']:    
        motorcycle_sections.append(doc['section_name'])
    print("did page", page)


did page 0
did page 1
did page 2
did page 3
did page 4
did page 5
did page 6
did page 7
did page 8
did page 9
did page 10
did page 11
did page 12
did page 13
did page 14
did page 15
did page 16
did page 17
did page 18
did page 19
did page 20
did page 21
did page 22
did page 23
did page 24
did page 25
did page 26
did page 27
did page 28
did page 29
did page 30
did page 31
did page 32
did page 33
did page 34
did page 35
did page 36
did page 37
did page 38
did page 39
did page 40
did page 41
did page 42
did page 43
did page 44
did page 45
did page 46
did page 47
did page 48
did page 49
did page 50
did page 51
did page 52
did page 53
did page 54
did page 55
did page 56
did page 57
did page 58
did page 59
did page 60
did page 61
did page 62
did page 63
did page 64
did page 65
did page 66
did page 67
did page 68
did page 69
did page 70
did page 71
did page 72
did page 73
did page 74
did page 75
did page 76
did page 77
did page 78
did page 79
did page 80
did page 81
did page 82
did page 83
did page 84
did page 85
did page 86
did page 87
did page 88
did page 89
did page 90
did page 91
did page 92
did page 93
did page 94
did page 95
did page 96
did page 97
did page 98
did page 99
did page 100

In [23]:
from collections import Counter
most_motorcycle_section={'section': '???', 'total': 0}
cleaned_list=dict(Counter(motorcycle_sections))
for key, value in cleaned_list.items():
    if key and value > most_motorcycle_section['total']:
        most_motorcycle_section={'section': key, 'total': value}
        print("new value:", key, value)
print("The section that mentions motorcycles the most (at least on the first 100 pages) is", most_motorcycle_section['section'], "with", most_motorcycle_section['total'], "total mentions")


new value: Technology; Automobiles 1
new value: Arts; Books 3
new value: Health 4
new value: Arts 9
new value: Autos 67
new value: U.S. 70
The section that mentions motorcycles the most (at least on the first 100 pages) is U.S. with 70 total mentions

In [4]:
url="https://api.nytimes.com/svc/movies/v2/reviews/search.json"
critics_picks=[]
#movie_list=[]
reviewer_list=[]
offset=0
for a in range(0,3):
    response=requests.get(url+"?offset="+str(offset)+"&api-key="+api)
    movie_list_raw=response.json()
    for movie in movie_list_raw['results']:
        #print(movie.keys())
        #movie_list.append({'title': movie['display_title'], 'reviewer': movie['byline']})
        reviewer_list.append(movie['byline'])
        if movie['critics_pick']:
            critics_picks.append(movie['display_title'])
    print("Of the first", offset+20, "titles,", len(critics_picks), "were critic's picks")
    offset+=20
    
#print(critics_picks)


Of the first 20 titles, 6 were critic's picks
Of the first 40 titles, 15 were critic's picks
Of the first 60 titles, 20 were critic's picks

In [14]:
from collections import Counter
reviewiest=Counter(reviewer_list).most_common(1)[0]
print("The reviewer with the most reviews was", reviewiest[0].title(), "with", reviewiest[1], "total")


The reviewer with the most reviews was A. O. Scott with 9 total

In [ ]:


In [ ]: