In [2]:
import requests

In [3]:
response = requests.get('http://api.nytimes.com/svc/books/v2/lists/2010-05-09/hardcover-fiction.json?api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)

In [4]:
#print(data.keys())

In [5]:
#print(data['results'])

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


In [6]:
#What books topped the Hardcover Fiction NYT best-sellers list on Mother's Day in 2009 and 2010? How about Father's Day?
#mothers day in 2010
book_result = data['results']

#print(book_result)

print("The hardcover Fiction NYT best-sellers on mothers day in 2010 are:")

for i in book_result:
    #print(i['book_details'])
    for item in i['book_details']:
        print("-", item['title'])


The hardcover Fiction NYT best-sellers on mothers day in 2010 are:
- DELIVER US FROM EVIL
- THE HELP
- THE DOUBLE COMFORT SAFARI CLUB
- THIS BODY OF DEATH
- LUCID INTERVALS
- THE SHADOW OF YOUR SMILE
- BURNING LAMP
- EVERY LAST ONE
- EIGHT DAYS TO LIVE
- CHANGES
- CAUGHT
- HOUSE RULES
- MATTERHORN
- THE WALK
- DECEPTION
- BEATRICE AND VIRGIL
- WRECKED
- SILVER BORNE
- ABRAHAM LINCOLN: VAMPIRE HUNTER
- A RIVER IN THE SKY

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


In [7]:
#mothers day in 2009
response = requests.get('http://api.nytimes.com/svc/books/v2/lists/2009-05-10/hardcover-fiction.json?api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)

print("The hardcover Fiction NYT best-sellers on mothers day in 2009 are:")
book_result = data['results']

#print(book_result)

for i in book_result:
    #print(i['book_details'])
    for item in i['book_details']:
        print("-",item['title'])


The hardcover Fiction NYT best-sellers on mothers day in 2009 are:
- FIRST FAMILY
- TEA TIME FOR THE TRADITIONALLY BUILT
- LOITERING WITH INTENT
- JUST TAKE MY HEART
- THE PERFECT POISON
- THE HOST
- LOOK AGAIN
- DEADLOCK
- LONG LOST
- TURN COAT
- THE ASSOCIATE
- HANDLE WITH CARE
- THE HELP
- THE GUERNSEY LITERARY AND POTATO PEEL PIE SOCIETY
- FATALLY FLAKY
- ARTHAS
- A RELIABLE WIFE
- BORDERLINE
- ONE SECOND AFTER
- BONEMAN'S DAUGHTERS

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


In [8]:
#fathers day in 2010
response = requests.get('http://api.nytimes.com/svc/books/v2/lists/2010-06-20/hardcover-fiction.json?api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)
print("The hardcover Fiction NYT best-sellers on fathers day in 2010 are:")
book_result = data['results']

#print(book_result)

for i in book_result:
    #print(i['book_details'])
    for item in i['book_details']:
        print("-", item['title'])


The hardcover Fiction NYT best-sellers on fathers day in 2010 are:
- THE GIRL WHO KICKED THE HORNET’S NEST
- BULLET
- THE SPY
- THE HELP
- DEAD IN THE FAMILY
- 61 HOURS
- THE BURNING WIRE
- STORM PREY
- THE BOURNE OBJECTIVE
- INNOCENT
- HEART OF THE MATTER
- THE 9TH JUDGMENT
- BLOCKADE BILLY
- ALLIES
- THE RULE OF NINE
- FEVER DREAM
- DELIVER US FROM EVIL
- MATTERHORN
- THE PARTICULAR SADNESS OF LEMON CAKE
- DANGEROUS

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


In [9]:
#fathers day in 2009
response = requests.get('http://api.nytimes.com/svc/books/v2/lists/2009-06-21/hardcover-fiction.json?api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)
book_result = data['results']
print("The hardcover Fiction NYT best-sellers on fathers day in 2009 are:")

#print(book_result)

for i in book_result:
    #print(i['book_details'])
    for item in i['book_details']:
        print("-", item['title'])


The hardcover Fiction NYT best-sellers on fathers day in 2009 are:
- SKIN TRADE
- MEDUSA
- THE SCARECROW
- SHANGHAI GIRLS
- MATTERS OF THE HEART
- GONE TOMORROW
- DEAD AND GONE
- THE 8TH CONFESSION
- THE STRAIN
- WICKED PREY
- THE HOST
- FIRST FAMILY
- CEMETERY DANCE
- UNDEAD AND UNWELCOME
- THE HELP
- PYGMY
- MY FATHER'S TEARS AND OTHER STORIES
- ROAD DOGS
- THE STORY SISTERS
- HEARTLESS

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


In [10]:
#What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?

response = requests.get('http://api.nytimes.com/svc/books/v2/lists/names.json?date=2009-06-06&api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)

In [11]:
#What are all the different book categories the NYT ranked in June 6, 2009
book_result = data['results']
print("The following are the different book categories the NYT ranked in June 6, 2009:")

#print(book_result)

for i in book_result:
    print("-", i['display_name'])


The following are the different book categories the NYT ranked in June 6, 2009:
- Hardcover Fiction
- Hardcover Nonfiction
- Paperback Trade Fiction
- Paperback Mass-Market Fiction
- Paperback Nonfiction
- Hardcover Advice & Misc.
- Paperback Advice & Misc.
- Children’s Chapter Books
- Children’s Paperback Books
- Children’s Picture Books
- Children’s Series
- Hardcover Graphic Books
- Paperback Graphic Books
- Manga

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


In [12]:
#What are all the different book categories the NYT ranked in June 6, 2015?

response = requests.get('http://api.nytimes.com/svc/books/v2/lists/names.json?date=2015-06-06&api-key=3880684abea14d86b6280c6dbd80a793')
data = response.json()
#print(data)
book_result = data['results']
print("The following are the different book categories the NYT ranked in June 6, 2015:")

#print(book_result)

for i in book_result:
    print("-", i['display_name'])


The following are the different book categories the NYT ranked in June 6, 2015:
- Combined Print & E-Book Fiction
- Combined Print & E-Book Nonfiction
- Hardcover Fiction
- Hardcover Nonfiction
- Paperback Trade Fiction
- Paperback Mass-Market Fiction
- Paperback Nonfiction
- E-Book Fiction
- E-Book Nonfiction
- Advice, How-To & Miscellaneous
- Children’s Middle Grade
- Children’s Picture Books
- Children’s Series
- Young Adult
- Hardcover Graphic Books
- Paperback Graphic Books
- Manga
- Animals
- Business
- Celebrities
- Crime and Punishment
- Culture
- Education
- Espionage
- Expeditions
- Fashion, Manners and Customs
- Food and Diet
- Games and Activities
- Health
- Humor
- Indigenous Americans
- Love and Relationships
- Parenthood and Family
- Politics and American History
- Race and Civil Rights
- Religion, Spirituality and Faith
- Science
- Sports and Fitness
- Travel

3) Finding the Total Occurrence of Muammar Gaddafi's

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 [13]:
Gadafi_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gadafi&fq=Libya&api-key=3880684abea14d86b6280c6dbd80a793')
Gadafi_data = Gadafi_response.json()
Gadafi_data_result = Gadafi_data['response']['meta']['hits']
print("Gadafi appears", Gadafi_data_result, "times")
Gaddafi_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gaddafi&fq=Libya&api-key=3880684abea14d86b6280c6dbd80a793')
Gaddafi_data = Gaddafi_response.json()
Gaddafi_data_result = Gaddafi_data['response']['meta']['hits']
print("Gaddafi appears", Gaddafi_data_result, "times")
Kadafi_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=Kadafi&fq=Libya&api-key=3880684abea14d86b6280c6dbd80a793')
Kadafi_data = Kadafi_response.json()
Kadafi_data_result = Kadafi_data['response']['meta']['hits']
print("Kadafi appears", Kadafi_data_result, "times")
Qaddafi_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=Qaddafi&fq=Libya&api-key=3880684abea14d86b6280c6dbd80a793')
Qaddafi_data = Qaddafi_response.json()
Qaddafi_data_result = Qaddafi_data['response']['meta']['hits']
print("Qaddafi appears", Qaddafi_data_result, "times")


Gadafi appears 0 times
Gaddafi appears 1020 times
Kadafi appears 3 times
Qaddafi appears 5252 times

4a) Hipster

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


In [14]:
# testing it for count
hipster_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19953112&sort=oldest&api-key=3880684abea14d86b6280c6dbd80a793')
hipster_data = hipster_response.json()
hipster_data_result = hipster_data['response']['meta']['hits']
print("hipster appears", hipster_data_result, "times")


hipster appears 3047 times

4b) Hipster

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


In [15]:
#print(hipster_data)
#print(hipster_data.keys())
hipster_resp = hipster_data['response']
hipster_resp = hipster_data['response']['docs']
for item in hipster_resp:
    print(item['headline']['main'], item['pub_date'])
#print("The word, hipster, appears for the first time in the following article: ", hipster_resp)
#hipster_para = hipster_data['response']['docs'][0]['lead_paragraph']
#print("The word, hipster, appears for the first time in the following paragraph:")
#print("-------------------------------------------------------------------------")
#print(hipster_para)


SOUND 1995-02-05T00:00:00Z
The Revenge of the Un-Hip 1995-02-05T00:00:00Z
For Actors, a Casting Call And a Cautionary Note 1995-02-12T00:00:00Z
Terminal Kitsch 1995-02-12T00:00:00Z
From Lumpy Blue Mass To Chic Blue Line 1995-03-05T00:00:00Z
Guys' Stuff (for Guys?) 1995-03-19T00:00:00Z
Mixed Results From 2 Designers 1995-04-06T00:00:00Z
Reviews/Fashion; Cool Rises to Intimidating Heights 1995-04-07T00:00:00Z
Oswald and Mailer: The Eternal Basic Questions 1995-04-25T00:00:00Z
By Design; Westward Ho! 1995-04-25T00:00:00Z

5) Gay Marriage

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.


In [28]:
gay50S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"&begin_date=19500101&end_date=19591231&api-key=3880684abea14d86b6280c6dbd80a793')
gay50S_data = gay50S_response.json()
gay50S_data_result = gay50S_data['response']['meta']['hits']
print("Gay Marriage appears", gay50S_data_result, "times in the period 1950-1959")


Gay Marriage appears 0 times in the period 1950-1959

In [17]:
gay60S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"&begin_date=19600101&end_date=19691231&api-key=3880684abea14d86b6280c6dbd80a793')
gay60S_data = gay60S_response.json()
gay60S_data_result = gay60S_data['response']['meta']['hits']
print("Gay Marriage appears", gay60S_data_result, "times in the period 1960-1969")


Gay Marriage appears 0 times in the period 1960-1969

In [18]:
#1950
gay50S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19500101&end_date=19591231&api-key=3880684abea14d86b6280c6dbd80a793')
gay50S_data = gay50S_response.json()
gay50S_data_result = gay50S_data['response']['meta']['hits']
print("Gay Marriage appears", gay50S_data_result, "times in the period 1950-1959")

#1960

gay60S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19600101&end_date=19691231&api-key=3880684abea14d86b6280c6dbd80a793')
gay60S_data = gay60S_response.json()
gay60S_data_result = gay60S_data['response']['meta']['hits']
print("Gay Marriage appears", gay60S_data_result, "times in the period 1960-1969")

#1970
gay70S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19700101&end_date=19781231&api-key=3880684abea14d86b6280c6dbd80a793')
gay70S_data = gay70S_response.json()
gay70S_data_result = gay70S_data['response']['meta']['hits']
print("Gay Marriage appears", gay70S_data_result, "times in the period 1970-1978")
#1980
gay80S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19800101&end_date=19891231&api-key=3880684abea14d86b6280c6dbd80a793')
gay80S_data = gay80S_response.json()
gay80S_data_result = gay80S_data['response']['meta']['hits']
print("Gay Marriage appears", gay80S_data_result, "times in the period 1980-1989")
#1990
gay90S_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19900101&end_date=19991231&api-key=3880684abea14d86b6280c6dbd80a793')
gay90S_data = gay90S_response.json()
gay90S_data_result = gay90S_data['response']['meta']['hits']
print("Gay Marriage appears", gay90S_data_result, "times in the period 1990-1999")
#2000
gay00s_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=20000101&end_date=20091231&api-key=3880684abea14d86b6280c6dbd80a793')
gay00s_data = gay00s_response.json()
gay00s_data_result = gay00s_data['response']['meta']['hits']
print("Gay Marriage appears", gay00s_data_result, "times in the period 2000-2009")
# 2010
gm_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=20100101&api-key=3880684abea14d86b6280c6dbd80a793')
gm_data = gm_response.json()
gm_data_result = gm_data['response']['meta']['hits']
print("Gay Marriage appears", gm_data_result, "times from the year 2010")


Gay Marriage appears 0 times in the period 1950-1959
Gay Marriage appears 0 times in the period 1960-1969
Gay Marriage appears 0 times in the period 1970-1978
Gay Marriage appears 3 times in the period 1980-1989
Gay Marriage appears 137 times in the period 1990-1999
Gay Marriage appears 2510 times in the period 2000-2009
Gay Marriage appears 4758 times from the year 2010

6) What section talks about motorcycles the most?

Tip: You'll be using facets


In [19]:
motor_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycle&facet_field=section_name&api-key=3880684abea14d86b6280c6dbd80a793')
motor_data = motor_response.json()
#motor_data_result = motor_data['response']['meta']['hits']
#print("Motorcycles appear", motor_data_result, "times")
motor_info = motor_data['response']['facets']['section_name']['terms']
print("This input gives all the sections and count:", motor_info)
#for i in motor_info:
    #print(i)
print("Therefore, Motorcycles appear", motor_info[0]['term'], "section the most")


This input gives all the sections and count: [{'count': 1742, 'term': 'World'}, {'count': 1265, 'term': 'U.S.'}, {'count': 1116, 'term': 'Sports'}, {'count': 1016, 'term': 'New York and Region'}, {'count': 922, 'term': 'Arts'}]
Therefore, Motorcycles appear World section the most

7) Critics's Picks

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 [20]:
#first 20 movies
movie_response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?&api-key=3880684abea14d86b6280c6dbd80a793')
movie_data = movie_response.json()
#print(movie_data)
#print(movie_data.keys())
count = 0
movie_result = movie_data['results']
for i in movie_result:
    #print(i)
    #print(i.keys())
     #print(item)
    if i['critics_pick']:
        count = count + 1
print("Out of last 20 movies", count, "movies were critics picks")

#first 40 movies
movie_response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?&&offset=20&api-key=3880684abea14d86b6280c6dbd80a793')
movie_data = movie_response.json()
#print(movie_data)
#print(movie_data.keys())
count_40 = 0
movie_result = movie_data['results']
for i in movie_result:
    #print(i)
    #print(i.keys())
     #print(item)
    if i['critics_pick']:
        count_40 = count_40 + 1
#print(count_40)
last_fourty = count + count_40
print("Out of last 40 movies", last_fourty, "movies were critics picks")

#first 60 movies
movie_response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?&offset=40&api-key=3880684abea14d86b6280c6dbd80a793')
movie_data = movie_response.json()
#print(movie_data)
#print(movie_data.keys())
count_60 = 0
movie_result = movie_data['results']
for i in movie_result:
    #print(i)
    #print(i.keys())
     #print(item)
    if i['critics_pick']:
        count_60 = count_60 + 1
#print(count_60)
last_sixty = last_fourty + count_60
print("Out of last 60 movies", last_sixty, "movies were critics picks")


Out of last 20 movies 6 movies were critics picks
Out of last 40 movies 15 movies were critics picks
Out of last 60 movies 20 movies were critics picks

8) Critics with Highest Reviews

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


In [21]:
last_fourty = []
offset = [0, 20]

for n in offset:
    url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&offset=" + str(n)
    movie_reviews_60 = requests.get(url)
    movie_reviews_60 = movie_reviews_60.json()
    last_fourty = last_fourty + movie_reviews_60['results']

print(len(last_fourty))


40

In [22]:
list_byline = []
for i in last_fourty:
    list_byline.append(i['byline'])
print(list_byline)
max_occ = max(list_byline)


['A. O. SCOTT', 'A. O. SCOTT', 'GLENN KENNY', 'NEIL GENZLINGER', 'ANDY WEBSTER', 'KEN JAWOROWSKI', 'ANDY WEBSTER', 'DANIEL M. GOLD', 'JEANNETTE CATSOULIS', 'HELEN T. VERONGOS', 'ANDY WEBSTER', 'KEN JAWOROWSKI', 'MANOHLA DARGIS', 'A. O. SCOTT', 'STEPHEN HOLDEN', 'NEIL GENZLINGER', 'MANOHLA DARGIS', 'BEN KENIGSBERG', 'STEPHEN HOLDEN', 'MANOHLA DARGIS', 'STEPHEN HOLDEN', 'A. O. SCOTT', 'STEPHEN HOLDEN', 'NEIL GENZLINGER', 'BEN KENIGSBERG', 'GLENN KENNY', 'NEIL GENZLINGER', 'HELEN T. VERONGOS', 'BEN KENIGSBERG', 'GLENN KENNY', 'JEANNETTE CATSOULIS', 'GLENN KENNY', 'ANDY WEBSTER', 'A. O. SCOTT', 'HELEN T. VERONGOS', 'JEANNETTE CATSOULIS', 'ANDY WEBSTER', 'GLENN KENNY', 'KEN JAWOROWSKI', 'ANDY WEBSTER']

In [23]:
print("The author with the highest number of reviews to his credit is", max_occ)


The author with the highest number of reviews to his credit is STEPHEN HOLDEN

In [ ]: