From 1ba5b4cf4b6f34727082a2bcf10d00c36afc5560 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sat, 27 May 2023 23:01:29 -0400 Subject: [PATCH] Fix retroarch defaulting to utc --- vrobbler/apps/scrobbles/exceptions.py | 2 ++ vrobbler/apps/videogames/retroarch.py | 30 +++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 vrobbler/apps/scrobbles/exceptions.py diff --git a/vrobbler/apps/scrobbles/exceptions.py b/vrobbler/apps/scrobbles/exceptions.py new file mode 100644 index 0000000..bcb9a5d --- /dev/null +++ b/vrobbler/apps/scrobbles/exceptions.py @@ -0,0 +1,2 @@ +class UserNotFound(Exception): + ... diff --git a/vrobbler/apps/videogames/retroarch.py b/vrobbler/apps/videogames/retroarch.py index 0d1717a..dfe69c0 100644 --- a/vrobbler/apps/videogames/retroarch.py +++ b/vrobbler/apps/videogames/retroarch.py @@ -7,14 +7,17 @@ from typing import List import pytz from dateutil.parser import ParserError, parse from django.apps import apps - -from vrobbler.apps.scrobbles.utils import convert_to_seconds -from vrobbler.apps.videogames.scrapers import scrape_game_name_from_adb -from vrobbler.apps.videogames.utils import get_or_create_videogame +from django.conf import settings +from django.contrib.auth import get_user_model +from scrobbles.utils import convert_to_seconds +from videogames.models import VideoGame +from videogames.scrapers import scrape_game_name_from_adb +from videogames.utils import get_or_create_videogame +from vrobbler.apps.scrobbles.exceptions import UserNotFound logger = logging.getLogger(__name__) -from videogames.models import VideoGame +User = get_user_model() def load_game_data(directory_path: str, user_tz=None) -> dict: @@ -36,7 +39,7 @@ def load_game_data(directory_path: str, user_tz=None) -> dict: directory = os.fsencode(directory_path) games = {} if not user_tz: - user_tz = pytz.utc + user_tz = settings.TIME_ZONE for file in os.listdir(directory): filename = os.fsdecode(file) @@ -77,8 +80,12 @@ def import_retroarch_lrtl_files(playlog_path: str, user_id: int) -> List[dict]: 4. Calculate scrobble time from runtime - last_scrobble.long_play_time """ Scrobble = apps.get_model("scrobbles", "Scrobble") + user = User.objects.filter(pk=user_id).first() + if not user: + logger.warning(f"User ID {user_id} is not valid, cannot scrobble") + raise UserNotFound - game_logs = load_game_data(playlog_path) + game_logs = load_game_data(playlog_path, user.profile.timezone) found_game = None new_scrobbles = [] @@ -97,24 +104,25 @@ def import_retroarch_lrtl_files(playlog_path: str, user_id: int) -> List[dict]: found_game.retroarch_name = game_name found_game.save(update_fields=["retroarch_name"]) + end_datetime = game_data.get("last_played") if found_game: found_scrobble = found_game.scrobble_set.filter( - stop_timestamp=game_data["last_played"] + stop_timestamp=end_datetime ) if found_scrobble: logger.info( - f"Found scrobble for {game_name} with stop_timestamp {game_data['last_played']}, not scrobbling" + f"Found scrobble for {game_name} with stop_timestamp {end_datetime}, not scrobbling" ) continue last_scrobble = found_game.scrobble_set.last() + # Default to 0 for delta, but if there's an past scrobble, use that delta_runtime = 0 if last_scrobble: delta_runtime = last_scrobble.long_play_seconds - playback_position_seconds = game_data["runtime"] - delta_runtime - timestamp = game_data["last_played"] + timedelta( + timestamp = end_datetime - timedelta( seconds=playback_position_seconds )