Twitter Bot With Tweepy - Python
Solution 1:
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). 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( ... )
. take note that update_status()
is a method of the api
object. also, please do note that your id
is uninstantiated or has no initial value.
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.
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. (Sad I know :/ ). But that's where data / tweet preprocessing comes in.
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.
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). the query value is basically like the 'search' bar of 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 = 100for 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
Post a Comment for "Twitter Bot With Tweepy - Python"