Add genre fetching from IGDB
This commit is contained in:
@ -62,7 +62,7 @@ def lookup_game_from_igdb(igdb_id: str) -> Dict:
|
|||||||
"Authorization": f"Bearer {get_igdb_token()}",
|
"Authorization": f"Bearer {get_igdb_token()}",
|
||||||
"Client-ID": IGDB_CLIENT_ID,
|
"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 = {}
|
game_dict = {}
|
||||||
body = f"fields {fields}; where id = {igdb_id};"
|
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(
|
release_date = datetime.utcfromtimestamp(release_date).replace(
|
||||||
tzinfo=pytz.utc
|
tzinfo=pytz.utc
|
||||||
)
|
)
|
||||||
|
genres = []
|
||||||
|
if "genres" in game.keys():
|
||||||
|
for genre in game.get("genres"):
|
||||||
|
genres.append(genre["name"])
|
||||||
|
|
||||||
game_dict = {
|
game_dict = {
|
||||||
"igdb_id": game.get("id"),
|
"igdb_id": game.get("id"),
|
||||||
@ -105,6 +109,7 @@ def lookup_game_from_igdb(igdb_id: str) -> Dict:
|
|||||||
"rating_count": game.get("rating_count"),
|
"rating_count": game.get("rating_count"),
|
||||||
"release_date": release_date,
|
"release_date": release_date,
|
||||||
"summary": game.get("summary"),
|
"summary": game.get("summary"),
|
||||||
|
"genres": genres,
|
||||||
}
|
}
|
||||||
|
|
||||||
return game_dict
|
return game_dict
|
||||||
|
|||||||
@ -72,16 +72,20 @@ def load_game_data_from_igdb(
|
|||||||
|
|
||||||
logger.info(f"Looking up video game {igdb_id}")
|
logger.info(f"Looking up video game {igdb_id}")
|
||||||
game_dict = lookup_game_from_igdb(igdb_id)
|
game_dict = lookup_game_from_igdb(igdb_id)
|
||||||
|
|
||||||
if not game_dict:
|
if not game_dict:
|
||||||
logger.warn(f"No game data found on IGDB for ID {igdb_id}")
|
logger.warn(f"No game data found on IGDB for ID {igdb_id}")
|
||||||
return
|
return
|
||||||
|
|
||||||
screenshot_url = game_dict.pop("screenshot_url")
|
screenshot_url = game_dict.pop("screenshot_url")
|
||||||
cover_url = game_dict.pop("cover_url")
|
cover_url = game_dict.pop("cover_url")
|
||||||
|
genres = game_dict.pop("genres")
|
||||||
|
|
||||||
VideoGame.objects.filter(pk=game.id).update(**game_dict)
|
VideoGame.objects.filter(pk=game.id).update(**game_dict)
|
||||||
game.refresh_from_db()
|
game.refresh_from_db()
|
||||||
|
|
||||||
|
game.genre.add(*genres)
|
||||||
|
|
||||||
if not game.screenshot:
|
if not game.screenshot:
|
||||||
r = requests.get(screenshot_url)
|
r = requests.get(screenshot_url)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
|
|||||||
Reference in New Issue
Block a user