错误处理在Tweepy中

时间:2015-08-20 20:30:26

标签: python tweepy

我正在学习python,几周前就已经开始了。我曾尝试编写一个代码来检查在流API中使用特定主题标签的推文,然后回复推文,以防之前的推文尚未发布到句柄中。在运行代码时,我试图避免超出速率限制,以免出现任何错误。但Twitter存在重复状态的问题。我希望代码能够继续运行而不是停止遇到问题。请帮忙。以下是代码:

import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
import json
import time

consumer_key = 
consumer_secret = 
access_token = 
access_secret = 

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

def check(status):
    datafile = file('C:\Users\User\Desktop\Growth Handles.txt', 'r')
    found = False
    for line in datafile:
        if status.user.screen_name in line:
            found = True
            break
    return found


class MyListener(StreamListener):


    def on_status(self, status):
        f=status.user.screen_name
        if check(status) :
            pass
        else:
            Append=open('Growth Handles.txt' , 'a' )
            Append.write(f + "\n")
            Append.close()
            Reply='@' + f + ' Check out Tomorrowland 2014 Setlist . http://.... '
            api = tweepy.API(auth)
            api.update_status(status=Reply)
            time.sleep(45)
        return True

    def on_error(self, status):
        print(status)
        return True



twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['#musiclovers'])

0 个答案:

没有答案