Created
December 13, 2015 22:01
-
-
Save davidcomfort/21c7486490736bbe3b20 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
def parse_articles(articles): | |
''' | |
This function takes in a response to the NYT api and parses | |
the articles into a list of dictionaries | |
''' | |
news = [] | |
for i in articles['response']['docs']: | |
dic = {} | |
dic['id'] = i['_id'] | |
if i['abstract'] is not None: | |
dic['abstract'] = i['abstract'].encode("utf8") | |
dic['headline'] = i['headline']['main'].encode("utf8") | |
dic['desk'] = i['news_desk'] | |
dic['date'] = i['pub_date'][0:10] # cutting time of day. | |
dic['section'] = i['section_name'] | |
if i['snippet'] is not None: | |
dic['snippet'] = i['snippet'].encode("utf8") | |
dic['source'] = i['source'] | |
dic['type'] = i['type_of_material'] | |
dic['url'] = i['web_url'] | |
dic['word_count'] = i['word_count'] | |
# locations | |
locations = [] | |
for x in range(0,len(i['keywords'])): | |
if 'glocations' in i['keywords'][x]['name']: | |
locations.append(i['keywords'][x]['value']) | |
dic['locations'] = locations | |
# subject | |
subjects = [] | |
for x in range(0,len(i['keywords'])): | |
if 'subject' in i['keywords'][x]['name']: | |
subjects.append(i['keywords'][x]['value']) | |
dic['subjects'] = subjects | |
news.append(dic) | |
return(news) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment