From 21df4e0a77495a715d376f49c0a20fab480f1f2f Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 16 Feb 2023 02:27:39 -0500 Subject: [PATCH] Add task to sync with last.fm --- poetry.lock | 2 +- pyproject.toml | 1 + vrobbler/apps/scrobbles/tasks.py | 15 + vrobbler/apps/scrobbles/urls.py | 1 + vrobbler/celery.py | 13 + vrobbler/settings.py | 6 + .../templates/scrobbles/scrobble_list.html | 438 ++++++++++-------- 7 files changed, 275 insertions(+), 201 deletions(-) create mode 100644 vrobbler/apps/scrobbles/tasks.py create mode 100644 vrobbler/celery.py diff --git a/poetry.lock b/poetry.lock index ddb985c..43c93a1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1598,7 +1598,7 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "9d9a0b85a6cb22dd5adba7db19813ef01cf843f229a44d4ea0e2d4b899353d43" +content-hash = "4b71b291b00a768d7d3c253a02faf70249d1f10ba85fcb88fc5c80fecb412332" [metadata.files] amqp = [ diff --git a/pyproject.toml b/pyproject.toml index 28632c5..bf4592f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ pytz = "^2022.7.1" django-redis = "^5.2.0" pylast = "^5.1.0" django-encrypted-field = "^1.0.5" +celery = "^5.2.7" [tool.poetry.dev-dependencies] Werkzeug = "2.0.3" diff --git a/vrobbler/apps/scrobbles/tasks.py b/vrobbler/apps/scrobbles/tasks.py new file mode 100644 index 0000000..88a3769 --- /dev/null +++ b/vrobbler/apps/scrobbles/tasks.py @@ -0,0 +1,15 @@ +import logging +from celery import shared_task + +from scrobbles.models import LastFmImport + +logger = logging.getLogger(__name__) + + +@shared_task +def process_lastfm_import(import_id): + lastfm_import = LastFmImport.objects.filter(id=import_id).first() + if not lastfm_import: + logger.warn(f"LastFmImport not found with id {import_id}") + + lastfm_import.process() diff --git a/vrobbler/apps/scrobbles/urls.py b/vrobbler/apps/scrobbles/urls.py index 8353301..7754349 100644 --- a/vrobbler/apps/scrobbles/urls.py +++ b/vrobbler/apps/scrobbles/urls.py @@ -12,6 +12,7 @@ urlpatterns = [ views.AudioScrobblerImportCreateView.as_view(), name='audioscrobbler-file-upload', ), + path('lastfm-import/', views.lastfm_import, name='lastfm-import'), path('jellyfin/', views.jellyfin_websocket, name='jellyfin-websocket'), path('mopidy/', views.mopidy_websocket, name='mopidy-websocket'), path('export/', views.export, name='export'), diff --git a/vrobbler/celery.py b/vrobbler/celery.py new file mode 100644 index 0000000..7636f64 --- /dev/null +++ b/vrobbler/celery.py @@ -0,0 +1,13 @@ +import os + +from celery import Celery + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vrobbler.settings") +app = Celery() +app.config_from_object("django.conf:settings", namespace="CELERY") +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print(f"Request: {self.request!r}") diff --git a/vrobbler/settings.py b/vrobbler/settings.py index 1d473e8..6f11aa9 100644 --- a/vrobbler/settings.py +++ b/vrobbler/settings.py @@ -72,6 +72,12 @@ X_FRAME_OPTIONS = "SAMEORIGIN" REDIS_URL = os.getenv("VROBBLER_REDIS_URL", None) +CELERY_TASK_ALWAYS_EAGER = os.getenv("VROBBLER_SKIP_CELERY", False) +CELERY_BROKER_URL = REDIS_URL if REDIS_URL else "memory://localhost/" +CELERY_RESULT_BACKEND = "django-db" +CELERY_TIMEZONE = os.getenv("VROBBLER_TIME_ZONE", "US/Eastern") +CELERY_TASK_TRACK_STARTED = True + INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", diff --git a/vrobbler/templates/scrobbles/scrobble_list.html b/vrobbler/templates/scrobbles/scrobble_list.html index 2871b90..2b55836 100644 --- a/vrobbler/templates/scrobbles/scrobble_list.html +++ b/vrobbler/templates/scrobbles/scrobble_list.html @@ -4,17 +4,27 @@ {% block content %}
-
+

Dashboard

{% if user.is_authenticated %}
- - + {% if user.profile.lastfm_username %} + +
+ +
+ {% endif %} + +
{% endif %}
-