我已经在IIS上部署了DJango应用程序。它使用tweepy获取推文。当我在请求之间获取历史性推文时,请求丢失了,没有任何记录,也没有引发异常。我已附上代码片段。
注意:当我在调试模式下运行相同的代码时,一切正常。
此代码通过ID获取每个推文的状态。
def get_tweet(id, api=None):
status = []
try:
logger.info('retrieve_tweet.get_tweet() invoked.')
if api is None:
logger.info('get_tweet api none found.')
CONSUMER_KEY = config.TWITTER_CONSUMER_KEY
CONSUMER_SECRET = config.TWITTER_CONSUMER_SECRET
ACCESS_TOKEN = config.TWITTER_ACCESS_TOKEN
ACCESS_TOKEN_SECRET = config.TWITTER_ACCESS_TOKEN_SECRET
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, retry_count=2,
retry_delay=5,
retry_errors=[401], timeout=300000)
status = api.get_status(id, tweet_mode='extended')
logger.info('retrieve_tweet.get_tweet() end.')
return status
except Exception as ex:
logger.error('Exception in retrieve_tweet.get_tweet(): %s', ex)
return None
以下代码从获取的对话中获取父推文:
child_status = get_tweet(id=id, api=api)
logger.info('child_status: %s', child_status)
if child_status:
status.append(child_status._json)
in_reply_to_status_id = child_status.in_reply_to_status_id
while in_reply_to_status_id is not None:
parent_status = get_tweet(id=child_status.in_reply_to_status_id, api=api)
logger.info('parent status inside while: %s', parent_status)
if parent_status:
status.append(parent_status._json)
in_reply_to_status_id = parent_status.in_reply_to_status_id
if in_reply_to_status_id is None:
logger.info('All the parent tweets are found.')
else:
child_status = parent_status
else:
in_reply_to_status_id = None
if len(status) > 1:
status = sorted(status, key=lambda x: x['id'])
else:
logger.info('no parent tweets found.')
status = None
logger.info('retrieve_tweet.get_parent_tweet() end.')
以下是日志文件中的日志
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:68] : retrieve_tweet.get_parent_tweet() invoked.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:69] : parent tweet id: 1021160191147094017
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:40] : retrieve_tweet.get_tweet() invoked.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:57] : retrieve_tweet.get_tweet() end.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:106] : no parent tweets found.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:109] : retrieve_tweet.get_parent_tweet() end.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:68] : retrieve_tweet.get_parent_tweet() invoked.
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:69] : parent tweet id: 1021158630886977538
[24/Jul/2018 01:05:01] | INFO | [dashboard.analysis.retreive_tweet:40] : retrieve_tweet.get_tweet() invoked.
在调用get_tweet()之后,没有日志,并抛出500异常。