Created
June 29, 2018 19:49
-
-
Save pypt/159036d5540c7a50e117811074641df5 to your computer and use it in GitHub Desktop.
Search for NYTLabels-themed stories
This file contains hidden or 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 mediacloud | |
def nyt_labels_tag_id_for_tag(mc, tag_name): | |
"""Return a tags_id for a NYTLabels tag name to be used to search for stories tagged with said tag.""" | |
# Every NYTLabels-"themed" story is internally tagged with a name of the theme | |
tags = mc.tagList(name_like=tag_name) | |
for tag in tags: | |
# tags/list search is fuzzy so we have to confirm that we have found the right tag | |
if tag['tag'] == tag_name: | |
# Every NYTLabels tag belongs in a "nyt_labels" tag set | |
if tag['tag_set_name'] == 'nyt_labels': | |
return tag['tags_id'] | |
return None | |
def nyt_labels_stories_for_theme(mc, theme_name): | |
"""Return a list of stories for a NYTLabels theme.""" | |
# First off, get a tag ID for the NYTLabels tag that corresponds to the theme name | |
tags_id = nyt_labels_tag_id_for_tag(mc=mc, tag_name=theme_name) | |
assert tags_id is not None | |
# Search for stories using the found tag's ID | |
return mc.storyList(solr_query='tags_id_stories:%d' % tags_id) | |
def nyt_labels_sample(): | |
mc = mediacloud.api.MediaCloud(auth_token='YOUR_MEDIACLOUD_API_KEY_HERE') | |
stories = nyt_labels_stories_for_theme(mc=mc, theme_name='renting and leasing') | |
print(stories) | |
if __name__ == '__main__': | |
nyt_labels_sample() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment