Fix importing from AGB

This commit is contained in:
2023-05-28 10:11:24 -04:00
parent f55aed7b3d
commit a7e8e4f1dc
4 changed files with 45 additions and 19 deletions

View File

@ -0,0 +1,2 @@
class GameNotFound(Exception):
pass

View File

@ -14,6 +14,7 @@ from videogames.models import VideoGame
from videogames.scrapers import scrape_game_name_from_adb from videogames.scrapers import scrape_game_name_from_adb
from videogames.utils import get_or_create_videogame from videogames.utils import get_or_create_videogame
from vrobbler.apps.scrobbles.exceptions import UserNotFound from vrobbler.apps.scrobbles.exceptions import UserNotFound
from vrobbler.apps.videogames.exceptions import GameNotFound
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -44,9 +45,7 @@ def load_game_data(directory_path: str, user_tz=None) -> dict:
for file in os.listdir(directory): for file in os.listdir(directory):
filename = os.fsdecode(file) filename = os.fsdecode(file)
if not filename.endswith("lrtl"): if not filename.endswith("lrtl"):
logger.info( logger.info(f'Skipping "{filename}", not lrtl file')
f"Found non-gamelog file extension, skipping {filename}"
)
continue continue
game_name = filename.split(".lrtl")[0].split(" (")[0] game_name = filename.split(".lrtl")[0].split(" (")[0]
@ -94,14 +93,26 @@ def import_retroarch_lrtl_files(playlog_path: str, user_id: int) -> List[dict]:
for game_name, game_data in game_logs.items(): for game_name, game_data in game_logs.items():
# Use the retroarch name, because we can't change those but may want to # Use the retroarch name, because we can't change those but may want to
# tweak the found game # tweak the found game
logger.info(f"Received name {game_name}")
try:
mame_name = scrape_game_name_from_adb(game_name) mame_name = scrape_game_name_from_adb(game_name)
except GameNotFound as e:
logger.warning(e)
continue
if mame_name: if mame_name:
logger.info(f"Found name {game_name}")
game_name = mame_name game_name = mame_name
found_game = VideoGame.objects.filter(retroarch_name=game_name).first() found_game = VideoGame.objects.filter(retroarch_name=game_name).first()
if not found_game: if not found_game:
try:
found_game = get_or_create_videogame(game_name) found_game = get_or_create_videogame(game_name)
except GameNotFound as e:
logger.warning(f"Game not found for: {e}")
continue
if found_game: if found_game:
found_game.retroarch_name = game_name found_game.retroarch_name = game_name
found_game.save(update_fields=["retroarch_name"]) found_game.save(update_fields=["retroarch_name"])
@ -112,9 +123,7 @@ def import_retroarch_lrtl_files(playlog_path: str, user_id: int) -> List[dict]:
stop_timestamp=end_datetime stop_timestamp=end_datetime
) )
if found_scrobble: if found_scrobble:
logger.info( logger.info(f"Skipping scrobble for game {found_game.id}")
f"Found scrobble for {game_name} with stop_timestamp {end_datetime}, not scrobbling"
)
continue continue
last_scrobble = found_game.scrobble_set.last() last_scrobble = found_game.scrobble_set.last()
@ -148,4 +157,5 @@ def import_retroarch_lrtl_files(playlog_path: str, user_id: int) -> List[dict]:
) )
) )
created_scrobbles = Scrobble.objects.bulk_create(new_scrobbles) created_scrobbles = Scrobble.objects.bulk_create(new_scrobbles)
logger.info(f"Created {len(created_scrobbles)} scrobbles")
return new_scrobbles return new_scrobbles

View File

@ -4,6 +4,8 @@ from typing import Optional
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from vrobbler.apps.videogames.exceptions import GameNotFound
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
MAME_LOOKUP_URL = "http://adb.arcadeitalia.net/dettaglio_mame.php?game_name={query}&search_id=2" MAME_LOOKUP_URL = "http://adb.arcadeitalia.net/dettaglio_mame.php?game_name={query}&search_id=2"
@ -26,11 +28,14 @@ def scrape_game_name_from_adb(name: str) -> str:
title = "" title = ""
headers = {"User-Agent": "Vrobbler 0.11.12"} headers = {"User-Agent": "Vrobbler 0.11.12"}
url = MAME_LOOKUP_URL.format(query=name) url = MAME_LOOKUP_URL.format(query=name)
r = requests.get(url, headers=headers) resp = requests.get(url, headers=headers)
if r.status_code == 200: if not resp.ok:
soup = BeautifulSoup(r.text, "html.parser") raise GameNotFound(f"Lookup failed with code {resp.status_code}")
soup = BeautifulSoup(resp.text, "html.parser")
title = _get_title_from_soup(soup).split(" (")[0].split(" / ")[0] title = _get_title_from_soup(soup).split(" (")[0].split(" / ")[0]
logger.info(f"Found title {title}")
if title == "Arcade Database": if title == "Arcade Database":
title = "" title = ""

View File

@ -3,10 +3,11 @@ from typing import Optional
import requests import requests
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from videogames.models import VideoGame, VideoGamePlatform
from videogames.howlongtobeat import lookup_game_from_hltb from videogames.howlongtobeat import lookup_game_from_hltb
from videogames.igdb import lookup_game_from_igdb from videogames.igdb import lookup_game_from_igdb
from videogames.models import VideoGame, VideoGamePlatform
from vrobbler.apps.videogames.exceptions import GameNotFound
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -45,16 +46,24 @@ def get_or_create_videogame(
if "genres" in game_dict.keys(): if "genres" in game_dict.keys():
genres = game_dict.pop("genres") genres = game_dict.pop("genres")
title = game_dict.get("title")
if not title:
raise GameNotFound(name_or_id)
hltb_id = game_dict.get("hltb_id") hltb_id = game_dict.get("hltb_id")
igdb_id = game_dict.get("igdb_id") igdb_id = game_dict.get("igdb_id")
if hltb_id: if hltb_id:
game, game_created = VideoGame.objects.get_or_create(hltb_id=hltb_id)
elif igdb_id:
game, game_created = VideoGame.objects.get_or_create(igdb_id=igdb_id)
else:
game, game_created = VideoGame.objects.get_or_create( game, game_created = VideoGame.objects.get_or_create(
title=game_dict.get("title") hltb_id=hltb_id,
title=title,
) )
elif igdb_id:
game, game_created = VideoGame.objects.get_or_create(
igdb_id=igdb_id,
title=title,
)
else:
game, game_created = VideoGame.objects.get_or_create(title=title)
if game_created or force_update: if game_created or force_update:
VideoGame.objects.filter(pk=game.id).update(**game_dict) VideoGame.objects.filter(pk=game.id).update(**game_dict)