In [1]:
#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 [2]:
import requests
In [3]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=71621eb479f045bf8bee783b6943fdd4')
Bestseller_lists = response.json()
In [4]:
Bestseller_lists.keys()
Out[4]:
In [5]:
type(Bestseller_lists['results'])
Out[5]:
In [6]:
bestseller = Bestseller_lists['results']
for x in bestseller:
print(x['list_name'])
In [7]:
#Now looking at the best seller list on Mother's Day May 10 2009 and Mother's Day May 2010.
In [8]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2009-05-10/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [9]:
books.keys()
Out[9]:
In [10]:
type(books['results'])
Out[10]:
In [11]:
print(books['results'].keys())
In [12]:
books_details = books['results']['books']
type(books)
Out[12]:
In [13]:
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 [14]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-05-09/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [15]:
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 [16]:
#And now for father's day 2009 and 2010:
In [17]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-06-21/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [18]:
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 [19]:
response = requests.get('https://api.nytimes.com/svc/books/v3/lists/2010-06-20/hardcover-fiction.json?api-key=a39223b33e0e46fd82dbddcc4972ff91')
books = response.json()
In [20]:
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 [21]:
#2) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?
In [22]:
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 [23]:
categories.keys()
Out[23]:
In [24]:
categories['results'].keys()
Out[24]:
In [25]:
Lists = categories['results']['lists']
type(Lists)
Out[25]:
In [26]:
print("These were the NYTimes book categories on 6 June 2009:")
for list in Lists:
print(list['list_name'])
In [27]:
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 [28]:
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 [29]:
#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 [30]:
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 [31]:
Qaddafi.keys()
Out[31]:
In [32]:
type(Qaddafi['response']['meta'])
Out[32]:
In [33]:
Qaddafi['response']['meta'].keys()
Out[33]:
In [34]:
#Without adding Libya to the search
In [35]:
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 [36]:
#Added Libya to the search
In [37]:
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 [38]:
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 [39]:
#4) What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph?
In [44]:
Hipster_Hits = (Hipster['response']['meta'])
In [59]:
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[59]:
In [77]:
Hipster_Hits = (Hipster['response']['meta'])
In [78]:
print(Hipster_Hits['hits'])
In [97]:
Hipster['response']['docs'][0]['headline'].keys()
Out[97]:
In [99]:
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 [100]:
#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 [50]:
#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 [51]:
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 [52]:
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 [53]:
#6) What section talks about motorcycles the most? **Tip: You'll be using facets**
In [54]:
#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[54]:
In [55]:
motorcycles['response'].keys()
Out[55]:
In [56]:
type(motorcycles['response']['meta'])
Out[56]:
In [57]:
print(motorcycles['response']['meta'])
In [58]:
print(motorcycles['response']['facets'])
In [59]:
motorcycles['response']['facets'].keys()
Out[59]:
In [60]:
print(motorcycles['response']['facets']['section_name'])
In [61]:
print(motorcycles['response']['facets']['section_name']['terms'])
In [62]:
Sections_count = motorcycles['response']['facets']['section_name']['terms']
for count in Sections_count:
print(count['term'], count['count'])
In [63]:
#7) How many of the last 20 movies reviewed by the NYT were Critics' Picks? How about the last 40? The last 60?
In [64]:
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[64]:
In [65]:
#movie_reviews = movie_reviews['results']
In [66]:
movie_reviews = movie_reviews['results']
In [67]:
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 [68]:
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 [69]:
movie_reviews_60.keys()
Out[69]:
In [70]:
type(movie_reviews_60['results'])
Out[70]:
In [71]:
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 [72]:
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 [73]:
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 [74]:
#8)Out of the last 40 movie reviews from the NYT, which critic has written the most reviews?
In [82]:
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 [86]:
#for byline in All_results:
# print(byline['byline'])
In [84]:
byline_list = []
for by_line in All_results:
byline_list.append(by_line['byline'])
print(byline_list)
In [ ]:
from collections import Counter
In [ ]:
counts = Counter(byline_list)
In [ ]:
print("The author with the most number of counts is", counts.most_common(1))
In [ ]:
In [ ]: