I have some code that follows everyone in return when they follow me:
import twitter
from sets import Set
try:
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='')
following = api.GetFriends()
friendNames = Set()
for friend in following:
friendNames.add(friend.screen_name)
followers = api.GetFollowers()
for follower in followers:
if (not follower.screen_name in friendNames):
api.CreateFriendship(follower.screen_name)
except Exception as exc:
print('generated an exception: %s' % (exc))
This is throwing up the following error:
generated an exception: [{'message': 'Rate limit exceeded', 'code': 88}]
I am aware that their is a restriction around how many followers you can follow back per hour using this code via the API, however I have manually followed all my followers back and still got the same error. Can anyone see what the issue is here?
Thanks
Related
I wrote this function to call weather api
def weather_data(df):
import json
query_data = urllib.request.urlopen(f"https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{df['Latitude']},{df['Longitude']}/{df['date_time_epoch']}?key= any key ID")
json_decode = query_data.read().decode('utf-8')
extracted_info = json.loads(json_decode)
temp = extracted_info['days'][0]['temp']
feelslike = extracted_info['days'][0]['feelslike']
return pd.Series([temp, feelslike])
When I test to call 10K-35K records, There is no problem. However, when I run it to call 500K ->400K -> 200K record, it shows HTTPError: HTTP Error 502: Bad Gateway. I pay the subscription fee to use this service.
Does anyone know how to fix this?
I am trying to create an array to send emails in Mailchimps API. I am following their documentation and I got the following error:
An exception occurred: {'status': 'error', 'code': -2, 'name': 'ValidationError', 'message': 'Validation error: {"message":{"to":["Please enter an array"]}}'}
So I've updated my array as follows:
message = {"from_email": "test1#email.com","from_name":"Test","message":{"to":["email":"test2#email.com"]}}
However, I'm getting an invalid syntax error under my nested array in messages and I can't quite seem to solve this. Could anyone quickly help?
Seems like you used list [] in place of {} for dictionary
..."message":{"to":["email":"test2#email.com"]}}
^ ^
change it to
message = {"from_email": "test1#email.com","from_name":"Test","message":{"to":{"email":"test2#email.com"}}}
As per the documentation of Mail-Chimp and as you tagged with Python, you may use their Python SDK and sending a mail is as simple as below -
import mailchimp_transactional as MailchimpTransactional
from mailchimp_transactional.api_client import ApiClientError
try:
mailchimp = MailchimpTransactional.Client("YOUR_API_KEY")
response = client.messages.send({"message": {}})
print(response)
except ApiClientError as error:
print("An exception occurred: {}".format(error.text))
For Installation, use pip install mailchimp_transactional
I'm not yet very familiar with error handling in Python. I'd like to know how to retrieve the error code when I get an error using the python-twitter package :
import twitter
#[...]
try:
twitter_connexion.friendships.create(screen_name = "someone_who_blocked_me", follow = True)
except twitter.TwitterHTTPError as twittererror:
print(twittererror)
Twitter sent status 403 for URL: 1.1/friendships/create.json using parameters: (follow=True&oauth_consumer_key=XXX&oauth_nonce=XXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=XXX&oauth_token=XXX&oauth_version=1.0&screen_name=someone_who_blocked_me&oauth_signature=XXX)
details: {'errors': [{'message': 'You have been blocked from following this account at the request of the user.', 'code': 162}]}
In this case, I'd like to retrieve the 'code': 162 part, or just the 162.
twittererror.args is a tuple with one element in it, which is a string, print(twittererror.args[0][0:10]) outputs Twitter se
How can I get the 162 ?
(Obviously, twittererror.args[0][582:585] is not the answer I'm looking for, as any other error message will be of a different length and the error code won't be at [582:585])
Looking at how the TwitterHTTPError is defined in this GitHub repo, you should obtain the dict with twittererror.response_data.
Therefore you can do something like that:
import twitter
#[...]
try:
twitter_connexion.friendships.create(screen_name = "someone_who_blocked_me", follow = True)
except twitter.TwitterHTTPError as twittererror:
for error in twittererror.response_data.get("errors", []):
print(error.get("code", None))
Question
I was writing a Twitter bot with Python an Tweepy. I successfully got the bot to work last night, however, after following 60 people, the bot started throwing an error that says [{u'message': u'Rate Limit Exceeded', u'code': 88}]. I understand that I am only allowed to do a certain amount of calls to the Twitter API and I have found this link that shows how many calls I can make on each of these functions. After reviewing my code, I have found that the error is being thrown where I am saying for follower in tweepy.Cursor(api.followers, me).items():. On the page that I found that says how many requests I get, it says that I get 15 requests for every 15 minutes for getting my followers. I have waited overnight and I retried the code this morning, however, it was still throwing the same error. I don't understand why Tweepy is throwing a rate limit exceeded error whenever I should still have requests left.
Code
Here is my code that's throwing the error.
#!/usr/bin/python
import tweepy, time, pprint
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
me = api.me()
pprint.pprint(api.rate_limit_status())
while True:
try:
for follower in tweepy.Cursor(api.followers, me).items():
api.create_friendship(id=follower.id)
for follower in tweepy.Cursor(api.friends, me).items():
for friend in tweepy.Cursor(api.friends, follower.id).items():
if friend.name != me.name:
api.create_friendship(id=friend.id)
except tweepy.TweepError, e:
print "TweepError raised, ignoring and continuing."
print e
continue
I have found the line that throws the error by typing in the interactive prompt
for follower in tweepy.Cursor(api.followers, me).items():
print follower
where it gives me the error
**Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
for follower in api.followers(id=me.id):
File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 239, in _call
return method.execute()
File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 223, in execute
raise TweepError(error_msg, resp)
TweepError: [{u'message': u'Rate limit exceeded', u'code': 88}]**
API().followers method is actually GET followers/list, which is limited to 15 requests per window (before next epoch). Each call returns 20 users list by default, and if you are reaching limit exceed error, I'm sure authenticated user has more than 300 followers.
The solution is to increase the length of user's list received during each call, such that within 15 calls it can fetch all the users. Modify your code as following
for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
print follower
count denotes the number of users to be fetched per request.
Maximum value of count can be 200, it means within 15 minute, you can only fetch 200 x 15 = 3000 followers, which is enough in general scenarios.
I am using tweepy to make a twitter application.
When users tweet/update profile, etc, they will get some errors. I want to classify error and give user more information.
try:
tweet/update profile/ follow....
except tweepy.TweepError, e:
if tweepy.TweepError is "Account update failed: Description is too long (maximum is 160 characters)"
Do something
if tweepy.TweepError is "Failed to send request: Invalid request URL: http://api.twitter.com/1/account/update_profile.json?location=%E5%85%B5%E5%BA%A"
Do something
if tweepy.TweepError is "[{u'message': u'Over capacity', u'code': 130}]"
Do something
Is the only way to classify error is to compare e with string, for example, Account update failed: Description is too long (maximum is 160 characters)?
Right, it is the only way now. There is only one TweepError exception defined. It's raised throughout the app with different text.
Here's the relevant open issue on github. So there is a chance that it'll be improved in the future.