0

使用Twitter API,我通过下面编写的代码获得我的 rate_limiting 信息:

def limit():
twitter = Twitter(auth=OAuth('....'))
g = twitter.application.rate_limit_status()
print g

这很棒,但我得到了这样的大混乱:

{u'rate_limit_context': {u'access_token': u'....'}, u'resources': {u'account': {u'/account/verify_credentials': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/account/settings': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'blocks': {u'/blocks/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/blocks/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'users': {u'/users/contributors': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/lookup': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/search': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/suggestions/:slug/members': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/suggestions/:slug': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/show/:id': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/suggestions': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/profile_banner': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/contributees': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'friends': {u'/friends/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friends/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'saved_searches': {u'/saved_searches/show/:id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/saved_searches/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/saved_searches/destroy/:id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'lists': {u'/lists/subscriptions': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/subscribers/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/statuses': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/subscribers': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/members/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/memberships': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/members': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/ownerships': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'search': {u'/search/tweets': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}}, u'application': {u'/application/rate_limit_status': {u'reset': 1371673401, u'limit': 180, u'remaining': 178}}, u'trends': {u'/trends/available': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/trends/closest': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/trends/place': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'followers': {u'/followers/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/followers/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'favorites': {u'/favorites/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'friendships': {u'/friendships/outgoing': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/show': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/friendships/incoming': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/no_retweets/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/lookup': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'geo': {u'/geo/similar_places': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/id/:place_id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/reverse_geocode': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/search': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'direct_messages': {u'/direct_messages/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages/sent': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages/sent_and_received': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'statuses': {u'/statuses/retweets_of_me': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/retweeters/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/mentions_timeline': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/user_timeline': {u'reset': 1371672792, u'limit': 180, u'remaining': 177}, u'/statuses/oembed': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/statuses/show/:id': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/statuses/home_timeline': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/retweets/:id': {u'reset': 1371672792, u'limit': 15, u'remaining': 0}}, u'help': {u'/help/tos': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/configuration': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/privacy': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/languages': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}}}

不过,我只需要其中的一部分。我只需要知道这方面的剩余限制:

u'/statuses/retweets/:id': {u'reset': 1371672792, u'limit': 15, u'remaining': 0}

我怎样才能得到那个号码?

4

3 回答 3

1

在不知道 Twitter API 的详细信息的情况下,该方法似乎rate_limit_status()返回了dictionary一个dictionaries. 因此,您应该能够通过查找正确的键来获取所需的数据:

>>> g = eval("{u'rate_limit_context': {u'access_token': u'....'}, u'resources': ... ")
>>> g[u'resources'][u'statuses'][u'/statuses/retweets/:id']
{u'limit': 15, u'remaining': 0, u'reset': 1371672792}
>>> g[u'resources'][u'statuses'][u'/statuses/retweets/:id'][u'remaining']
0
于 2013-06-19T20:30:25.910 回答
0

在过去的 72 小时里,这个问题一直困扰着我。首先,我们应该在这里澄清一下——您使用的是 Tweepy 库,对吗?我使用 PyPI 列表顶部的 twitter 包编写了一些代码,但似乎他们没有从 API 1.0 升级到 API 1.1,流行度排名第二的 python-twitter 似乎也没有准备好,尽管较旧的 API 在一个多月前就已停用。

整个速率限制方案已从 API 1.0 更改为 API 1.1 - 以前您每小时有 350 个通用令牌,并且系统每小时重置。先前基于源 IP 无身份验证获得 150 个 API 令牌的搜索能力已不复存在。现在时间增量为 15 分钟,您将获得 15 个帖子和 180 个帖子。

https://dev.twitter.com/docs/rate-limiting/1.1

新的 API 为每个 API 访问点的级别提供了粒度 - 而不是一个包含 350 个令牌的存储桶,看起来每个可以想象的 post 类型操作都被类型限制为每季度 15 个,get 类型操作被限制为每季度 180 个一刻钟。看起来 Twitter 将更好地处理 API 使用统计。就我的目的而言,容量似乎几乎翻了一番,从 350 次呼叫/小时到 (4x180 = 720) 次呼叫/小时。谢谢你,推特。

另一个响应者描述的访问方法很丑 - 我们必须深入挖掘四个级别的字典,以便在概念上是端点/计数元组的字典?如果这是完整的结构,那就这样吧,但确实需要一种简单的方法来命名所需的端点并取回一个整数。那将是可读的,这种混乱的字典引用不是。

于 2013-08-03T06:47:44.290 回答
0

您可以在此处查看您在 Twitter 文档中描述的返回对象的完整 json 结构示例

正如 andersschuller 在这里已经提到的,在 Python 中,您可以通过调用它的键来获得您想要的对象,g[u'resources'][u'statuses'][u'/statuses/retweets/:id']

于 2015-05-27T07:57:24.150 回答