Graded = 8/8
In [55]:
#1) What books topped the Hardcover Fiction NYT best-sellers list on
#Mother's Day in 2009 and 2010? How about Father's Day?
#Establishing the name of bestseller lists
In [56]:
import requests
In [57]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=71621eb479f045bf8bee783b6943fdd4')
Bestseller_lists = response.json()
In [58]:
Bestseller_lists.keys()
Out[58]:
In [59]:
type(Bestseller_lists['results'])
Out[59]:
In [60]:
bestseller = Bestseller_lists['results']
for x in bestseller:
print(x['list_name'])
In [61]:
#Now looking at the best seller list on Mother's Day May 10 2009 and Mother's Day May 2010.
In [62]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2009-05-10/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [63]:
books.keys()
Out[63]:
In [64]:
type(books['results'])
Out[64]:
In [65]:
print(books['results'].keys())
In [66]:
books_details = books['results']['books']
type(books)
Out[66]:
In [67]:
print("The list of books topping the bestseller's list of Hardcore Fiction on Mother's Day 2009 was:")
for title in books_details:
print(title['title'])
In [68]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-05-09/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [69]:
books_details_2010 = books['results']['books']
print("The list of books topping the bestseller's list of Hardcore Fiction on Mother's Day 2010 was:")
for title in books_details_2010:
print(title['title'])
In [70]:
#And now for father's day 2009 and 2010:
In [71]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-06-21/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [72]:
books_details_2009_fathers = books['results']['books']
print("The list of books topping the bestseller's list of Hardcore Fiction on fathers's Day 2009 was:")
for title in books_details_2009_fathers:
print(title['title'])
In [73]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-06-20/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [74]:
books_details_2010_fathers = books['results']['books']
print("The list of books topping the bestseller's list of Hardcore Fiction on fathers's Day 2010 was:")
for title in books_details_2010_fathers:
print(title['title'])
In [75]:
#2) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?
In [76]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/overview.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&published_date=2009-06-06')
categories = response.json()
In [77]:
categories.keys()
Out[77]:
In [78]:
categories['results'].keys()
Out[78]:
In [79]:
Lists = categories['results']['lists']
type(Lists)
Out[79]:
In [80]:
print("These were the NYTimes book categories on 6 June 2009:")
for list in Lists:
print(list['list_name'])
In [81]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/overview.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&published_date=2015-06-06')
categories = response.json()
In [82]:
Lists = categories['results']['lists']
print("And these were the NYTimes book categories on 6 June 2015:")
for list in Lists:
print(list['list_name'])
In [83]:
#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?
In [84]:
responseQaddafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Qaddafi&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseKadafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Kadafi&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseGaddafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gaddafi&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseGadafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gadafi&api-key=a39223b33e0e46fd82dbddcc4972ff91')
Qaddafi = responseQaddafi.json()
Kadafi = responseKadafi.json()
Gaddafi = responseGaddafi.json()
Gadafi = responseGadafi.json()
In [85]:
Qaddafi.keys()
Out[85]:
In [86]:
type(Qaddafi['response']['meta'])
Out[86]:
In [87]:
Qaddafi['response']['meta'].keys()
Out[87]:
In [88]:
#Without adding Libya to the search
In [89]:
print("The New York Times used the Qaddafi spelling", Qaddafi['response']['meta']['hits'], "times.")
print("The New York Times used the Kadafi spelling", Kadafi['response']['meta']['hits'], "times.")
print("The New York Times used the Gaddafi spelling", Gaddafi['response']['meta']['hits'], "times.")
print("The New York Times used the Gadafi spelling", Gadafi['response']['meta']['hits'], "times.")
In [90]:
#Added Libya to the search
In [91]:
responseQaddafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Qaddafi&fq=Libya&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseKadafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Kadafi&fq=Libya&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseGaddafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gaddafi&fq=Libya&api-key=a39223b33e0e46fd82dbddcc4972ff91')
responseGadafi = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gadafi&fq=Libya&api-key=a39223b33e0e46fd82dbddcc4972ff91')
Qaddafi = responseQaddafi.json()
Kadafi = responseKadafi.json()
Gaddafi = responseGaddafi.json()
Gadafi = responseGadafi.json()
In [92]:
print("The New York Times used the Qaddafi spelling", Qaddafi['response']['meta']['hits'], "times.")
print("The New York Times used the Kadafi spelling", Kadafi['response']['meta']['hits'], "times.")
print("The New York Times used the Gaddafi spelling", Gaddafi['response']['meta']['hits'], "times.")
print("The New York Times used the Gadafi spelling", Gadafi['response']['meta']['hits'], "times.")
In [ ]:
#4) What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph?
In [94]:
Hipster_Hits = (Hipster['response']['meta'])
In [93]:
hipster_response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19951231&sort=oldest&api-key=a39223b33e0e46fd82dbddcc4972ff91')
Hipster = hipster_response.json()
Hipster.keys()
Out[93]:
In [95]:
Hipster_Hits = (Hipster['response']['meta'])
In [96]:
print(Hipster_Hits['hits'])
In [97]:
Hipster['response']['docs'][0]['headline'].keys()
Out[97]:
In [98]:
print("The title of the first article in 1995 that mentioned Hipster was", Hipster['response']['docs'][0]['headline']['kicker'], Hipster['response']['docs'][0]['headline']['main'])
In [99]:
#print(Hipster['response']['docs'])
print("This is the leading paragraph of the first article that mentioned Hipster in 1995:", Hipster['response']['docs'][0]['lead_paragraph'])
In [100]:
#5) How many times was gay marriage mentioned in the NYT between 1950-1959, 1960-1969, 1970-1978, 1980-1989, 1990-1999, 2000-2009, and 2010-present?
In [101]:
r50s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19500101&end_date=19591231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r60s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19600101&end_date=19691231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r70s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19700101&end_date=19791231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r80s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19800101&end_date=19891231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r90s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=19900101&end_date=19991231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r00s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=20000101&end_date=20091231&api-key=a39223b33e0e46fd82dbddcc4972ff91')
r10s = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22gay%20marriage%22&begin_date=20100101&end_date=20160707&api-key=a39223b33e0e46fd82dbddcc4972ff91')
re50s = r50s.json()
re60s = r60s.json()
re70s = r70s.json()
re80s = r80s.json()
re90s = r90s.json()
re00s = r00s.json()
re10s = r10s.json()
In [102]:
print("1950 - 1959:", re50s['response']['meta']['hits'])
print("1960 - 1969:", re60s['response']['meta']['hits'])
print("1970 - 1979:", re70s['response']['meta']['hits'])
print("1980 - 1989:", re80s['response']['meta']['hits'])
print("1990 - 1999:", re90s['response']['meta']['hits'])
print("2000 - 2009:", re00s['response']['meta']['hits'])
print("2010 - 2016:", re10s['response']['meta']['hits'])
In [103]:
#6) What section talks about motorcycles the most? **Tip: You'll be using facets**
In [104]:
#Searching for all the section names
motorcyle_facets = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycles&facet_field=section_name&api-key=a39223b33e0e46fd82dbddcc4972ff91')
motorcycles = motorcyle_facets.json()
motorcycles.keys()
Out[104]:
In [105]:
motorcycles['response'].keys()
Out[105]:
In [106]:
type(motorcycles['response']['meta'])
Out[106]:
In [107]:
print(motorcycles['response']['meta'])
In [108]:
print(motorcycles['response']['facets'])
In [109]:
motorcycles['response']['facets'].keys()
Out[109]:
In [110]:
print(motorcycles['response']['facets']['section_name'])
In [111]:
print(motorcycles['response']['facets']['section_name']['terms'])
In [112]:
Sections_count = motorcycles['response']['facets']['section_name']['terms']
for count in Sections_count:
print(count['term'], count['count'])
In [113]:
#7) How many of the last 20 movies reviewed by the NYT were Critics' Picks? How about the last 40? The last 60?
In [114]:
response = requests.get('https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08')
movie_reviews = response.json()
movie_reviews.keys()
Out[114]:
In [115]:
#movie_reviews = movie_reviews['results']
In [116]:
movie_reviews = movie_reviews['results']
In [117]:
critics_pick = 0
for name in movie_reviews:
if name['critics_pick']:
critics_pick = critics_pick + 1
#print(name['display_title'], name['critics_pick'])
print("Out ot the first 20 movie reviews", critics_pick, "were from critics' pick.")
In [118]:
All_results = []
offset = [0, 20, 40]
#url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset="
for n in offset:
url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset=" + str(n)
movie_reviews_60 = requests.get(url)
movie_reviews_60 = movie_reviews_60.json()
All_results = All_results + movie_reviews_60['results']
print(len(All_results))
In [119]:
movie_reviews_60.keys()
Out[119]:
In [120]:
type(movie_reviews_60['results'])
Out[120]:
In [121]:
critics_pick_60 = 0
for name in All_results:
#print(name['display_title'], name['critics_pick'])
if name['critics_pick']:
critics_pick_60 = critics_pick_60 + 1
print("Out ot the first 60 movie reviews", critics_pick_60, "were from critics' pick.")
In [122]:
All_results = []
offset = [0, 20]
#url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset="
for n in offset:
url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset=" + str(n)
movie_reviews_40 = requests.get(url)
movie_reviews_40 = movie_reviews_40.json()
All_results = All_results + movie_reviews_40['results']
print(len(All_results))
In [123]:
critics_pick_40 = 0
for name in All_results:
#print(name['display_title'], name['critics_pick'])
if name['critics_pick']:
critics_pick_40 = critics_pick_40 + 1
print("Out ot the first 40 movie reviews", critics_pick_40, "were from critics' pick.")
In [124]:
#8)Out of the last 40 movie reviews from the NYT, which critic has written the most reviews?
In [125]:
All_results = []
offset = [0, 20]
#url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset="
for n in offset:
url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=a39223b33e0e46fd82dbddcc4972ff91&publication-date=2015-01-01;2016-06-08&offset=" + str(n)
movie_reviews_60 = requests.get(url)
movie_reviews_60 = movie_reviews_60.json()
All_results = All_results + movie_reviews_60['results']
print(len(All_results))
In [126]:
#for byline in All_results:
# print(byline['byline'])
In [127]:
byline_list = []
for by_line in All_results:
byline_list.append(by_line['byline'])
print(byline_list)
In [128]:
from collections import Counter
In [129]:
counts = Counter(byline_list)
In [130]:
print("The author with the most number of counts is", counts.most_common(1))
In [ ]:
In [ ]: