import requests

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)

#print(data.keys())

#print(data['results'])

"""
Explanation: Grade:
End of explanation
"""


#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'])
    

    

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


#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'])
    

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


#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'])
    

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


#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'])

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


#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)

#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'])
    

"""
Explanation: 2a) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?
End of explanation
"""


#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'])

"""
Explanation: 2b) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015?
End of explanation
"""


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")

    

"""
Explanation: 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.
End of explanation
"""


# 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")

"""
Explanation: 4a) Hipster
What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph?
End of explanation
"""


#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)

# TA-COMMENT: (-0.5) Missing the first paragraph of the first story

"""
Explanation: 4b) Hipster
What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph?
End of explanation
"""


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")

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")

# TA-COMMENT: Is there a way to do this programmatically without repeating yourself? 

#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")

"""
Explanation: 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.
End of explanation
"""


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")

"""
Explanation: 6) What section talks about motorcycles the most? ##
Tip: You'll be using facets
End of explanation
"""


#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")

"""
Explanation: 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.
End of explanation
"""


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))


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


print("The author with the highest number of reviews to his credit is", max_occ)

"""
Explanation: 8) Critics with Highest Reviews
Out of the last 40 movie reviews from the NYT, which critic has written the most reviews?
End of explanation
"""


