def update_latest_info(account: str, rsp: TwitterResponse): for item in rsp.get_iterator(): if item['id'] > latest_info[account]['last_tweet_id']: latest_info[account]['last_tweet_id'] = item['id'] if item['user']['screen_name'] == account: latest_info[account]['profile_image'] = item['user'][ 'profile_image_url']
def update_twitter_metrics(response: TwitterResponse): quota = response.get_quota() if 'limit' in quota and quota['limit']: API_RATE_LIMIT.labels(platform='twitter', type='limit').set(quota['limit']) if 'remaining' in quota and quota['remaining']: API_RATE_LIMIT.labels(platform='twitter', type='remaining').set(quota['remaining']) API_RESPONSE_CODE.labels(platform='twitter', code=response.status_code).inc()
def mocked_requests_get(*args): class MockResponse: def __init__(self, json_data, status_code): self.json_data = json_data self.status_code = status_code def json(self): return self.json_data if args[1] == 'users/show': r = MockResponse({'id': 1337}, 200) return TwitterResponse(r, None)
def extract_media_url(response: TwitterResponse): try: return response.json()['entities']['media'][0]['media_url'] except Exception: return ""