简体   繁体   English

Tweepy的Twitter Bot-Python

[英]Twitter Bot with Tweepy - Python

I have been working on a Twitter Bot which replies to people with "im" in their message with "Hi ___, I'm Dad!". 我一直在开发一个Twitter Bot,该机器人在邮件中用“嗨___,我是爸爸!”回复“ im”的人。

I used to have it working before, but then my computer died and lost all of it's files. 我以前曾经让它工作,但是后来我的电脑死了,丢失了所有文件。

I wrote the original bot a couple years ago, and I haven't looked at Tweepy in a while. 几年前,我写了原始的bot,但有一段时间没看过Tweepy了。

I was pretty sure I had it 100% figured out - and no errors popped up, but the bot isn't working and I don't know why. 我很确定自己100%都知道了-并没有弹出错误,但该机器人无法正常工作,我也不知道为什么。

I can log in just fine, but something is wrong with the actual replying part. 我可以正常登录,但是实际的回复部分出了点问题。

Can someone help me out? 有人可以帮我吗?

import tweepy as tt
import time


#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'

#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_results = api.search("r'\bi\'?m\s+(.*)',re.IGNORECASE,text", count=100)

user = api.me()
print(user.name)


#reply
for tweet in search_results:

# answer to the hashtag tweet
    if len(reply) > 0:
        c=message.content
        c=c.replace("im ","")
        answer="@"+user+" Hi " + c + ", I'm Dad!"
        print ("Reply:",answer)
# tweet and pause not to stress twitter
        twitter.update_status(status=answer,in_reply_to_status_id=id)
        time.sleep(300) #every 5 minutes

if you wish to continue using the r(egular)e(xpression) module, you might want to import re but as far as I know, and also based on answers of others here on stackoverflow, Twitter doesn't support searching of tweets using RE (as of the moment). 如果您希望继续使用r(egular)e(xpression)模块,则可能要import re但据我所知,并且根据其他有关stackoverflow的回答,Twitter不支持使用来搜索推文RE(目前)。 I can attest to this, since I'm gathering tweets weekly, and re searches fail. 我可以证明这一点,因为我每周都收集推文,而重新搜索失败。

try using api.update_status( ... ) instead of twitter.update_status( ... ) . 尝试使用api.update_status( ... )代替twitter.update_status( ... ) take note that update_status() is a method of the api object. 请注意, update_status()api对象的方法。 also, please do note that your id is uninstantiated or has no initial value. 另外,请注意,您的id未实例化或没有初始值。

Here's a snippet from my twitter gathering script for further context, in my implementation, I save rows of tweets in a csv using csvwriter. 这是我的Twitter收集脚本的一个片段,用于进一步的上下文,在我的实现中,我使用csvwriter将几行tweet保存在一个csv中。

for tweet in tweepy.Cursor(api.search, q='twitter').items():  
    csvWriter.writerow([tweet.text.encode('utf8'),
                        tweet.user.screen_name, tweet.created_at,
                        tweet.retweet_count, tweet.favorite_count,
                        tweet.user.location.encode('utf8')], tweet.user.id)

Like you I also tried using RE a few years back, (and just recently), I could confirm that RE's not yet supported in twitter searches/queries. 像您一样,几年前(以及最近)我也尝试使用RE,我可以确认Twitter搜索/查询尚不支持RE。 (Sad I know :/ ). (很遗憾,我知道:/)。 But that's where data / tweet preprocessing comes in. 但这就是数据/ tweet预处理的来源。

Another point i'd like to make is, you can't retrieve the total number of replies a tweet gets (i'm assuming that like me you're using a standard api (not premium or enterprise).. see this link for context about the reply_count tweet feature. 我想提出的另一点是,你不能检索回复鸣叫得到的总数(我假设你像我一样正在使用标准的API(没有溢价或企业)..看到这个链接了有关reply_count tweet功能的上下文。

for your case, I would suggest the following code to 'search' your intended tweets, I used tweepy's cursor to do an api.search, followed by the q(uery). 对于您的情况,我建议使用以下代码“搜索”您想要的推文,我使用tweepy的光标执行api.search,然后输入q(uery)。 the query value is basically like the 'search' bar of twitter. 查询值基本上类似于Twitter的“搜索”栏。 although my solution isn't actually your solution, you can pick up from my code if you like. 尽管我的解决方案实际上不是您的解决方案,但是您可以根据需要从我的代码中进行学习。

import tweepy as tt
import time
import sys
reload(sys)
sys.setdefaultencoding('utf8')


#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'

#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_query = "hi I'm Dad"
user = api.me()
print(user.name)

max_tweets = 100

for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
    c=tweet.text.encode('utf8')
    c=c.replace("im ","")
    answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
    print ("Reply:",answer)
    api.update_status(status=answer,in_reply_to_status_id=tweet.id)
    time.sleep(300) #every 5 minutes

/ogs / ogs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM