This is an example of how my code in tweepy looks like:
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
random = random.randint(1,1000)
for tweet in tweepy.Cursor(api.search, q='twitter', lang='en', result_type='recent').items():
if not (tweet.retweeted) and 'RT #' not in (tweet.text):
api.update_status('#' + tweet.user.screen_name + ' ' + str(random) + ': test', in_reply_to_status_id = tweet.id_str)
print('Replied to the tweet!')
sleep (900)
The code works but for some reason, after a while of running the code, my tweets go missing from the search. Before it goes missing from the search, it goes missing from the tweet I replied to. I don't really know why this is happening.
The Twitter search function is optimized to show recent tweets (and probably especially so when you have the arg result_type='recent'). The tweet still exists but it is not showing up in the search because it is no longer recent enough. If you go into the twitter browser I am sure the tweets and replies are still there (navigate to the user's timeline to find it easiest). Or, try removing the result_type='recent'.
Hope this helps.
Tweets go missing from search when you are either tweeting too much or someone reports your tweets too often. Twitter is just based off of algorithms that detect if you are tweeting the same thing over and over (spamming), following / unfollowing too quickly or retweeting too much. Give it a rest it should clear after 72 hours max. I believe the term is called “shadow banned”.
Related
I'm writing a program to scrape tweets between two specific dates from a user, while making sure that they are not retweets or replies. I am using snscrape and tweepy.
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('from:' + lines[x] + ' since:' + startDate + ' until:' + endDate).get_items()):
if tweet.retweetedTweet is None and tweet.inReplyToTweetId is None and tweet.inReplyToUser is None:
This is what I have for the check, however, if the tweet is in reply to a tweet that has been deleted, then the tweet is no longer considered a reply and the check passes as None. Is there a way around this? I'm looking at pulling tweets from large companies like Tesco and Sainsburys and manually sorting through their tweets by hand will be tedious and want to find a way to fix this within the code.
An example of this is this tweet, as the code passes the check for inReplyToTweetId is None
Any help would be greatly appreciated, thank you.
I actually solved this a lot quicker than I thought I would. Turns out, in the tweet object, the mentionedUsers array is empty for these specific tweets, so I added the following if statement which solved the problem:
if not ('#' in tweet.content and not tweet.mentionedUsers):
This just checks whether the user has a mention (# symbol) in the actual text of the tweet and also whether the mentionedUsers array is empty to discard it as being a reply.
I want to get popular and recent tweets for different Hash tags, with Tweepy. The solution I have right now is shown below. But this method does a separate call for each hashtag, which is not good in the presence of rate limitations. Is it possible to do this in one call? If so, how?
for ht in hash_tags:
tweets = tweepy.Cursor(api.search, ht + " -filter:retweets",
result_type='mixed', since=date_since,
tweet_mode='extended').items(num_of_tweets)
add_tweets(tweets)
Rather than running a for loop in which you search for one hashtag at a time, you can run a search which contains many of the hashtags you need using the OR operator from Twitter's standard search operators. This is a broader search, and you'll get a mixed bag of results for each hashtag, but it cuts down on the number of requests you make overall.
So add all of your hashtags to a string, and pass this through as your query.
#just an example, not the nicest!
query_string = ""
for i in hash_tags:
if i == hash_tags[-1]:
query_string+=str(i)
else:
query_string+=str(i) + " OR "
tweets = tweepy.Cursor(api.search, q=query_string + " -filter:retweets",
result_type='mixed', since=date_since,
tweet_mode='extended').items(num_of_tweets)
add_tweets(tweets)
This means your search will be something like #coolstuff OR #reallycoolstuff OR #evencoolerstuff. It may be worth increasing the number of items returned when doing this, as the search will be so much more broad.
Keep in mind that there are limits to the size of query you can search with, so you may need to break this down into smaller queries if you've got a lot of hashtags. This may also help you get better results (e.g a more popular hashtag in your query string taking up a lot of your results, so do a separate search for less popular hashtags separately; how you'd measure that though, would be up to you!)
Hope this helps you get started.
I need to create a program with tweepy for a homework. Im not a programmer.
I would like the program to search for menacing tweets toward for example Justin Trudeau. And then send me an email when it spot one.
To determine if a tweet is menacing or not, the tweet would have to contain, for example, the keyword "trudeau" and one of the following "bomb" or "kill". Once i get this to work, I'll refine the keyword filter.
So i have tried this:
api = tweepy.API(auth)
searchterm1 = "trudeau"
searchterm2 = "bomb" or "kill"
search = tweepy.Cursor(api.search,
q= searchterm1 and searchterm2
lang="en",
result_type="recent").items(10)
for item in search:
print (item.text)
But it only shows me tweets with the last keyword, not one of them like it should with the or function, no?
I want to show only tweets that contain the word "trudeau" and one of the keyword in searchterm2
Thanks for your help
You're gonna need this, where you'll find that your query string should be:
q = 'trudeau bomb OR kill'
From your example you can get to that query string like this:
searchterm1 = 'trudeau'
searchterm2 = 'bomb OR kill'
q = ' '.join([searchterm1, searchterm2])
I'm currently trying to make a twitter bot that is supposed to reply to one tweet, which it filters using regex, and reply to it.
The relevant code looks as follows:
questionRegex = re.compile(regex here)
def searchWeatherRequest(weatherReport) :
for tweet in tweepy.Cursor(api.search,
q=questionRegex,
lang="en",
since=today).items(1):
try:
tweetId = tweet.user.id
username = tweet.user.screen_name
print ('\Tweet by: #' + username)
tweet.retweet()
api.update_status("#" + username + "Today's weather" + weatherReport)
print (tweet.text)
except tweepy.TweepError as e:
print (e.reason)
except StopIteration:
break
time.sleep(3600)
But whenever I run the code, I receive the message "no tweets found" (even after posting a tweet that would match the regex, so I know that it's not just because there are simply no tweets that would match it).
I also tried filtering the tweets in steps (first, I filter tweets using just one word, and then I filter those tweets using regex) but this did not work either.
Does anyone know what I'm doing wrong. I read multiple articles and questions about this but none of the solutions seemed to work.
I read one question you couldn't filter tweets using regex but other answers suggested otherwise. Is it true that you simply can't use regex, or am I encountering a simple coding error?
Unfortunately regexes won't work here. This is because the q= is expecting a string to come through and thus won't interperet the regex you're passing, instead I believe it'd either just error or take the re.compile(regex here) as a string itself, which of course, isn't likely to turn up many - if any - results.
So it looks like your current method isn't going to work. A workaround could be using Twitter's standard operators. You could build up strings using filter operations that when passed to the Cursor, essentially act the same way as your regex did. Keep in mind though that there are character limits and overly complicated queries may also be rejected. You can find details on that in the search tweets docs.
Another option would be to have a fairly general search, and then use your regex from there to filter the results. The answerer to a fairly similar question as yours shares some articles here.
Hope that helps and gets you on the right path.
I'm trying to write my first twitter bot, and what I specifically want it to do is to reply when a specific user tweets something. Instead of searching for every user that mentions the word 'taco' I want to only search for when a small list of people mention 'taco'. For example, my bot only looks for tweets from Bob and Sue that mention 'taco', but not tweets from Fred. I'm not finding what I need.
From the Tweepy documentation, you should probably use the method API.user_timeline to read a specific user tweets.
API.user_timeline([user_id/screen_name])
Returns the 20 most recent statuses posted from the
authenticating user or the user specified.
Parameters:
user_id – Specifies the ID of the user
screen_name – Specifies the screen name of the user
You could accomplish this through the user timeline API endpoint; however, depending on how many terms and users you want to track, you'd have to worry about rate limits (the user timeline endpoint rate limit is pretty high, 300/app auth/15 mins, 150/user auth/15 mins = 450/15 mins), and also the fact that you'd have to call the endpoint manually at some time interval.
Another way to do this is by using the streaming API endpoint, specifically the user stream. Follow everyone you want to reply to, and then create some rules for specific phrases. As followed users post tweets, they should stream to your user stream endpoint. You'd just have to have a listener running, with some logic for tracked users/phrases.
track = {
'taco': ['Bob', 'Sue'],
'salsa': ['John', 'Steve'],
'guacamole': ['Mary', 'Fred']
}
You'd subclass Tweepy's StreamListener:
class TacoListener(tweepy.StreamListener):
def on_status(self, status):
# Note, I rolled my own Twitter API wrapper, and just had a glance at the Tweepy docs, so some of this syntax might be incorrect, change it as required
# I think Tweepy has syntax like: status.text; I'll just refer to the fields as they appear in the Twitter JSON payload
for k, v in track.items():
if k in status.get('text') and status.get('screen_name') in v:
tweet = ""
for name in v:
tweet += "#" + name
tweet += " are talking about " + k + "! Yum."
api.update_status(status=tweet)