I have the code below but I keep hitting the API limit when muting more than 200 accounts followed by the API limit error. I wanted to use 'wait_on_rate_limit' to make it continue once Twitter's limit has reset but the below code still comes up with the same error
import tweepy
import time
consumer_key = *****
consumer_secret = *****
key = *****
secret = *****
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
user_name = 'twitteruser'
def mute():
followers = api.followers_ids(user_name)
for x in followers:
api.create_mute(x)
print ('muted follower')
while True:
mute()
time.sleep (300)
Is anyone able to help me so that the code will just continue running once the limit has been reset?
Thanks
The create_mute function is not able to rely on a rate limit header that can be measured, as this is an account limit rather than an API limit (this is the case with most POST / create operations on the Twitter API). The per-user per-day/hour/minute mute limit is not documented. You'll have to put in some waits to try to manage this more slowly rather than blasting through all of the mutes in one go.
Related
I'm trying to update the stream every 15 minutes to change its rules.
As far as I understand it is impossible to update the filter rules in real time. So I try to stop the stream and then start it again.
class MyStream(tweepy.StreamingClient):
def disconnect(self):
self.running=False
print('stop stream)
stream = MyStream(bearer_token=bearer_token, wait_on_rate_limit=True)
stream.disconnect()
But it doesn't work. Streaming continues to work.
Can you please tell me how to reallocate what I have in mind?
update
I try to add a rule to the stream, then wait 10 seconds and add another one. But it doesn't work. Can you please tell me what the problem is and how to fix it?
import telebot
import tweepy
import time
bot = telebot.TeleBot()
api_key =
api_key_secret =
bearer_token =
access_token =
access_token_secret =
client = tweepy.Client(bearer_token, api_key, api_key_secret, access_token, access_token_secret)
auth = tweepy.OAuth1UserHandler(api_key, api_key_secret, access_token, access_token_secret)
api = tweepy.API(auth)
class MyStream(tweepy.StreamingClient):
def on_connect(self):
print('Connected')
def on_response(self, response):
print(response)
stream = MyStream(bearer_token=bearer_token, wait_on_rate_limit=True)
rules = ['book', 'tree', 'word']
#create the stream
for rule in rules:
stream.add_rules(tweepy.StreamRule(rule))
print('Showing the rule')
print(stream.get_rules().data)
stream.filter(tweet_fields=["referenced_tweets"])
# this part of the code no longer works.
print('sleep 10 sec')
time.sleep(10)
# this part not working too
print('Final Streaming Rules:')
print(stream.get_rules().data)
In Twitter API v2 (Tweepy using the tweepy.client interface and StreamingClient object) the stream does not need to disconnect in order to update the rules, you do that by adding rules via StreamingClient.add_rules(). Docs:
StreamingClient.add_rules() can be used to add rules before using StreamingClient.filter() to connect to and run a filtered stream:
streaming_client.add_rules(tweepy.StreamRule("Tweepy"))
streaming_client.filter()
StreamingClient.get_rules() can be used to retrieve existing rules
and
StreamingClient.delete_rules() can be used to delete rules.
I have made a code that will mute the followers of a designated twitter account but obviously when pulling the follower IDs I can only get 5000. Is there a way of me continuing to pull more using a 'last seen' method or cursor?
import tweepy
import time
consumer_key = *****
consumer_secret = *****
key = *****
secret = *****
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
user_name = 'realdonaldtrump'
def mute():
muted_users = api.mutes_ids()
followers = api.followers_ids(user_name)
try:
for x in followers:
if x in muted_users :
pass
else:
api.create_mute(x)
time.sleep(5)
except Exception:
print('Error')
Yes, this function does support cursors. From the Tweepy examples, you can use them like this - you’d need to modify this for muting rather than following.
for follower in tweepy.Cursor(api.followers).items():
follower.follow()
The issue you will hit with an account with a very large number of followers is that the rate limit here is low - 15 calls in 15 minutes - so this will take a very long time to complete. You may also hit account limits for the number of accounts you can mute within a time period.
Is it possible to write a script for twitter that checks when the last time a certain user has tweeted?
Preferably using python.
Yes, it is possible. Here is how using TwitterAPI.
from TwitterAPI import TwitterAPI
SCREEN_NAME = 'justinbieber'
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
ACCESS_TOKEN_KEY = 'XXXX'
ACCESS_TOKEN_SECRET = 'XXXX'
api = TwitterAPI(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN_KEY,
ACCESS_TOKEN_SECRET)
r = api.request('statuses/user_timeline', {'screen_name':SCREEN_NAME, 'count':1})
for item in r:
print(item['created_at'])
There is a python library for accessing the Twitter API called Tweepy
More info on the API can be found here: https://dev.twitter.com
An older post but may be relevant: streaming api with tweepy only returns second last tweet and NOT the immediately last tweet
After two days of unsuccessful attempt to use twitter gem I have decided to use tweepy of python for a task. (My original attempt was with ruby and I posted the question here)
My task is to collect all those actresses who have a verified account on twitter. I have taken the list of actresses from wikipedia.
Everything looks fine till now. I have started hitting twitter REST api with each name and I check whether it is a verified account or not.
The only problem I have is that the response is very slow. It takes about 12-15 seconds for every request. Am I doing something wrong here or is it how it is suppose to be.
Below is my code in its entirety :
import tweepy
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token_key = 'xx-xx'
access_token_secret = 'xxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
api = tweepy.API(auth)
actresses = []
f = open('final','r')
for line in f:
actresses.append(line)
f.close()
print actresses
for actress in actresses:
print actress
users = api.search_users(actress)
for u in users:
if u.verified == True and u.name == actress:
print u.name + " === https://twitter.com/" + u.screen_name
Also is there any better way to extract the verified actresses using that list?
Unfortunately, there is no faster way to do it, given that you only know the actresses' full names, and not their screen names. Each request will take a long time, as Twitter needs to return the results of users matching the query (there may be quite a few). Each one needs to be loaded and examined, which can take a while, depending on how many results were returned.
I have problems with the new Twitter API: v1.0 is working without problems, but if I change the URL to v1.1 I get all the time a error "400 Bad request" (seen with Firebug).
Example:
https://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitterapi
This is working like a charm, everything works as excepted.
Simply changing the URL to .../1.1/... and I get a Bad request error and even to JSON error response or even some content at all.
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi
Note: It couldn't be a rate limitation, because I accessed the URL the first time ever.
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi redirects me to https://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitterapi
Looks like 1.1 is the same thing as 1
UPD: Looks like this is a rate limit (as 1.1 link worked for me 2 hours ago). Even if you hit API page for the first time, some of your apps (descktop or mobile) could use API methods.
UPD2: in 1.1 400 Bad request means you are not autorized (https://dev.twitter.com/docs/error-codes-responses, https://dev.twitter.com/docs/auth/oauth#user-context). So you need to get user context
You need to authenticate and authorize using oauth before using v1.1 apis
Here is something which works with python tweepy - gets statuses from users timeline
def twitter_fetch(screen_name = "BBCNews",maxnumtweets=10):
'Fetch tweets from #BBCNews'
# API described at https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
consumer_token = '' #substitute values from twitter website
consumer_secret = ''
access_token = ''
access_secret = ''
auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
auth.set_access_token(access_token,access_secret)
api = tweepy.API(auth)
#print api.me().name
#api.update_status('Hello -tweepy + oauth!')
for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(2):
print status.text+'\n'
if __name__ == '__main__':
twitter_fetch('BBCNews',10)
For me the cause was the size of the media that was attached to the tweet. If it was <1.2MB it went through OK, but if it was over, I would get a 400 error every time.
Strange considering Twitter says the tweet limit is 3MB https://twittercommunity.com/t/getting-media-parameter-is-invalid-after-successfully-uploading-media/58354