Created
July 18, 2018 12:43
-
-
Save MerleLiuKun/a4ec055bb3d8b3dee8281c8763171517 to your computer and use it in GitHub Desktop.
TwitterApi request retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from app.ext.token_gainer import twitter_api | |
from TwitterAPI.TwitterError import TwitterConnectionError, TwitterRequestError | |
def call_request(url, params, retries=3, api=None): | |
""" | |
:param url: twitter api | |
:param params: api param conmmon dict(t) | |
:param retries: retry times. | |
:param api: Twitter api | |
:return: | |
""" | |
data = None | |
try: | |
if api: | |
data = api.request(url, params) | |
else: | |
api = twitter_api.api | |
data = api.request(url, params) | |
except (TwitterConnectionError, TwitterRequestError) as e: | |
print(e) | |
if retries > 0: | |
api = twitter_api.refresh_api() | |
return call_request(url, params, retries - 1, api) | |
except Exception as e: | |
print(e) | |
return None | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment