Browse Source

[music] Cleans up last.fm import logging

We weren't tracking import info very well in the original flow, this
should provide better insight into what happened after a run.
Colin Powell 6 months ago
parent
commit
36048d9a0a
1 changed files with 28 additions and 12 deletions
  1. 28 12
      vrobbler/apps/music/lastfm.py

+ 28 - 12
vrobbler/apps/music/lastfm.py

@@ -1,14 +1,12 @@
 import logging
 import logging
 import time
 import time
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, UTC
 
 
 import pylast
 import pylast
 import pytz
 import pytz
 from django.conf import settings
 from django.conf import settings
 from django.utils import timezone
 from django.utils import timezone
 from music.utils import (
 from music.utils import (
-    get_or_create_album,
-    get_or_create_artist,
     get_or_create_track,
     get_or_create_track,
 )
 )
 
 
@@ -89,9 +87,14 @@ class LastFM:
             new_scrobbles.append(new_scrobble)
             new_scrobbles.append(new_scrobble)
 
 
         created = Scrobble.objects.bulk_create(new_scrobbles)
         created = Scrobble.objects.bulk_create(new_scrobbles)
+        # TODO Add a notification for users that their import is complete
         logger.info(
         logger.info(
-            f"Created {len(created)} scrobbles",
-            extra={"created_scrobbles": created},
+            f"Last.fm import fnished",
+            extra={
+                "scrobbles_created": len(created),
+                "user_id": self.vrobbler_user,
+                "lastfm_user": self.user,
+            },
         )
         )
         return created
         return created
 
 
@@ -117,30 +120,43 @@ class LastFM:
             mbid = None
             mbid = None
             artist = None
             artist = None
 
 
+            log_dict = {"scrobble": scrobble}
             try:
             try:
                 run_time = int(scrobble.track.get_duration() / 1000)
                 run_time = int(scrobble.track.get_duration() / 1000)
                 mbid = scrobble.track.get_mbid()
                 mbid = scrobble.track.get_mbid()
                 artist = scrobble.track.get_artist().name
                 artist = scrobble.track.get_artist().name
+                log_dict["artist"] = artist
+                log_dict["mbid"] = mbid
+                log_dict["run_time"] = run_time
             except pylast.MalformedResponseError as e:
             except pylast.MalformedResponseError as e:
-                logger.warn(e)
+                logger.warning(e)
             except pylast.WSError as e:
             except pylast.WSError as e:
-                logger.warn(
-                    "LastFM barfed trying to get the track for {scrobble.track}"
+                logger.info(
+                    "LastFM barfed trying to get the track for {scrobble.track}",
+                    extra=log_dict,
                 )
                 )
             except pylast.NetworkError as e:
             except pylast.NetworkError as e:
-                logger.warn(
-                    "LastFM barfed trying to get the track for {scrobble.track}"
+                logger.info(
+                    "LastFM barfed trying to get the track for {scrobble.track}",
+                    extra=log_dict,
                 )
                 )
 
 
             if not artist:
             if not artist:
-                logger.warn(f"Silly LastFM, no artist found for {scrobble}")
+                logger.info(
+                    f"Silly LastFM, no artist found for scrobble",
+                    extra=log_dict,
+                )
                 continue
                 continue
 
 
+            # TODO figure out if this will actually work
+            #timestamp = datetime.fromtimestamp(int(scrobble.timestamp), UTC)
             timestamp = datetime.utcfromtimestamp(
             timestamp = datetime.utcfromtimestamp(
                 int(scrobble.timestamp)
                 int(scrobble.timestamp)
             ).replace(tzinfo=pytz.utc)
             ).replace(tzinfo=pytz.utc)
 
 
-            logger.info(f"{artist},{scrobble.track.title},{timestamp}")
+            logger.info(
+                f"Scrobble appended to list for bulk create", extra=log_dict
+            )
             scrobbles.append(
             scrobbles.append(
                 {
                 {
                     "artist": artist,
                     "artist": artist,