Bladeren bron

[scrobbles] LastFM only creates import if there are imports

Colin Powell 1 dag geleden
bovenliggende
commit
4eb8289e55
2 gewijzigde bestanden met toevoegingen van 31 en 1 verwijderingen
  1. 4 1
      vrobbler/apps/scrobbles/importers/lastfm.py
  2. 27 0
      vrobbler/apps/scrobbles/utils.py

+ 4 - 1
vrobbler/apps/scrobbles/importers/lastfm.py

@@ -93,7 +93,7 @@ class LastFM:
         )
         return created
 
-    def get_last_scrobbles(self, time_from=None, time_to=None):
+    def get_last_scrobbles(self, time_from=None, time_to=None, check=False):
         """Given a user, Last.fm api key, and secret key, grab a list of scrobbled
         tracks"""
         lfm_params = {}
@@ -109,6 +109,9 @@ class LastFM:
         found_scrobbles = self.user.get_recent_tracks(**lfm_params)
         # TOOD spin this out into a celery task over certain threshold of found scrobbles?
 
+        if check and found_scrobbles:
+            return True
+
         for scrobble in found_scrobbles:
             logger.info(f"Processing {scrobble}")
             run_time = None

+ 27 - 0
vrobbler/apps/scrobbles/utils.py

@@ -114,6 +114,8 @@ def get_long_plays_completed(user: User) -> list:
 
 def import_lastfm_for_all_users(restart=False):
     """Grab a list of all users with LastFM enabled and kickoff imports for them"""
+    from scrobbles.importers.lastfm import LastFM
+
     LastFmImport = apps.get_model("scrobbles", "LastFMImport")
     lastfm_enabled_user_ids = UserProfile.objects.filter(
         lastfm_username__isnull=False,
@@ -124,6 +126,31 @@ def import_lastfm_for_all_users(restart=False):
     lastfm_import_count = 0
 
     for user_id in lastfm_enabled_user_ids:
+
+        lfm_import = LastFmImport.objects.filter(
+            user_id=user_id, processed_finished__isnull=False
+        ).last()
+        if lfm_import:
+            last_processed = lfm_import.processed_finished
+        else:
+            logger.info(
+                f"Not resuming failed LastFM import {lfm_import.id} for user {user_id}, use restart=True to restart"
+                "No existing LastFM import, we should start a monthly parsing of lastFm for this user going back to 2002"
+            )
+            continue
+
+        lfm_client = LastFM(
+            user=get_user_model().objects.filter(id=user_id).first()
+        )
+
+        has_scrobbles = lfm_client.get_last_scrobbles(
+            time_from=last_processed, check=True
+        )
+
+        if not has_scrobbles:
+            logger.info("No new scrobbles to import from LastFM")
+            continue
+
         lfm_import, created = LastFmImport.objects.get_or_create(
             user_id=user_id, processed_finished__isnull=True
         )