From 750fa05b15a352f911d343385334bdc746c104bd Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Mon, 18 Mar 2024 14:41:00 -0400 Subject: [PATCH] [videogames] Order by ID desc from IGDB to get better results --- vrobbler/apps/videogames/igdb.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/vrobbler/apps/videogames/igdb.py b/vrobbler/apps/videogames/igdb.py index 318db79..4432e1c 100644 --- a/vrobbler/apps/videogames/igdb.py +++ b/vrobbler/apps/videogames/igdb.py @@ -1,11 +1,12 @@ import json import logging from datetime import datetime +from operator import itemgetter from typing import Dict, Tuple -from django.conf import settings import pytz import requests +from django.conf import settings from django.contrib.auth import get_user_model TWITCH_AUTH_BASE = "https://id.twitch.tv/" @@ -49,9 +50,22 @@ def lookup_game_id_from_gdb(name: str) -> str: response = requests.post(SEARCH_URL, data=body, headers=headers) results = json.loads(response.content) if not results: - logger.warn(f"Search of game on IGDB failed for name {name}") + logger.warn( + f"Search of game on IGDB failed, no results found", + extra={"name": name}, + ) return "" + if results[0].get("status") == 406: + logger.warn( + f"Search of game on IGBD failed, API error", + extra={ + "cause": results.get("cause"), + "details": results.get("details"), + }, + ) + # Sort our result by IDs so we always get the lowest ID, which is likely to be the least esoteric game + results = sorted(results, key=itemgetter("id"), reverse=True) return results[0].get("game", "")