Skip to content

Instantly share code, notes, and snippets.

@katsully
Created April 8, 2015 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsully/8ce5bb1efb3ab0e6f399 to your computer and use it in GitHub Desktop.
Save katsully/8ce5bb1efb3ab0e6f399 to your computer and use it in GitHub Desktop.
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