Created
April 8, 2015 01:36
-
-
Save katsully/8ce5bb1efb3ab0e6f399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def searchNYTimes(term,key,page=0): | |
r = requests.get('http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=headline:(' + term + ')&page='+str(page)+'&api-key=' + str(key)) | |
if r.ok == False: | |
return False | |
data = r.json() | |
docs = data['response']['docs'] | |
for doc in docs: | |
headline = doc['headline']['main'] | |
if headline.rsplit(' ')[0] == term: | |
return headline | |
def generate(term): | |
file_key = open("api_key.txt") | |
key = file_key.readline() | |
file_key.close() | |
for i in range(20): | |
headline = searchNYTimes(term,key) | |
if headline is False: | |
break | |
if headline is None: | |
for i in range(15): | |
headline = searchNYTimes(term,key,i+1) | |
if headline is not None: | |
break | |
if headline is None or headline is False: | |
break | |
print headline.encode('utf-8') | |
term = headline.rsplit(' ')[-1] | |
if term.find('-') !=-1: | |
term = term.rsplit('-')[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment