In [3]:
import requests
api=('ceabb939ee5542d3adeab7e2cdc529ef')
In [3]:
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2009-05-10/hardcover-fiction.json?api-key='+api)
mothers_day_09_raw=response.json()
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2010-05-09/hardcover-fiction.json?api-key='+api)
mothers_day_10_raw=response.json()
mothers_day_09_books=mothers_day_09_raw['results']['books']
mothers_day_10_books=mothers_day_10_raw['results']['books']
In [4]:
print("Here's the Mother's Day '09 best seller list for hardcover fiction:")
for book in mothers_day_09_books:
print(book['rank'], book['title'], '-', book['description'])
print("\nAnd here's the same, for '10:")
for book in mothers_day_10_books:
print(book['rank'], book['title'], '-', book['description'])
In [5]:
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2009-06-21/hardcover-fiction.json?api-key='+api)
fathers_day_09_raw=response.json()
response=requests.get('http://api.nytimes.com/svc/books/v3/lists/2010-06-20/hardcover-fiction.json?api-key='+api)
fathers_day_10_raw=response.json()
fathers_day_09_books=fathers_day_09_raw['results']['books']
fathers_day_10_books=fathers_day_10_raw['results']['books']
In [6]:
print("Let's contrast that to Father's Day '09:")
for book in fathers_day_09_books:
print(book['rank'], book['title'], '-', book['description'])
print("\nAnd the same, for '10:")
for book in fathers_day_10_books:
print(book['rank'], book['title'], '-', book['description'])
In [7]:
response=requests.get('https://api.nytimes.com/svc/books/v3/lists/names.json?api-key='+api)
raw_list_names=response.json()
#raw_list_names.keys()
In [8]:
list_names=raw_list_names['results']
#type(list_names)
june_06_2009_list=[]
june_06_2015_list=[]
for name in list_names:
if name['oldest_published_date']<'2009-06-06':
june_06_2009_list.append(name['list_name'])
if name['oldest_published_date']<'2015-06-06':
june_06_2015_list.append(name['list_name'])
print("Here's a big list of all the book categories ranked on June 06, 2009:", ", ".join(june_06_2009_list))
print("\nAnd here's the same list but for 2015:", ", ".join(june_06_2015_list))
In [9]:
muammar_list=['Gadafi', 'Gaddafi', 'Kadafi', 'Qaddafi']
for muammar in muammar_list:
response=requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q='+muammar+'&fq=Libya&api-key='+api)
muammar_data=response.json()
print(muammar, "returns", muammar_data['response']['meta']['hits'], "hits")
In [10]:
response=requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19951231&api-key='+api)
hipster_data=response.json()
earliest_hipster_doc={'title': '???', 'pub_date': '???', 'lede': '???'}
earliest_date='1995-12-31'
for doc in hipster_data['response']['docs']:
pub_date=doc['pub_date'][:-10]
if pub_date<earliest_date:
earliest_date=pub_date
earliest_hipster_doc['title']=doc['headline']['main']
earliest_hipster_doc['pub_date']=pub_date
earliest_hipster_doc['lede']=doc['lead_paragraph']
print("The first article to mention the word hipster in 1995 was titled", earliest_hipster_doc['title'], "and had the publication date", earliest_hipster_doc['pub_date'])
print("And here is the first paragraph of that amazing article:\n", earliest_hipster_doc['lede'])
In [11]:
url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"'
begin_date=[19500101, 19600101, 19700101, 19800101, 19900101, 20000101, 20100101]
end_date=[19591231, 19691231, 19791231, 19891231, 19991231, 20091231, 20161231]
decade_counter=1950
for a in range(0, 7):
response=requests.get(url+'&begin_date='+str(begin_date[a])+'&end_date='+str(end_date[a])+'&api-key='+api)
raw_gay_marriage_doc=response.json()
print("There were", raw_gay_marriage_doc['response']['meta']['hits'], "mentions of gay marriange in the", str(decade_counter)+"'s.")
decade_counter+=10
In [18]:
# This whole section was originally to get the number of hits so we can iterate through all of the pages. Unfortunately,
# you can't actually request beyond page 100 in the nyt api. So instead, I commented this out and said 0 to 101.
# url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycle'
# response=requests.get(url+'&api-key='+api)
# raw_motorcycle_doc=response.json()
# hits=raw_motorcycle_doc['response']['meta']['hits']
url='https://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycle'
motorcycle_sections=[]
for page in range(0, 101):
response=requests.get(url+'&page='+str(page)+'&api-key='+api)
raw_motorcycle_doc=response.json()
for doc in raw_motorcycle_doc['response']['docs']:
motorcycle_sections.append(doc['section_name'])
print("did page", page)
In [23]:
from collections import Counter
most_motorcycle_section={'section': '???', 'total': 0}
cleaned_list=dict(Counter(motorcycle_sections))
for key, value in cleaned_list.items():
if key and value > most_motorcycle_section['total']:
most_motorcycle_section={'section': key, 'total': value}
print("new value:", key, value)
print("The section that mentions motorcycles the most (at least on the first 100 pages) is", most_motorcycle_section['section'], "with", most_motorcycle_section['total'], "total mentions")
In [4]:
url="https://api.nytimes.com/svc/movies/v2/reviews/search.json"
critics_picks=[]
#movie_list=[]
reviewer_list=[]
offset=0
for a in range(0,3):
response=requests.get(url+"?offset="+str(offset)+"&api-key="+api)
movie_list_raw=response.json()
for movie in movie_list_raw['results']:
#print(movie.keys())
#movie_list.append({'title': movie['display_title'], 'reviewer': movie['byline']})
reviewer_list.append(movie['byline'])
if movie['critics_pick']:
critics_picks.append(movie['display_title'])
print("Of the first", offset+20, "titles,", len(critics_picks), "were critic's picks")
offset+=20
#print(critics_picks)
In [14]:
from collections import Counter
reviewiest=Counter(reviewer_list).most_common(1)[0]
print("The reviewer with the most reviews was", reviewiest[0].title(), "with", reviewiest[1], "total")
In [ ]:
In [ ]: