媒体云API客户端库
mediacloud的Python项目详细描述
mediacloud python api客户端
这是一个用于访问MediaCloud API v2的python客户机。 我们支持python版本2.7和3.6。
用法
第一个sign up for an API key。然后
pip install mediacloud
查看CHANGELOG.md
以获取详细的更改历史记录。
示例
了解去年美国顶级在线新闻网站中提到“津巴布韦”的报道有多少:
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 found
从纽约时报获取有关2018年某个主题的2000篇报道,并将输出转储到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.publish_date_query(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))
在2013年提到“津巴布韦”和“总统”的美国顶级在线新闻网站的报道中找到最常用的词汇:
importmediacloud.api,datetimemc=mediacloud.api.MediaCloud('MY_API_KEY')words=mc.wordCount('zimbabwe AND president AND tags_id_media:58722749',mc.publish_date_query(datetime.date(2013,1,1),datetime.date(2014,1,1)))print(words[0])# prints the most common word
按ID查找有关某个特定故事的所有详细信息:
importmediacloud.apimc=mediacloud.api.MediaCloud('MY_API_KEY')story=mc.story(169440976)print(story['url'])# prints the url the story came from
将前100篇文章从一天中保存到数据库:
importmediacloud.api,datetimemc=mediacloud.api.MediaCloud('MY_API_KEY')db=mediacloud.storage.MongoStoryDatabase('one_day')stories=mc.storyList('*',mc.publish_date_query(datetime.date(2014,01,01),datetime.date(2014,01,02)),last_processed_stories_id=0,rows=100)[db.addStory(s)forsinstories]print(db.storyCount())
查看mediacloud/test/
模块中的测试以获取更详细的示例。
开发
如果您对向该模块添加代码感兴趣,请首先克隆the GitHub repository。
测试
您需要创建一个MC_API_KEY
环境变量并将其设置为您的api密钥(我们使用^{make test
。我们运行持续集成(通过Travis),
所以每次推送都会运行整个测试套件(我们也每晚在prs上这样做)。
分发新版本
如果需要,请设置twin's keyring integration以避免键入您的pypi 密码一遍又一遍。
- 运行
make test
以确保所有测试通过 - 更新
mediacloud/__init__.py
中的版本号 - 在changelog.md中简要记录更改内容
- 运行
make build-release
创建安装包 - 运行
make release-test
将其上载到pypi的测试平台 - 运行
make release
将其上载到pypi