Fix retroarch defaulting to utc

This commit is contained in:
2023-05-27 23:01:29 -04:00
parent 39add993e0
commit 1ba5b4cf4b
2 changed files with 21 additions and 11 deletions

View File

@ -0,0 +1,2 @@
class UserNotFound(Exception):
...

View File

@ -7,14 +7,17 @@ from typing import List
import pytz import pytz
from dateutil.parser import ParserError, parse from dateutil.parser import ParserError, parse
from django.apps import apps from django.apps import apps
from django.conf import settings
from vrobbler.apps.scrobbles.utils import convert_to_seconds from django.contrib.auth import get_user_model
from vrobbler.apps.videogames.scrapers import scrape_game_name_from_adb from scrobbles.utils import convert_to_seconds
from vrobbler.apps.videogames.utils import get_or_create_videogame 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__) logger = logging.getLogger(__name__)
from videogames.models import VideoGame User = get_user_model()
def load_game_data(directory_path: str, user_tz=None) -> dict: 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) directory = os.fsencode(directory_path)
games = {} games = {}
if not user_tz: if not user_tz:
user_tz = pytz.utc user_tz = settings.TIME_ZONE
for file in os.listdir(directory): for file in os.listdir(directory):
filename = os.fsdecode(file) 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 4. Calculate scrobble time from runtime - last_scrobble.long_play_time
""" """
Scrobble = apps.get_model("scrobbles", "Scrobble") 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 found_game = None
new_scrobbles = [] 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.retroarch_name = game_name
found_game.save(update_fields=["retroarch_name"]) found_game.save(update_fields=["retroarch_name"])
end_datetime = game_data.get("last_played")
if found_game: if found_game:
found_scrobble = found_game.scrobble_set.filter( found_scrobble = found_game.scrobble_set.filter(
stop_timestamp=game_data["last_played"] stop_timestamp=end_datetime
) )
if found_scrobble: if found_scrobble:
logger.info( 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 continue
last_scrobble = found_game.scrobble_set.last() last_scrobble = found_game.scrobble_set.last()
# Default to 0 for delta, but if there's an past scrobble, use that
delta_runtime = 0 delta_runtime = 0
if last_scrobble: if last_scrobble:
delta_runtime = last_scrobble.long_play_seconds delta_runtime = last_scrobble.long_play_seconds
playback_position_seconds = game_data["runtime"] - delta_runtime playback_position_seconds = game_data["runtime"] - delta_runtime
timestamp = game_data["last_played"] + timedelta( timestamp = end_datetime - timedelta(
seconds=playback_position_seconds seconds=playback_position_seconds
) )