From 4eb8289e5555857d7e5192824221f2ef945cb6a9 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 25 Jul 2025 22:53:31 -0400 Subject: [PATCH] [scrobbles] LastFM only creates import if there are imports --- vrobbler/apps/scrobbles/importers/lastfm.py | 5 +++- vrobbler/apps/scrobbles/utils.py | 27 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/vrobbler/apps/scrobbles/importers/lastfm.py b/vrobbler/apps/scrobbles/importers/lastfm.py index 23a8d36..05d1c6d 100644 --- a/vrobbler/apps/scrobbles/importers/lastfm.py +++ b/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 diff --git a/vrobbler/apps/scrobbles/utils.py b/vrobbler/apps/scrobbles/utils.py index 076f584..fee5093 100644 --- a/vrobbler/apps/scrobbles/utils.py +++ b/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 )