Ver Fonte

Fix user timezones in scrobbler files

Colin Powell há 2 anos atrás
pai
commit
8f9c825903
2 ficheiros alterados com 10 adições e 6 exclusões
  1. 3 1
      vrobbler/apps/scrobbles/models.py
  2. 7 5
      vrobbler/apps/scrobbles/tsv.py

+ 3 - 1
vrobbler/apps/scrobbles/models.py

@@ -52,7 +52,9 @@ class AudioScrobblerTSVImport(TimeStampedModel):
         tz = None
         if self.user:
             tz = self.user.profile.tzinfo
-        scrobbles = process_audioscrobbler_tsv_file(self.tsv_file.path, tz=tz)
+        scrobbles = process_audioscrobbler_tsv_file(
+            self.tsv_file.path, user_tz=tz
+        )
         if scrobbles:
             self.process_log = f"Created {len(scrobbles)} scrobbles"
             for scrobble in scrobbles:

+ 7 - 5
vrobbler/apps/scrobbles/tsv.py

@@ -9,11 +9,11 @@ from scrobbles.models import Scrobble
 logger = logging.getLogger(__name__)
 
 
-def process_audioscrobbler_tsv_file(file_path, tz=None):
+def process_audioscrobbler_tsv_file(file_path, user_tz=None):
     """Takes a path to a file of TSV data and imports it as past scrobbles"""
     new_scrobbles = []
-    if not tz:
-        tz = pytz.utc
+    if not user_tz:
+        user_tz = pytz.utc
 
     with open(file_path) as infile:
         source = 'Audioscrobbler File'
@@ -71,8 +71,10 @@ def process_audioscrobbler_tsv_file(file_path, tz=None):
                 track.musicbrainz_id = row[7]
                 track.save()
 
-            timestamp = datetime.utcfromtimestamp(int(row[6])).replace(
-                tzinfo=tz
+            timestamp = (
+                datetime.utcfromtimestamp(int(row[6]))
+                .replace(tzinfo=user_tz)
+                .astimezone(pytz.utc)
             )
             source = 'Audioscrobbler File'