In [131]:
#my IPA key b577eb5b46ad4bec8ee159c89208e220
#base url http://api.nytimes.com/svc/books/{version}/lists
In [132]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists.json?list=hardcover-fiction&published-date=2009-05-10&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
print(best_seller.keys())
In [133]:
print(type(best_seller))
In [134]:
print(type(best_seller['results']))
In [135]:
print(len(best_seller['results']))
In [136]:
print(best_seller['results'][0])
In [137]:
mother_best_seller_results_2009 = best_seller['results']
for item in mother_best_seller_results_2009:
print("This books ranks #", item['rank'], "on the list") #just to make sure they are in order
for book in item['book_details']:
print(book['title'])
In [138]:
print("The top 3 books in the Hardcover fiction NYT best-sellers on Mother's day 2009 were:")
for item in mother_best_seller_results_2009:
if item['rank']< 4: #to get top 3 books on the list
for book in item['book_details']:
print(book['title'])
In [139]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists.json?list=hardcover-fiction&published-date=2010-05-09&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller_2010 = response.json()
print(best_seller.keys())
In [140]:
print(best_seller_2010['results'][0])
In [141]:
mother_best_seller_2010_results = best_seller_2010['results']
In [142]:
print("The top 3 books in the Hardcover fiction NYT best-sellers on Mother's day 2010 were:")
for item in mother_best_seller_2010_results:
if item['rank']< 4: #to get top 3 books on the list
for book in item['book_details']:
print(book['title'])
In [143]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists.json?list=hardcover-fiction&published-date=2009-06-21&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
In [144]:
father_best_seller_results_2009 = best_seller['results']
print("The top 3 books in the Hardcover fiction NYT best-sellers on Father's day 2009 were:")
for item in father_best_seller_results_2009:
if item['rank']< 4: #to get top 3 books on the list
for book in item['book_details']:
print(book['title'])
In [145]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists.json?list=hardcover-fiction&published-date=2010-06-20&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
In [146]:
father_best_seller_results_2010 = best_seller['results']
print("The top 3 books in the Hardcover fiction NYT best-sellers on Father's day 2010 were:")
for item in father_best_seller_results_2010:
if item['rank']< 4: #to get top 3 books on the list
for book in item['book_details']:
print(book['title'])
In [147]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists/names.json?published-date=2009-06-06&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
In [148]:
print(best_seller.keys())
In [149]:
print(len(best_seller['results']))
In [150]:
book_categories_2009 = best_seller['results']
In [151]:
for item in book_categories_2009:
print(item['display_name'])
In [152]:
import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists/names.json?published-date=2015-06-06&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
print(len(best_seller['results']))
In [153]:
book_categories_2015 = best_seller['results']
for item in book_categories_2015:
print(item['display_name'])
Tip: Add "Libya" to your search to make sure (-ish) you're talking about the right guy.
In [154]:
import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gadafi&fq=Libya&api-key=b577eb5b46ad4bec8ee159c89208e220")
gadafi = response.json()
print(gadafi.keys())
print(gadafi['response'])
print(gadafi['response'].keys())
print(gadafi['response']['docs']) #so no results for GADAFI.
In [155]:
print('The New York times has not used the name Gadafi to refer to Muammar Gaddafi')
In [156]:
import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gaddafi&fq=Libya&api-key=b577eb5b46ad4bec8ee159c89208e220")
gaddafi = response.json()
print(gaddafi.keys())
print(gaddafi['response'].keys())
print(type(gaddafi['response']['meta']))
print(gaddafi['response']['meta'])
In [157]:
print("'The New York times used the name Gaddafi to refer to Muammar Gaddafi", gaddafi['response']['meta']['hits'], "times")
In [158]:
import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Kadafi&fq=Libya&api-key=b577eb5b46ad4bec8ee159c89208e220")
kadafi = response.json()
print(kadafi.keys())
print(kadafi['response'].keys())
print(type(kadafi['response']['meta']))
print(kadafi['response']['meta'])
In [159]:
print("'The New York times used the name Kadafi to refer to Muammar Gaddafi", kadafi['response']['meta']['hits'], "times")
In [160]:
import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Qaddafi&fq=Libya&api-key=b577eb5b46ad4bec8ee159c89208e220")
qaddafi = response.json()
print(qaddafi.keys())
print(qaddafi['response'].keys())
print(type(qaddafi['response']['meta']))
print(qaddafi['response']['meta'])
In [161]:
print("'The New York times used the name Qaddafi to refer to Muammar Gaddafi", qaddafi['response']['meta']['hits'], "times")
In [162]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19953112&sort=oldest&api-key=b577eb5b46ad4bec8ee159c89208e220")
hipster = response.json()
print(hipster.keys())
print(hipster['response'].keys())
print(hipster['response']['docs'][0])
hipster_info= hipster['response']['docs']
In [163]:
print('These articles all had the word hipster in them and were published in 1995') #ordered from oldest to newest
for item in hipster_info:
print(item['headline']['main'], item['pub_date'])
In [164]:
for item in hipster_info:
if item['headline']['main'] == "SOUND":
print("This is the first article to mention the word hispter in 1995 and was titled:", item['headline']['main'],"and it was publised on:", item['pub_date'])
print("This is the lead paragraph of", item['headline']['main'],item['lead_paragraph'])
In [165]:
import requests
response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"&begin_date=19500101&end_date=19593112&api-key=b577eb5b46ad4bec8ee159c89208e220')
marriage_1959 = response.json()
print(marriage_1959.keys())
print(marriage_1959['response'].keys())
print(marriage_1959['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_1959['response']['meta']['hits'], "between 1950-1959")
In [166]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=19600101&end_date=19693112&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_1969 = response.json()
print(marriage_1969.keys())
print(marriage_1969['response'].keys())
print(marriage_1969['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_1969['response']['meta']['hits'], "between 1960-1969")
In [188]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=19700101&end_date=19783112&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_1978 = response.json()
print(marriage_1978.keys())
print(marriage_1978['response'].keys())
print(marriage_1978['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_1978['response']['meta']['hits'], "between 1970-1978")
In [189]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=19800101&end_date=19893112&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_1989 = response.json()
print(marriage_1989.keys())
print(marriage_1989['response'].keys())
print(marriage_1989['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_1989['response']['meta']['hits'], "between 1980-1989")
In [190]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=19900101&end_date=20003112&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_2000 = response.json()
print(marriage_2000.keys())
print(marriage_2000['response'].keys())
print(marriage_2000['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_2000['response']['meta']['hits'], "between 1990-2000")
In [191]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=20000101&end_date=20093112&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_2009 = response.json()
print(marriage_2009.keys())
print(marriage_2009['response'].keys())
print(marriage_2009['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_2009['response']['meta']['hits'], "between 2000-2009")
In [192]:
import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q='gay marriage'&begin_date=20100101&end_date=20160609&api-key=b577eb5b46ad4bec8ee159c89208e220")
marriage_2016 = response.json()
print(marriage_2016.keys())
print(marriage_2016['response'].keys())
print(marriage_2016['response']['meta'])
print("___________")
print("Gay marriage was mentioned", marriage_2016['response']['meta']['hits'], "between 2010-present")
In [193]:
import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycles&facet_field=section_name&api-key=b577eb5b46ad4bec8ee159c89208e220")
motorcycles = response.json()
In [194]:
print(motorcycles.keys())
In [195]:
print(motorcycles['response'].keys())
In [196]:
print(motorcycles['response']['facets']['section_name']['terms'])
In [197]:
motorcycles_info= motorcycles['response']['facets']['section_name']['terms']
print(motorcycles_info)
print("These are the sections that talk the most about motorcycles:")
print("_________________")
for item in motorcycles_info:
print("The",item['term'],"section mentioned motorcycle", item['count'], "times")
In [198]:
motorcycle_info= motorcycles['response']['facets']['section_name']['terms']
most_motorcycle_section = 0
section_name = ""
for item in motorcycle_info:
if item['count']>most_motorcycle_section:
most_motorcycle_section = item['count']
section_name = item['term']
print(section_name, "is the sections that talks the most about motorcycles, with", most_motorcycle_section, "mentions of the word")
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 [199]:
import requests
response = requests.get('http://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=b577eb5b46ad4bec8ee159c89208e220')
movies_reviews_20 = response.json()
print(movies_reviews_20.keys())
In [200]:
print(movies_reviews_20['results'][0])
In [201]:
critics_pick = 0
not_a_critics_pick = 0
for item in movies_reviews_20['results']:
print(item['display_title'], item['critics_pick'])
if item['critics_pick'] == 1:
print("-------------CRITICS PICK!")
critics_pick = critics_pick + 1
else:
print("-------------NOT CRITICS PICK!")
not_a_critics_pick = not_a_critics_pick + 1
print("______________________")
print("There were", critics_pick, "critics picks in the last 20 revies by the NYT")
In [211]:
import requests
response = requests.get('http://api.nytimes.com/svc/movies/v2/reviews/search.json?offset=20&api-key=b577eb5b46ad4bec8ee159c89208e220')
movies_reviews_40 = response.json()
print(movies_reviews_40.keys())
import requests
response = requests.get('http://api.nytimes.com/svc/movies/v2/reviews/search.json?offset=40&api-key=b577eb5b46ad4bec8ee159c89208e220')
movies_reviews_60 = response.json()
print(movies_reviews_60.keys())
In [219]:
new_medium_list = movies_reviews_20['results'] + movies_reviews_40['results']
In [220]:
print(len(new_medium_list))
In [221]:
critics_pick = 0
not_a_critics_pick = 0
for item in new_medium_list:
print(item['display_title'], item['critics_pick'])
if item['critics_pick'] == 1:
print("-------------CRITICS PICK!")
critics_pick = critics_pick + 1
else:
print("-------------NOT CRITICS PICK!")
not_a_critics_pick = not_a_critics_pick + 1
print("______________________")
print("There were", critics_pick, "critics picks in the last 40 revies by the NYT")
In [212]:
new_big_list = movies_reviews_20['results'] + movies_reviews_40['results'] + movies_reviews_60['results']
In [222]:
print(new_big_list[0])
In [214]:
print(len(new_big_list))
In [215]:
critics_pick = 0
not_a_critics_pick = 0
for item in new_big_list:
print(item['display_title'], item['critics_pick'])
if item['critics_pick'] == 1:
print("-------------CRITICS PICK!")
critics_pick = critics_pick + 1
else:
print("-------------NOT CRITICS PICK!")
not_a_critics_pick = not_a_critics_pick + 1
print("______________________")
print("There were", critics_pick, "critics picks in the last 60 revies by the NYT")
In [216]:
medium_list = movies_reviews_20['results'] + movies_reviews_40['results']
print(type(medium_list))
print(medium_list[0])
for item in medium_list:
print(item['byline'])
In [217]:
all_critics = []
for item in medium_list:
all_critics.append(item['byline'])
print(all_critics)
unique_medium_list = set(all_critics)
print(unique_medium_list)
print("___________________________________________________")
print("This is a list of the authors who have written the NYT last 40 movie reviews, in descending order:")
from collections import Counter
count = Counter(all_critics)
print(count)
print("___________________________________________________")
print("This is a list of the top 3 authors who have written the NYT last 40 movie reviews:")
count.most_common(3)
Out[217]:
In [ ]:
In [ ]:
In [ ]: