我正在为我的LED tweepy推特机器人进行画龙点睛的工作。我已经用不同的帐户对其进行了测试,并且发生了一些奇怪的事情。使用我的个人帐户,我发了推文,但没有任何作用。我的过滤器没有检测到主题标签,而只是忽略了所有内容。我的朋友们已经能够使用它,所以我决定登录一个单独的Twitter帐户。我可以在那发布推文,而我的机器人可以看到并识别它。
我真的不知道是什么原因造成的。我的漫游器是否有问题,还是帐户有问题?
import tweepy
import requests
import json
consumer_key = 'nein'
consumer_secret = 'das'
access_token = 'ist'
access_token_secret = 'böse'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
user = api.me()
print(user.name)
counter = 0
class LEDStreamListener(tweepy.StreamListener):
def on_data(self, raw_data):
with open("tweets.json", "w") as write_file:
write_file.write(raw_data)
data = json.loads(raw_data)
variable_checker(data)
def on_error(self, status_code):
if status_code == 420:
print('Stream disconnect, because of rate limit error')
return False
else:
print('Unknown Error ' + status_code)
return False
def retweet_tweet(tweet_id):
api.retweet(tweet_id)
api.create_favorite(tweet_id)
def tag_checker(tag_list):
for i in range(0, len(tag_list)):
iterated_tag = tag_list[i]['text']
if iterated_tag == 'HUNTER_LED_ON':
return iterated_tag
elif iterated_tag == 'HUNTER_LED_OFF':
return iterated_tag
elif iterated_tag == 'led_test':
return iterated_tag
return ' '
def variable_checker(json_file):
if 'delete' in json_file:
# If the tweet was deleted do nothing
print('Delete')
else:
usr = json_file['user']['screen_name']
tweet_id = json_file['id_str']
print(tweet_id)
text = json_file['text']
tag_list = json_file['entities']['hashtags']
tag = tag_checker(tag_list)
data_check(usr, tweet_id, tag, text)
def data_check(twitter_user, tweet, tag, text):
if tag == 'HUNTER_LED_OFF' and not text.startswith('RT'):
requests.get('http://192.168.1.175/off')
retweet_tweet(tweet)
api.update_status('I turned the led off for you', tweet)
print('off')
return
elif tag == 'HUNTER_LED_ON' and not text.startswith('RT'):
requests.get('http://192.168.1.175/on')
retweet_tweet(tweet)
api.update_status('I turned the led on for you', tweet)
print('on')
return
elif tag == 'led_test' and not text.startswith('RT'):
retweet_tweet(tweet)
reply = 'Nice test bro *highfives* keep up the good work'
api.update_status('@%s %s' % (twitter_user, reply), in_reply_to_status_id=tweet)
print('tested')
return
elif twitter_user == 'realDonaldTrump':
api.create_favorite(tweet)
requests.get('http://192.168.1.175/trump')
print('Make America Great Again!')
return
else:
return
ledStreamListener = LEDStreamListener()
ledStream = tweepy.Stream(auth=api.auth, listener=ledStreamListener)
ledStream.filter(track=['#HUNTER_LED_OFF', '#HUNTER_LED_ON', '#led_test'])
答案 0 :(得分:0)
这可能与Twitter有关,仅使大约1%的推文可流式传输给大多数用户。 Twitter确实提供了一个“ Firehose”帐户,该帐户可以为您提供所有功能,但价格相当昂贵。
您可以每隔几秒钟轮询一次用户时间轴,而不必使用流式API。
new_tweets = api.user_timeline(user_id = user_id,count= 1)