Last active
November 1, 2015 03:13
-
-
Save ahurriyetoglu/d274c22f6a4e10dd9152 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 as r | |
import json | |
import datetime | |
# Docs: http://developer.nytimes.com/docs | |
# Try at: http://developer.nytimes.com/io-docs | |
srch_api_key = "Your key <check docs>" | |
keyphrase = 'anything_you_want' | |
srch_api_result = r.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q="+keyphrase+"&api-key="+srch_api_key) | |
hits = int(srch_api_result.json()["response"]["meta"]["hits"]) # first get related articles. | |
article_list = [] # The list of the articles | |
for p in range(int(hits/10)+1): | |
srch_api_result = r.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=rohingya&api-key="+srch_api_key+"&page="+str(p)) | |
article_list += srch_api_result.json()["response"]["docs"] | |
len(article_list) | |
# An article contains the following fields: | |
# 'abstract', 'print_page', 'subsection_name', '_id', 'source', 'blog', 'byline', 'news_desk', 'headline', 'snippet', | |
# 'keywords', 'multimedia', 'document_type', 'web_url', 'type_of_material', 'word_count', 'pub_date', 'lead_paragraph', | |
# 'section_name', 'slideshow_credits' | |
article_dict = {} | |
for a in article_list: | |
article_dict[a["_id"]] = a # duplicate articles will be represented just once in a dictionary which has the Article ID as key. | |
cmmnt_api_key = "Comment API key. It is different from the Search API key!" | |
cmmnts_dict = {} | |
for a_id, a in article_dict.items(): | |
cmmnt_api_result = r.get("http://api.nytimes.com/svc/community/v3/user-content/url.json?url="+a["web_url"]+"&api-key="+cmmnt_api_key) | |
a["comments"] = cmmnt_api_result.json()["results"]["comments"] | |
article_dict[a_id] = a # Each article ID will be associated with the comments of that article. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment