addthis分析api的python包装器
addthis的Python项目详细描述
用于AddThis Analytics API的python包装器。
要求
- python 2.6、2.7或3.2+
- python-requests库
安装
从pypi安装:
pip install addthis
用法
from addthis import Addthis # create an AddThis instance using userid and password from your AddThis account and optionally provide a pubid addthis = Addthis(userid="YOUR_USER_ID", password="YOUR_PASSWORD", pubid="YOUR_PUB_ID") # get the number of shares for the last day print addthis.shares.day() # get the number of shares by day for the last week print addthis.shares.day(period="week")
您可以在http://support.addthis.com/customer/portal/articles/381264-addthis-analytics-api
再举几个例子
过去一周,我的内容每天在twitter上共享多少次?
>>> addthis.shares.day(period="week", service="twitter")
pub id=“我的pub\u id”的最大共享url是什么?
>>> addthis.shares.url(pubid="MY_PUB_ID")
这个月有多少用户分享了我的内容,按他们的兴趣分类?
>>> addthis.sharers.interest(period="month")
本周哪些共享服务向我的网站发送了最多的点击?
>>> addthis.clicks.service(period="week")
例外情况
添加此验证错误
addthis对象需要用两个参数来调用-“metric”和“dimension”:
addthis.<metric>.<dimension>()
例如:
>>> addthis.shares.day() # "shares" is a metric and "day" is a dimension
如果它获得其他数量的参数(例如addthis.shares()或addthis.shares.day.week()),它将引发一个addThisValidationError。
from addthis import Addthis, AddthisValidationError addthis = Addthis(userid="YOUR_USER_ID", password="YOUR_PASSWORD", pubid="YOUR_PUB_ID") try: addthis.shares() except AddthisValidationError as e: print e # "Incorrect number of parameters are given. Expected 2 but got 1."
添加此错误
addthisserror在addthis服务返回http状态代码不是200的响应时引发。异常对象有4个属性:
- 状态代码:来自HTTP响应的代码
- code,message,attachment:来自AddThis响应体的错误属性(有关详细信息,请参阅AddThis Analytics API documentation中的“错误”部分)
from addthis import Addthis, AddthisError addthis = Addthis(userid="INCORRECT_USER_ID", password="INCORRECT_PASSWORD", pubid="INCORRECT_PUB_ID") try: addthis.shares.day() except AddthisError as e: print e # "401 Error (code = '80', message='authentication failed', attachment='{u'nonce': None, u'realm': u'AddThis', u'opaque': None})'." print e.status_code # 401 print e.code # 80 print e.message # "authentication failed" print e.attachment # {u'nonce': None, u'realm': u'AddThis', u'opaque': None}