Pārlūkot izejas kodu

Fix LastFM and add UI for KoReader

Colin Powell 2 gadi atpakaļ
vecāks
revīzija
5d315b4834

+ 4 - 3
vrobbler/apps/music/utils.py

@@ -20,6 +20,7 @@ def get_or_create_artist(name: str, mbid: str = None) -> Artist:
         name = re.split("feat.", name, flags=re.IGNORECASE)[0].strip()
     if 'featuring' in name.lower():
         name = re.split("featuring", name, flags=re.IGNORECASE)[0].strip()
+
     artist_dict = lookup_artist_from_mb(name)
     mbid = mbid or artist_dict['id']
 
@@ -30,9 +31,9 @@ def get_or_create_artist(name: str, mbid: str = None) -> Artist:
 
     logger.debug(f"Cleaning artist {name} with {artist_dict['name']}")
     # Clean up bad names in our DB with MB names
-    if artist.name != artist_dict['name']:
-        artist.name = artist_dict["name"]
-        artist.save(update_fields=["name"])
+    # if artist.name != artist_dict["name"]:
+    #    artist.name = artist_dict["name"]
+    #    artist.save(update_fields=["name"])
 
     return artist
 

+ 8 - 9
vrobbler/apps/scrobbles/lastfm.py

@@ -47,17 +47,17 @@ class LastFM:
         new_scrobbles = []
         source = "Last.fm"
         source_id = ""
-        latest_scrobbles = self.get_last_scrobbles(time_from=last_processed)
+        lastfm_scrobbles = self.get_last_scrobbles(time_from=last_processed)
 
-        for scrobble in latest_scrobbles:
-            timestamp = scrobble.pop('timestamp')
+        for lfm_scrobble in lastfm_scrobbles:
+            timestamp = lfm_scrobble.pop('timestamp')
 
-            artist = get_or_create_artist(scrobble.pop('artist'))
-            album = get_or_create_album(scrobble.pop('album'), artist)
+            artist = get_or_create_artist(lfm_scrobble.pop('artist'))
+            album = get_or_create_album(lfm_scrobble.pop('album'), artist)
 
-            scrobble['artist'] = artist
-            scrobble['album'] = album
-            track = get_or_create_track(**scrobble)
+            lfm_scrobble['artist'] = artist
+            lfm_scrobble['album'] = album
+            track = get_or_create_track(**lfm_scrobble)
 
             new_scrobble = Scrobble(
                 user=self.vrobbler_user,
@@ -134,7 +134,6 @@ class LastFM:
             logger.info(f"{artist},{scrobble.track.title},{timestamp}")
             scrobbles.append(
                 {
-                    "user": self.vrobbler_user,
                     "artist": artist,
                     "album": scrobble.album,
                     "title": scrobble.track.title,

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

@@ -81,7 +81,7 @@ class BaseFileImportMixin(TimeStampedModel):
         self.process_log = ""
         if not scrobbles:
             self.process_count = 0
-            self.save(update_fields=["process_log" "process_count"])
+            self.save(update_fields=["process_log", "process_count"])
             return
 
         for count, scrobble in enumerate(scrobbles):

+ 27 - 2
vrobbler/apps/scrobbles/views.py

@@ -35,7 +35,12 @@ from scrobbles.constants import (
 from scrobbles.export import export_scrobbles
 from scrobbles.forms import ExportScrobbleForm, ScrobbleForm
 from scrobbles.imdb import lookup_video_from_imdb
-from scrobbles.models import AudioScrobblerTSVImport, LastFmImport, Scrobble
+from scrobbles.models import (
+    AudioScrobblerTSVImport,
+    KoReaderImport,
+    LastFmImport,
+    Scrobble,
+)
 from scrobbles.scrobblers import (
     jellyfin_scrobble_track,
     jellyfin_scrobble_video,
@@ -48,7 +53,11 @@ from scrobbles.serializers import (
     AudioScrobblerTSVImportSerializer,
     ScrobbleSerializer,
 )
-from scrobbles.tasks import process_lastfm_import, process_tsv_import
+from scrobbles.tasks import (
+    process_koreader_import,
+    process_lastfm_import,
+    process_tsv_import,
+)
 from scrobbles.thesportsdb import lookup_event_from_thesportsdb
 
 logger = logging.getLogger(__name__)
@@ -177,6 +186,22 @@ class AudioScrobblerImportCreateView(
         return HttpResponseRedirect(self.get_success_url())
 
 
+class KoReaderImportCreateView(
+    LoginRequiredMixin, JsonableResponseMixin, CreateView
+):
+    model = KoReaderImport
+    fields = ['sqlite_file']
+    template_name = 'scrobbles/upload_form.html'
+    success_url = reverse_lazy('vrobbler-home')
+
+    def form_valid(self, form):
+        self.object = form.save(commit=False)
+        self.object.user = self.request.user
+        self.object.save()
+        process_koreader_import.delay(self.object.id)
+        return HttpResponseRedirect(self.get_success_url())
+
+
 @permission_classes([IsAuthenticated])
 @api_view(['GET'])
 def lastfm_import(request):

+ 12 - 1
vrobbler/templates/scrobbles/scrobble_list.html

@@ -326,7 +326,18 @@
                 </div>
                 <div class="modal-footer">
                     <button type="submit" class="btn btn-primary">Import</button>
-                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
+                </div>
+            </form>
+            <form action="{% url 'koreader-file-upload' %}" method="post" enctype="multipart/form-data">
+                <div class="modal-body">
+                    {% csrf_token %}
+                    <div class="form-group">
+                        <label for="tsv_file" class="col-form-label">KOReader sqlite file:</label>
+                        <input type="file" name="sqlite_file" class="form-control" id="id_sqlite_file">
+                    </div>
+                </div>
+                <div class="modal-footer">
+                    <button type="submit" class="btn btn-primary">Import</button>
                 </div>
             </form>
         </div>

+ 5 - 0
vrobbler/urls.py

@@ -24,6 +24,11 @@ urlpatterns = [
         scrobbles_views.AudioScrobblerImportCreateView.as_view(),
         name='audioscrobbler-file-upload',
     ),
+    path(
+        'manual/koreader/',
+        scrobbles_views.KoReaderImportCreateView.as_view(),
+        name='koreader-file-upload',
+    ),
     path("", include(music_urls, namespace="music")),
     path("", include(video_urls, namespace="videos")),
     path(