Jelajahi Sumber

Add command for running lastfm imports

Colin Powell 2 tahun lalu
induk
melakukan
59e29d858a

+ 0 - 0
vrobbler/apps/scrobbles/management/__init__.py


+ 0 - 0
vrobbler/apps/scrobbles/management/commands/__init__.py


+ 7 - 0
vrobbler/apps/scrobbles/management/commands/import_from_lastfm.py

@@ -0,0 +1,7 @@
+from django.core.management.base import BaseCommand, no_translations
+from vrobbler.apps.scrobbles.utils import import_lastfm_for_all_users
+
+
+class Command(BaseCommand):
+    def handle(self, *args, **options):
+        import_lastfm_for_all_users()

+ 6 - 5
vrobbler/apps/scrobbles/tasks.py

@@ -1,12 +1,8 @@
 import logging
 
 from celery import shared_task
+from django.apps import apps
 from django.contrib.auth import get_user_model
-from scrobbles.models import (
-    AudioScrobblerTSVImport,
-    KoReaderImport,
-    LastFmImport,
-)
 from scrobbles.stats import build_yesterdays_charts_for_user
 
 logger = logging.getLogger(__name__)
@@ -15,6 +11,7 @@ User = get_user_model()
 
 @shared_task
 def process_lastfm_import(import_id):
+    LastFmImport = apps.get_model("scrobbles", "LastFMImport")
     lastfm_import = LastFmImport.objects.filter(id=import_id).first()
     if not lastfm_import:
         logger.warn(f"LastFmImport not found with id {import_id}")
@@ -24,6 +21,9 @@ def process_lastfm_import(import_id):
 
 @shared_task
 def process_tsv_import(import_id):
+    AudioScrobblerTSVImport = apps.get_model(
+        "scrobbles", "AudioscrobblerTSVImport"
+    )
     tsv_import = AudioScrobblerTSVImport.objects.filter(id=import_id).first()
     if not tsv_import:
         logger.warn(f"AudioScrobblerTSVImport not found with id {import_id}")
@@ -33,6 +33,7 @@ def process_tsv_import(import_id):
 
 @shared_task
 def process_koreader_import(import_id):
+    KoReaderImport = apps.get_model("scrobbles", "KoReaderImport")
     koreader_import = KoReaderImport.objects.filter(id=import_id).first()
     if not koreader_import:
         logger.warn(f"KOReaderImport not found with id {import_id}")

+ 24 - 4
vrobbler/apps/scrobbles/utils.py

@@ -1,15 +1,16 @@
 import logging
 from urllib.parse import unquote
 
-from django.utils import timezone
 from dateutil.parser import ParserError, parse
 from django.apps import apps
 from django.conf import settings
 from django.contrib.auth import get_user_model
 from django.db import models
-from vrobbler.apps.profiles.utils import now_user_timezone
-
-from vrobbler.apps.scrobbles.constants import LONG_PLAY_MEDIA
+from django.utils import timezone
+from profiles.models import UserProfile
+from profiles.utils import now_user_timezone
+from scrobbles.constants import LONG_PLAY_MEDIA
+from scrobbles.tasks import process_lastfm_import
 
 logger = logging.getLogger(__name__)
 User = get_user_model()
@@ -190,3 +191,22 @@ def get_long_plays_completed(user: User) -> list:
             ):
                 media_list.append(media)
     return media_list
+
+
+def import_lastfm_for_all_users():
+    """Grab a list of all users with LastFM enabled and kickoff imports for them"""
+    LastFmImport = apps.get_model("scrobbles", "LastFMImport")
+    lastfm_enabled_user_ids = UserProfile.objects.filter(
+        lastfm_username__isnull=False, lastfm_password__isnull=False
+    ).values_list("user_id", flat=True)
+
+    for user_id in lastfm_enabled_user_ids:
+        lfm_import, created = LastFmImport.objects.get_or_create(
+            user_id=user_id, processed_finished__isnull=True
+        )
+        if not created:
+            logger.info(
+                f"Not resuming failed LastFM import {lfm_import.id} for user {user_id}"
+            )
+            continue
+        process_lastfm_import.delay(lfm_import.id)