Add genre fetching from IGDB

This commit is contained in:
2023-03-14 18:44:28 -04:00
parent c9874b3fda
commit 5b5b67d42a
2 changed files with 10 additions and 1 deletions

View File

@ -62,7 +62,7 @@ def lookup_game_from_igdb(igdb_id: str) -> Dict:
"Authorization": f"Bearer {get_igdb_token()}",
"Client-ID": IGDB_CLIENT_ID,
}
fields = "id,name,alternative_names.*,release_dates.*,cover.*,screenshots.*,rating,rating_count,summary"
fields = "id,name,alternative_names.*,genres.*,release_dates.*,cover.*,screenshots.*,rating,rating_count,summary"
game_dict = {}
body = f"fields {fields}; where id = {igdb_id};"
@ -94,6 +94,10 @@ def lookup_game_from_igdb(igdb_id: str) -> Dict:
release_date = datetime.utcfromtimestamp(release_date).replace(
tzinfo=pytz.utc
)
genres = []
if "genres" in game.keys():
for genre in game.get("genres"):
genres.append(genre["name"])
game_dict = {
"igdb_id": game.get("id"),
@ -105,6 +109,7 @@ def lookup_game_from_igdb(igdb_id: str) -> Dict:
"rating_count": game.get("rating_count"),
"release_date": release_date,
"summary": game.get("summary"),
"genres": genres,
}
return game_dict

View File

@ -72,16 +72,20 @@ def load_game_data_from_igdb(
logger.info(f"Looking up video game {igdb_id}")
game_dict = lookup_game_from_igdb(igdb_id)
if not game_dict:
logger.warn(f"No game data found on IGDB for ID {igdb_id}")
return
screenshot_url = game_dict.pop("screenshot_url")
cover_url = game_dict.pop("cover_url")
genres = game_dict.pop("genres")
VideoGame.objects.filter(pk=game.id).update(**game_dict)
game.refresh_from_db()
game.genre.add(*genres)
if not game.screenshot:
r = requests.get(screenshot_url)
if r.status_code == 200: