Fix LastFM and add UI for KoReader

This commit is contained in:
2023-02-20 02:06:17 -05:00
parent 6ef8238442
commit 5d315b4834
6 changed files with 57 additions and 16 deletions

View File

@ -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() name = re.split("feat.", name, flags=re.IGNORECASE)[0].strip()
if 'featuring' in name.lower(): if 'featuring' in name.lower():
name = re.split("featuring", name, flags=re.IGNORECASE)[0].strip() name = re.split("featuring", name, flags=re.IGNORECASE)[0].strip()
artist_dict = lookup_artist_from_mb(name) artist_dict = lookup_artist_from_mb(name)
mbid = mbid or artist_dict['id'] 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']}") logger.debug(f"Cleaning artist {name} with {artist_dict['name']}")
# Clean up bad names in our DB with MB names # Clean up bad names in our DB with MB names
if artist.name != artist_dict['name']: # if artist.name != artist_dict["name"]:
artist.name = artist_dict["name"] # artist.name = artist_dict["name"]
artist.save(update_fields=["name"]) # artist.save(update_fields=["name"])
return artist return artist

View File

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

View File

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

View File

@ -35,7 +35,12 @@ from scrobbles.constants import (
from scrobbles.export import export_scrobbles from scrobbles.export import export_scrobbles
from scrobbles.forms import ExportScrobbleForm, ScrobbleForm from scrobbles.forms import ExportScrobbleForm, ScrobbleForm
from scrobbles.imdb import lookup_video_from_imdb 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 ( from scrobbles.scrobblers import (
jellyfin_scrobble_track, jellyfin_scrobble_track,
jellyfin_scrobble_video, jellyfin_scrobble_video,
@ -48,7 +53,11 @@ from scrobbles.serializers import (
AudioScrobblerTSVImportSerializer, AudioScrobblerTSVImportSerializer,
ScrobbleSerializer, 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 from scrobbles.thesportsdb import lookup_event_from_thesportsdb
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -177,6 +186,22 @@ class AudioScrobblerImportCreateView(
return HttpResponseRedirect(self.get_success_url()) 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]) @permission_classes([IsAuthenticated])
@api_view(['GET']) @api_view(['GET'])
def lastfm_import(request): def lastfm_import(request):

View File

@ -326,7 +326,18 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="submit" class="btn btn-primary">Import</button> <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> </div>
</form> </form>
</div> </div>

View File

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