[PYTHON] About tweepy error handling

Introduction

When I hit the Twitter API, I use tweepy because it's easy, Error handling is unexpectedly difficult.

I use it quite often, so I'll leave it as a memo.

About tweepy error handling

When you are getting a lot of tweets in a loop

--For Rate Limit, wait 15 minutes and hit the same API again. --If the specified data cannot be found due to deletion or freezing, you must stop hitting the API.

I just want to branch this difference.

So basically, I'm doing the following: ↓

import datetime
import time
import tweepy

while True:
		try:
			data = api.search(...)
			break
		except tweepy.TweepError as e:
			print(e)
			if e.reason == "[{'message': 'Rate limit exceeded', 'code': 88}]":
				print('Error: wait 15 minutes')
                #Shows the time when processing stopped even though it waited for 15 minutes.
				print(datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
				time.sleep(60 * 15)
			else:
				break

Addendum

It would be nice if this kind of processing was enough, It seems that there was a way to specify it officially. http://docs.tweepy.org/en/latest/api.html I told you in the comment section. Thank you very much.

Stumble point

** e ** was confusing.

print(e)

[{'message': 'Rate limit exceeded', 'code': 88}]

From the result, it looks like an associative array at first glance.


** But it wasn't. ** **

print(e.reason)
print(str(e.reason))

[{'message': 'Rate limit exceeded', 'code': 88}]
<class 'str'>

e.reason was a string! That is, the string [{'message':'Rate limit exceeded','code': 88}]!


That's why

if e.reason == "[{'message': 'Rate limit exceeded', 'code': 88}]":
    ...

It is processing such as.

I'm pleasure to be of some help.

Recommended Posts

About tweepy error handling
About FastAPI ~ Endpoint error handling ~
Mainframe error handling
Python Error Handling
SikuliX error handling
django.db.migrations.exceptions.InconsistentMigrationHistory error handling
Python, about exception handling
Error handling in PythonBox
GraphQL (gqlgen) error handling
Around feedparser error handling
About Go error handling * This is a rough article.
[Error countermeasures] django-heroku installation error handling
Error handling when installing mecab-python
Data handling 3 (development) About data format
PyCUDA build error handling memorandum
About handling Django static files
Error divided by 0 Handling of ZeroDivisionError
[Error handling] peewee.IntegrityError 1451 occurs in peewee
Error handling when updating Fish shell
About import error of PyQt5.QtWidgets (Anaconda)
Error handling during Django migrate'DIRS': [BASE_DIR /'templates']
A story about a 503 error on Heroku open