2017-03-05 53 views
0

我试图做一个python脚本,可以删除/撤消我最喜欢的所有我的twitter的最爱。我看过MATHEW INKSON's post来完成这项工作。我不需要删除我的推文,只想清除收藏夹。除此之外,该脚本已近两年之久,并且与最新的python不兼容。所以我编辑的一点与Python 3.6.0和我的脚本运行它看起来像这样:如何使用python删除所有的收藏夹?无法删除,获取错误。需要解决方案

import tweepy 
from datetime import datetime, timedelta 

test_mode = False 
verbose = False 
delete_favs = True 
days_to_keep = 7 

consumer_key = 'my consumer key' 
consumer_secret = 'my consumer secret' 
access_token = 'my access token' 
access_token_secret = 'my access token secret' 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 

cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep) 

if delete_favs: 
    print ("Retrieving favorited tweets") 
    favorites = tweepy.Cursor(api.favorites).items() 
    unfav_count = 0 
    kept_count = 0 

    for tweet in favorites: 
     if tweet.created_at < cutoff_date: 
      if verbose: 
       print ("Unfavoring %d: [%s] %s % (tweet.created_at,tweet.text)") 
       if not test_mode: 
        api.destroy_favorite(tweet.id) 
        unfav_count += 1 
       else: 
        kept_count += 1 
       print ("Unfavored %d tweets, ignored %d" % (unfav_count, kept_count)) 

但是,通过每次在我的Windows命令运行该脚本,我得到这个以下错误:

Traceback (most recent call last): 

File "C:\Users\xyz\Desktop\New folder\Unfavorite.py", line 25, in <module> 

for tweet in favorites: 
File "C:\Users\xyz\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 49, in __next__ 

File "C:\Users\xyz\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 197, in next 

File "C:\Users\xyz\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 108, in next 

File "C:\Users\xyz\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 250, in _call 

File "C:\Users\xyz\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 234, in execute 

tweepy.error.TweepError: Twitter error response: status code = 429 

我使用Python 3.6,我的应用程序的权限都是正确的。一切都很好,我的twitter应用程序。我猜想我的脚本有问题。 请有人帮助修复我的代码。我也看过其他一些剧本。那些没有解决。建议将不胜感激。 感谢提前。

+0

我投票结束这个问题作为题外话,因为垃圾邮件。 –

+1

请不要破坏你的帖子。 –

+0

@AlisterBulman只是破坏后,如果垃圾邮件,然后标记在它不会投票结束,任何方式,这不是垃圾邮件 –

回答

2

根据Twitter response codes,当由于应用程序的资源已用尽速率限制而无法提供请求时,将返回代码429。这意味着你的应用程序提出了太多的请求,你必须看看API Rate limits.

+1

我通常使用time.sleep()当我的API做出太多的请求,把一些时间差距在两个请求之间......你也可以尝试把睡眠放在循环中。否则,您可以尝试在一定限制后获得新的API会话。 – pratibha

+0

在文档中,他们提到:“API的速率限制主要基于每个用户的访问令牌(或者更准确地说,每个用户访问令牌),如果每个速率限制窗口允许15个请求,则允许15个请求每个访问令牌每个窗口“。 – pratibha

+0

请提出答案,如果解决您的问题, – pratibha