graded = 7/8

1) What books topped the Hardcover Fiction NYT best-sellers list on Mother's Day in 2009 and 2010? How about Father's Day?


In [2]:
import requests
date='2009-05-08' #Replace with 2010-05-09,2009-06-21,2010-06-20
url="https://api.nytimes.com/svc/books/v2/lists/"+date+"/hardcover-fiction.json?&num_results=1&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The Best Sellers on",date,"are the following:")
print("")
for n in range(0,len(data['results'])):
    for title in data['results'][n]['book_details']:
        print(n+1,".",title['title'],"by",title['author'])


The Best Sellers on 2009-05-08 are the following:

1 . FIRST FAMILY by David Baldacci
2 . TEA TIME FOR THE TRADITIONALLY BUILT by Alexander McCall Smith
3 . LOITERING WITH INTENT by Stuart Woods
4 . JUST TAKE MY HEART by Mary Higgins Clark
5 . THE PERFECT POISON by Amanda Quick
6 . THE HOST by Stephenie Meyer
7 . LOOK AGAIN by Lisa Scottoline
8 . DEADLOCK by Iris Johansen
9 . LONG LOST by Harlan Coben
10 . TURN COAT by Jim Butcher
11 . THE ASSOCIATE by John Grisham
12 . HANDLE WITH CARE by Jodi Picoult
13 . THE HELP by Kathryn Stockett
14 . THE GUERNSEY LITERARY AND POTATO PEEL PIE SOCIETY by Mary Ann Shaffer and Annie Barrows
15 . FATALLY FLAKY by Diane Mott Davidson
16 . ARTHAS by Christie Golden
17 . A RELIABLE WIFE by Robert Goolrick
18 . BORDERLINE by Nevada Barr
19 . ONE SECOND AFTER by William R Forstchen
20 . BONEMAN'S DAUGHTERS by Ted Dekker

In [438]:
import requests
date='2010-05-09' #Replace with 2010-05-09,2009-06-21,2010-06-20
url="https://api.nytimes.com/svc/books/v2/lists/"+date+"/hardcover-fiction.json?&num_results=1&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The Best Sellers on",date,"are the following:")
print("")
for n in range(0,len(data['results'])):
    for title in data['results'][n]['book_details']:
        print(n+1,".",title['title'],"by",title['author'])


The Best Sellers on 2010-05-09 are the following:

1 . DELIVER US FROM EVIL by David Baldacci
2 . THE HELP by Kathryn Stockett
3 . THE DOUBLE COMFORT SAFARI CLUB by Alexander McCall Smith
4 . THIS BODY OF DEATH by Elizabeth George
5 . LUCID INTERVALS by Stuart Woods
6 . THE SHADOW OF YOUR SMILE by Mary Higgins Clark
7 . BURNING LAMP by Amanda Quick
8 . EVERY LAST ONE by Anna Quindlen
9 . EIGHT DAYS TO LIVE by Iris Johansen
10 . CHANGES by Jim Butcher
11 . CAUGHT by Harlan Coben
12 . HOUSE RULES by Jodi Picoult
13 . MATTERHORN by Karl Marlantes
14 . THE WALK by Richard Paul Evans
15 . DECEPTION by Jonathan Kellerman
16 . BEATRICE AND VIRGIL by Yann Martel
17 . WRECKED by Carol Higgins Clark
18 . SILVER BORNE by Patricia Briggs
19 . ABRAHAM LINCOLN: VAMPIRE HUNTER by Seth Grahame-Smith
20 . A RIVER IN THE SKY by Elizabeth Peters

In [14]:
import requests
date='2009-06-21' #Replace with 2010-05-09,2009-06-21,2010-06-20
url="https://api.nytimes.com/svc/books/v2/lists/"+date+"/hardcover-fiction.json?&num_results=1&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The Best Sellers on",date,"are the following:")
print("")
for n in range(0,len(data['results'])):
    for title in data['results'][n]['book_details']:
        print(n+1,".",title['title'],"by",title['author'])


The Best Sellers on 2009-06-21 are the following:

1 . SKIN TRADE by Laurell K Hamilton
2 . MEDUSA by Clive Cussler and Paul Kemprecos
3 . THE SCARECROW by Michael Connelly
4 . SHANGHAI GIRLS by Lisa See
5 . MATTERS OF THE HEART by Danielle Steel
6 . GONE TOMORROW by Lee Child
7 . DEAD AND GONE by Charlaine Harris
8 . THE 8TH CONFESSION by James Patterson and Maxine Paetro
9 . THE STRAIN by Guillermo del Toro and Chuck Hogan
10 . WICKED PREY by John Sandford
11 . THE HOST by Stephenie Meyer
12 . FIRST FAMILY by David Baldacci
13 . CEMETERY DANCE by Douglas Preston and Lincoln Child
14 . UNDEAD AND UNWELCOME by MaryJanice Davidson
15 . THE HELP by Kathryn Stockett
16 . PYGMY by Chuck Palahniuk
17 . MY FATHER'S TEARS AND OTHER STORIES by John Updike
18 . ROAD DOGS by Elmore Leonard
19 . THE STORY SISTERS by Alice Hoffman
20 . HEARTLESS by Diana Palmer

In [15]:
import requests
date='2010-06-20' #Replace with 2010-05-09,2009-06-21,2010-06-20
url="https://api.nytimes.com/svc/books/v2/lists/"+date+"/hardcover-fiction.json?&num_results=1&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The Best Sellers on",date,"are the following:")
print("")
for n in range(0,len(data['results'])):
    for title in data['results'][n]['book_details']:
        print(n+1,".",title['title'],"by",title['author'])


The Best Sellers on 2010-06-20 are the following:

1 . THE GIRL WHO KICKED THE HORNET’S NEST by Stieg Larsson
2 . BULLET by Laurell K Hamilton
3 . THE SPY by Clive Cussler and Justin Scott
4 . THE HELP by Kathryn Stockett
5 . DEAD IN THE FAMILY by Charlaine Harris
6 . 61 HOURS by Lee Child
7 . THE BURNING WIRE by Jeffery Deaver
8 . STORM PREY by John Sandford
9 . THE BOURNE OBJECTIVE by Eric Van Lustbader
10 . INNOCENT by Scott Turow
11 . HEART OF THE MATTER by Emily Giffin
12 . THE 9TH JUDGMENT by James Patterson and Maxine Paetro
13 . BLOCKADE BILLY by Stephen King
14 . ALLIES by Christie Golden
15 . THE RULE OF NINE by Steve Martini
16 . FEVER DREAM by Lincoln Child and Douglas Preston
17 . DELIVER US FROM EVIL by David Baldacci
18 . MATTERHORN by Karl Marlantes
19 . THE PARTICULAR SADNESS OF LEMON CAKE by Aimee Bender
20 . DANGEROUS by Diana Palmer

2) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?


In [16]:
import requests
date='2009-06-06'
url="https://api.nytimes.com/svc/books/v3/lists/overview.json?&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The different book categories NYT ranked on",date,"are:")
print("")
for n in range(0,len(data['results'])):
   print(data['results']['lists'][n]['list_name'])


The different book categories NYT ranked on 2009-06-06 are:

Combined Print and E-Book Fiction
Combined Print and E-Book Nonfiction
Hardcover Fiction
Hardcover Nonfiction
Trade Fiction Paperback
Mass Market Paperback

In [17]:
import requests
date='2015-06-06'
url="https://api.nytimes.com/svc/books/v3/lists/overview.json?&api-key=4182fa9aca904ae18f4a1f6bef2fc7e9"
response=requests.get(url)
data=response.json()
print("The different book categories NYT ranked on",date,"are:")
print("")
for n in range(0,len(data['results'])):
   print(data['results']['lists'][n]['list_name'])


The different book categories NYT ranked on 2015-06-06 are:

Combined Print and E-Book Fiction
Combined Print and E-Book Nonfiction
Hardcover Fiction
Hardcover Nonfiction
Trade Fiction Paperback
Mass Market Paperback

In [3]:
#Ta-Stephan: you need to specify a date in the API. Here is how you would do it.
url ="https://api.nytimes.com/svc/books/v3/lists/overview.json?published_date=2009-06-06&api-key=9bddd887c630b8078e017396214a150a:15:61062085"
response = requests.get(url)
data = response.json()
for book_list in data['results']['lists']:
    print(book_list['list_name'])


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

3) Muammar Gaddafi's name can be transliterated many many ways. His last name is often a source of a million and one versions - Gadafi, Gaddafi, Kadafi, and Qaddafi to name a few. How many times has the New York Times referred to him by each of those names? Tip: Add "Libya" to your search to make sure (-ish) you're talking about the right guy.


In [18]:
despot_names = ['Gadafi', 'Gaddafi', 'Kadafi', 'Qaddafi']

for name in despot_names:
   despot_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=' + name +'+Libya&api-key=0c3ba2a8848c44eea6a3443a17e57448')
   despot_data = despot_response.json()
      
   despot_hits_meta = despot_data['response']['meta']
   despot_hit_count = despot_hits_meta['hits']
   print("The NYT has referred to the Libyan despot", despot_hit_count, "times using the spelling", name)


The NYT has referred to the Libyan despot 0 times using the spelling Gadafi
The NYT has referred to the Libyan despot 1030 times using the spelling Gaddafi
The NYT has referred to the Libyan despot 4 times using the spelling Kadafi
The NYT has referred to the Libyan despot 5688 times using the spelling Qaddafi

4) What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph?


In [19]:
import requests
term='hipster'
url='http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+ term +'&begin_date=19950101&end_date=19951231&api-key=0c3ba2a8848c44eea6a3443a17e57448'
response=requests.get(url)
data=response.json()
#print(data.keys())
#print(data['response'].keys())
 
print("The main headline for the first article in 1995 mentioning the term hipster is:",data['response']['docs'][0]['headline']['main'])
print("The kicker headline for the first article in 1995 mentioning the term hipster is:",data['response']['docs'][0]['headline']['kicker'])

print("")
print("The first paragrah for the first article in 1995 mentioning the term hipster is:",data['response']['docs'][0]['lead_paragraph'])


The main headline for the first article in 1995 mentioning the term hipster is: SOUND
The kicker headline for the first article in 1995 mentioning the term hipster is: SURFACING

The first paragrah for the first article in 1995 mentioning the term hipster is: 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)

5) How many times was gay marriage mentioned in the NYT between 1950-1959, 1960-1969, 1970-1978, 1980-1989, 1990-2099, 2000-2009, and 2010-present? Tip: You'll want to put quotes around the search term so it isn't just looking for "gay" and "marriage" in the same article. Tip: Write code to find the number of mentions between Jan 1, 1950 and Dec 31, 1959.


In [20]:
search_term='gay marriage'
begin_date='19500101'
end_date='19591231'
gay_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+search_term+'&begin_date='+begin_date+'&end_date='+end_date+'&api-key=0c3ba2a8848c44eea6a3443a17e57448')
gay_data=gay_response.json()

print(gay_data['response']['meta']['hits'],"is the number of times the term",search_term,",appears between",begin_date,"and",end_date)


556 is the number of times the term gay marriage ,appears between 19500101 and 19591231

In [21]:
search_term='gay marriage'
begin_date='19700101'
end_date='19791231'
gay_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+search_term+'&begin_date='+begin_date+'&end_date='+end_date+'&api-key=0c3ba2a8848c44eea6a3443a17e57448')
gay_data=gay_response.json()

print(gay_data['response']['meta']['hits'],"is the number of times the term",search_term,",appears between",begin_date,"and",end_date)


450 is the number of times the term gay marriage ,appears between 19700101 and 19791231

In [22]:
search_term='gay marriage'
begin_date='19800101'
end_date='19891231'
gay_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+search_term+'&begin_date='+begin_date+'&end_date='+end_date+'&api-key=0c3ba2a8848c44eea6a3443a17e57448')
gay_data=gay_response.json()

print(gay_data['response']['meta']['hits'],"is the number of times the term",search_term,",appears between",begin_date,"and",end_date)


368 is the number of times the term gay marriage ,appears between 19800101 and 19891231

In [23]:
search_term='gay marriage'
begin_date='19900101'
end_date='19991231'
gay_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+search_term+'&begin_date='+begin_date+'&end_date='+end_date+'&api-key=0c3ba2a8848c44eea6a3443a17e57448')
gay_data=gay_response.json()

print(gay_data['response']['meta']['hits'],"is the number of times the term",search_term,",appears between",begin_date,"and",end_date)


1102 is the number of times the term gay marriage ,appears between 19900101 and 19991231

In [24]:
search_term='gay marriage'
begin_date='20000101'
end_date='20091231'
gay_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q='+search_term+'&begin_date='+begin_date+'&end_date='+end_date+'&api-key=0c3ba2a8848c44eea6a3443a17e57448')
gay_data=gay_response.json()

print(gay_data['response']['meta']['hits'],"is the number of times the term",search_term,",appears between",begin_date,"and",end_date)


4858 is the number of times the term gay marriage ,appears between 20000101 and 20091231

6) What section talks about motorcycles the most? Tip: You'll be using facets


In [25]:
search_term='motorcycles'
motor_response = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycles&facet_field=section_name&api-key=0c3ba2a8848c44eea6a3443a17e57448')
motor_data=motor_response.json()

print("The section that talks about,",search_term,"the most is",motor_data['response']['facets']['section_name']['terms'][0]['term'])
print("It is mentioned",motor_data['response']['facets']['section_name']['terms'][0]['count'],"times in the section.")


The section that talks about, motorcycles the most is World
It is mentioned 1032 times in the section.

7) How many of the last 20 movies reviewed by the NYT were Critics' Picks? How about the last 40? The last 60? Tip: You really don't want to do this 3 separate times (1-20, 21-40 and 41-60) and add them together. What if, perhaps, you were able to figure out how to combine two lists? Then you could have a 1-20 list, a 1-40 list, and a 1-60 list, and then just run similar code for each of them.


In [40]:
critics_pick_count=0
meh_movie=0
offset_value = 0
movie_list=[]
critical_acclaimed_movies=[]
meh_movies=[]
for page in range(3):
    movie_response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?publication_date=20160611&api-key=07c67436f1864abc8a144c14adff69c8&'+ str(offset_value))
    movie_data=movie_response.json()
    n=0
    n1=0
    movie_list=movie_list+movie_data['results']
    for count in movie_data['results']:
        if(movie_data['results'][n]['critics_pick']==1):
            critics_pick_count=critics_pick_count+1
            #print(movie_data['results'][n]['display_title'],".This movie is critically acclaimed")
            critical_acclaimed_movies=critical_acclaimed_movies+movie_data['results']
        else:
            meh_movie=meh_movie+1
            #print(movie_data['results'][n]['display_title'],".This movie is meh.")
        n=n+1
        

print("The number of critically picked movies is",critics_pick_count)

print("The number of meh movies is",meh_movie)


De Palma .This movie is meh.
Genius .This movie is meh.
Call Her Applebroog .This movie is critically acclaimed
Careful What You Wish For .This movie is meh.
Len and Company .This movie is critically acclaimed
Germans & Jews .This movie is meh.
Puerto Ricans in Paris .This movie is meh.
Last Cab to Darwin .This movie is meh.
Now You See Me 2 .This movie is meh.
King Jack .This movie is critically acclaimed
Be Somebody .This movie is meh.
The Music of Strangers .This movie is meh.
Therapy for a Vampire .This movie is meh.
Tikkun .This movie is meh.
Diary of a Chambermaid .This movie is meh.
The Conjuring 2 .This movie is critically acclaimed
Warcraft .This movie is meh.
'Til Madness Do Us Part .This movie is meh.
From Afar .This movie is critically acclaimed
The Fits .This movie is critically acclaimed
De Palma .This movie is meh.
Genius .This movie is meh.
Call Her Applebroog .This movie is critically acclaimed
Careful What You Wish For .This movie is meh.
Len and Company .This movie is critically acclaimed
Germans & Jews .This movie is meh.
Puerto Ricans in Paris .This movie is meh.
Last Cab to Darwin .This movie is meh.
Now You See Me 2 .This movie is meh.
King Jack .This movie is critically acclaimed
Be Somebody .This movie is meh.
The Music of Strangers .This movie is meh.
Therapy for a Vampire .This movie is meh.
Tikkun .This movie is meh.
Diary of a Chambermaid .This movie is meh.
The Conjuring 2 .This movie is critically acclaimed
Warcraft .This movie is meh.
'Til Madness Do Us Part .This movie is meh.
From Afar .This movie is critically acclaimed
The Fits .This movie is critically acclaimed
De Palma .This movie is meh.
Genius .This movie is meh.
Call Her Applebroog .This movie is critically acclaimed
Careful What You Wish For .This movie is meh.
Len and Company .This movie is critically acclaimed
Germans & Jews .This movie is meh.
Puerto Ricans in Paris .This movie is meh.
Last Cab to Darwin .This movie is meh.
Now You See Me 2 .This movie is meh.
King Jack .This movie is critically acclaimed
Be Somebody .This movie is meh.
The Music of Strangers .This movie is meh.
Therapy for a Vampire .This movie is meh.
Tikkun .This movie is meh.
Diary of a Chambermaid .This movie is meh.
The Conjuring 2 .This movie is critically acclaimed
Warcraft .This movie is meh.
'Til Madness Do Us Part .This movie is meh.
From Afar .This movie is critically acclaimed
The Fits .This movie is critically acclaimed
The number of critically picked movies is 18
The number of meh movies is 42

8) Out of the last 40 movie reviews from the NYT, which critic has written the most reviews?


In [54]:
critics_pick_count=0
meh_movie=0
offset= 0
page=offset+20
reviewers=[]
reviewers1=[]
reviewers2=[]
x=0
import requests
for page in range(40):
    movie_response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?publication_date=20160611&&offset='+str(offset)+'&api-key=07c67436f1864abc8a144c14adff69c8')
    movie_data=movie_response.json()
    movie_results=movie_data['results']
    byline=movie_results[x]['byline']
    reviewers1.insert(x,byline)
    offset=offset+20
    x=x+1
    if(x>19):
        x=0
        reviewers2.insert(x,byline) 
reviewers = reviewers1 + reviewers2
print("The list of all the reviewers are",reviewers)
print("")
print("")
from collections import Counter
most_common,num_most_common = Counter(reviewers).most_common(1)[0]
print("The reviewer who has reviewed the most in the last 40 films is",most_common,"and that person has reviewed",num_most_common,"times")


The list of all the reviewers are ['A. O. SCOTT', 'KEN JAWOROWSKI', 'ANDY WEBSTER', 'KEN JAWOROWSKI', 'GLENN KENNY', 'NEIL GENZLINGER', 'JEANNETTE CATSOULIS', 'JEANNETTE CATSOULIS', 'KEN JAWOROWSKI', 'NICOLAS RAPOLD', 'JEANNETTE CATSOULIS', 'A. O. SCOTT', 'NICOLAS RAPOLD', 'ANDY WEBSTER', 'ANDY WEBSTER', 'MANOHLA DARGIS', 'A. O. SCOTT', 'DANIEL M. GOLD', 'A. O. SCOTT', 'ANDY WEBSTER', 'A. O. SCOTT', 'A. O. SCOTT', 'STEPHEN HOLDEN', 'A. O. SCOTT', 'ANDY WEBSTER', 'KEN JAWOROWSKI', 'STEPHEN HOLDEN', 'A. O. SCOTT', 'NEIL GENZLINGER', 'NICOLAS RAPOLD', 'MIKE HALE', 'ANDY WEBSTER', 'GLENN KENNY', 'GLENN KENNY', 'STEPHEN HOLDEN', 'A. O. SCOTT', 'GLENN KENNY', 'JEANNETTE CATSOULIS', 'GLENN KENNY', 'MANOHLA DARGIS', 'ANDY WEBSTER', 'MANOHLA DARGIS']
The reviewer who has reviewed the most in the last 40 films is A. O. SCOTT and that person has reviewed 9 times