def lookup_user(parent_db, user_id): ''' parent_db: The parent object that the user is nested under user_id: The user to lookup This method will perform the necessary API calls to lookup a Twitch user and their associated game and returns their associated game's id. ''' global games_dict global users_dict if user_id not in users_dict: # users_set.add(user_id) user_db = User() user_db.id = user_id # Have to do another request to get description for user because not included in teams users response user_json = requests.get((channel_api_url + str(user_db.id)), headers=headers).json() user_db.name = user_json.get('display_name') user_db.description = user_json.get('description') if not user_db.description: user_db.description = 'This user has no bio.' # This is what Twitch displays if a user leaves this field empty. language_code = user_json.get('language') language = language_code if language_code: try: language_iso = iso639.to_name(language_code) if language_iso: language = language_iso except: pass user_db.language = language user_db.views = user_json.get('views') user_db.followers = user_json.get('followers') user_db.url = user_json.get('url') user_db.created = user_json.get('created_at') user_db.updated = user_json.get('updated_at') user_db.image_url = user_json.get('logo') if user_db.image_url == None: user_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png' # This is Twitch's default user logo if isinstance(parent_db, Team): user_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): user_db.community_id = parent_db.id user_game_name = user_json.get('game') if user_game_name != None: # Get game information from 2nd API gb_request_url = gb_api_url + user_game_name + '&resources=game' game_response = requests.get(gb_request_url, headers={'user-agent': 'streamGlean'}) game_json = None if game_response: game_json = game_response.json() if game_json: game_json = game_json.get('results') if game_json: game_json = game_json[0] game_id = game_json.get('id') if game_id and game_id not in games_dict: user_db.game_id = game_id game_db = Game() game_db.id = game_id game_db.user_ids = [user_db.id] if isinstance(parent_db, Team): game_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): game_db.community_ids = [parent_db.id] game_db.name = game_json.get('name') game_db.description = game_json.get('deck') genres = [] giantbomb_game_api_url = 'http://www.giantbomb.com/api/game/' + str( game_db.id ) + '?api_key=0ac0037d403fff412c9e9ac9e60a23acc5b2e736&format=json' game_response = requests.get( giantbomb_game_api_url, headers={'user-agent': 'streamGlean'}) if game_response: game_response = game_response.json().get( 'results') genres_response = game_response.get('genres') if genres_response: for genre_dict in game_response.get( 'genres'): genres.append(genre_dict.get('name')) game_db.genres = genres platforms = [] game_platforms = game_json.get('platforms') if (game_platforms and isinstance(game_platforms, Iterable)): for platform_dict in game_platforms: platforms.append(platform_dict.get('name')) game_db.platforms = platforms game_db.release_date = game_json.get( 'original_release_date') rating = game_json.get('original_game_rating') if rating: for d in rating: if 'ESRB' in d.get('name'): actual_rating = d.get('name').replace( 'ESRB: ', '') game_db.rating = actual_rating game_image = game_json.get('image') if game_image: game_image_small_url = game_image.get( 'small_url') if game_image_small_url: game_db.image_url = game_image_small_url else: game_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png' # This is Twitch's default logo games_dict[game_db.id] = game_db elif game_id: game_db = games_dict[game_id] if user_db.id not in game_db.user_ids: game_db.user_ids += [user_db.id] if isinstance(parent_db, Team): if game_db.team_ids: if parent_db.id not in game_db.team_ids: game_db.team_ids += [parent_db.id] elif isinstance(parent_db, Community): if game_db.community_ids: if parent_db.id not in game_db.community_ids: game_db.community_ids += [parent_db.id] users_dict[user_db.id] = user_db return user_db.game_id else: user_db = users_dict[user_id] if isinstance(parent_db, Team): if user_db.team_ids != None: user_db.team_ids += [parent_db.id] else: user_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): user_db.community_id = parent_db.id