Package: mediacloud-api-legacy
Legacy MediaCloud Python API ClientThis is a python client for accessing theMediaCloud API v2.
We support Python versions 2.7 and 3.6.Related work:See ourMediaCloud-API-Tutorial_Notebooksrepo formanymore examples of useIf you happen to work in R, check out themediacloudrlibrary for R(from Dix Jan).This package exists to mantain access to our legacy search interface while we build out tooling and reliability for our new system. API keys for the new search tools will not work for this API.UsageFirstsign up for an API key.  Thenpip install mediacloud-api-legacyCheckCHANGELOG.mdfor a detailed history of changes.ExamplesFind out how many stories in the top US online news sites mentioned "Zimbabwe" in the last year:importmediacloud.apimc=mediacloud.api.MediaCloud('MY_API_KEY')res=mc.storyCount('zimbabwe AND president AND tags_id_media:58722749','publish_date:[NOW-1YEAR TO NOW]')print(res['count'])# prints the number of stories foundGet 2000 stories from the NYT about a topic in 2018 and dump the output to json:importmediacloud.api,json,datetimemc=mediacloud.api.MediaCloud('MY_API_KEY')fetch_size=500stories=[]last_processed_stories_id=0whilelen(stories)<2000:fetched_stories=mc.storyList('trump AND "north korea" AND media_id:1',solr_filter=mc.dates_as_query_clause(datetime.date(2018,1,1),datetime.date(2019,1,1)),last_processed_stories_id=last_processed_stories_id,rows=fetch_size)stories.extend(fetched_stories)iflen(fetched_stories)<fetch_size:breaklast_processed_stories_id=stories[-1]['processed_stories_id']print(json.dumps(stories))Find the most commonly used words in stories from the US top online news sites that mentioned "Zimbabwe" and "president" in 2013:importmediacloud.api,datetimemc=mediacloud.api.MediaCloud('MY_API_KEY')words=mc.wordCount('zimbabwe AND president AND tags_id_media:58722749',mc.dates_as_query_clause(datetime.date(2013,1,1),datetime.date(2014,1,1)))print(words[0])# prints the most common wordTo find out all the details about one particular story by id:importmediacloud.apimc=mediacloud.api.MediaCloud('MY_API_KEY')story=mc.story(169440976)print(story['url'])# prints the url the story came fromTo save the first 100 stories from one day to a database:importmediacloud.api,datetimemc=mediacloud.api.MediaCloud('MY_API_KEY')db=mediacloud.storage.MongoStoryDatabase('one_day')stories=mc.storyList('*',mc.dates_as_query_clause(datetime.date(2014,1,1),datetime.date(2014,1,2)),last_processed_stories_id=0,rows=100)[db.addStory(s)forsinstories]print(db.storyCount())Take a look at the test in themediacloud/test/module for more detailed examples.DevelopmentIf you are interested in adding code to this module, first clonethe GitHub repository.TestingYou need to create anMC_API_KEYenvvar and set it to your API key (we usepython-dotenv).
Then runmake test. We run continuous integration (viaTravis),
so every push runs the whole test suite (we also do this nightly and on PRs).Distributing a New VersionIf you want to, setuptwin's keyring integrationto avoid typing your PyPI
password over and over.Runmake testto make sure all the test passUpdate the version number inmediacloud/__init__.pyMake a brief note in the CHANGELOG.md about what changesRunmake build-releaseto create an install packageRunmake release-testto upload it to PyPI's test platformRunmake releaseto upload it to PyPI