From 27c4924327f1100ef5386d75002baa5a03f15f30 Mon Sep 17 00:00:00 2001 From: mungai-njoroge Date: Sat, 12 Aug 2023 18:23:28 +0300 Subject: [PATCH] add migration to remove the small thumbs path + misc --- README.md | 1 - app/api/album.py | 2 +- app/api/playlist.py | 12 +++++++---- app/lib/populate.py | 5 +++-- app/migrations/__init__.py | 6 +++--- app/migrations/base.py | 7 +------ app/migrations/v1_3_0/__init__.py | 34 +++++++++++++++++++++---------- app/models/album.py | 2 +- app/store/artists.py | 4 ++-- 9 files changed, 42 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 34b3d4d..33b10c3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ enjoy your music library in a web browser. ### For more screenshots, see the [screenshots page on the website](https://swingmusic.vercel.app/screenshots.html). -> This project is in the early stages of development. Many features are missing but will be added with time. ### Setup diff --git a/app/api/album.py b/app/api/album.py index 51e0802..1feb022 100644 --- a/app/api/album.py +++ b/app/api/album.py @@ -96,7 +96,7 @@ def get_album_tracks(albumhash: str): Returns all the tracks in the given album, sorted by disc and track number. """ tracks = TrackStore.get_tracks_by_albumhash(albumhash) - sort_by_track_no(tracks) + tracks = sort_by_track_no(tracks) return {"tracks": tracks} diff --git a/app/api/playlist.py b/app/api/playlist.py index e1e7e5e..c43e3cd 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -142,7 +142,7 @@ def get_album_trackhashes(albumhash: str): """ tracks = TrackStore.get_tracks_by_albumhash(albumhash) tracks = sort_by_track_no(tracks) - + return [t["trackhash"] for t in tracks] @@ -299,7 +299,7 @@ def remove_playlist_image(playlistid: str): if playlist is None: return {"error": "Playlist not found"}, 404 - PL.remove_image(pid) + PL.remove_banner(pid) playlist.image = None playlist.thumb = None @@ -412,18 +412,22 @@ def save_item_as_playlist(): trackhashes = get_album_trackhashes(itemhash) elif itemtype == "artist": trackhashes = get_artist_trackhashes(itemhash) + elif itemtype == "queue": + trackhashes = itemhash.split(",") else: trackhashes = [] if len(trackhashes) == 0: return {"error": "No tracks founds"}, 404 - playlist = insert_playlist(playlist_name, image=itemhash + ".webp") + image = itemhash + ".webp" if itemtype != "folder" and itemtype != "queue" else None + + playlist = insert_playlist(playlist_name, image) if playlist is None: return {"error": "Playlist could not be created"}, 500 - if itemtype != "folder": + if itemtype != "folder" and itemtype != "queue": filename = itemhash + ".webp" base_path = ( diff --git a/app/lib/populate.py b/app/lib/populate.py index bed72a3..cff7504 100644 --- a/app/lib/populate.py +++ b/app/lib/populate.py @@ -8,8 +8,7 @@ from tqdm import tqdm from app import settings from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb -from app.db.sqlite.lastfm.similar_artists import \ - SQLiteLastFMSimilarArtists as lastfmdb +from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb from app.db.sqlite.settings import SettingsSQLMethods as sdb from app.db.sqlite.tracks import SQLiteTrackMethods from app.lib.albumslib import validate_albums @@ -111,6 +110,8 @@ class Populate: if Ping()(): FetchSimilarArtistsLastFM() + ArtistStore.load_artists() + @staticmethod def remove_modified(tracks: Generator[Track, None, None]): """ diff --git a/app/migrations/__init__.py b/app/migrations/__init__.py index 61b61c5..3b7dad8 100644 --- a/app/migrations/__init__.py +++ b/app/migrations/__init__.py @@ -19,6 +19,7 @@ from app.migrations.base import Migration migrations: list[list[Migration]] = [ [ # v1.3.0 + v1_3_0.RemoveSmallThumbnailFolder, v1_3_0.RemovePlaylistArtistHashes, v1_3_0.AddSettingsToPlaylistTable, v1_3_0.AddLastUpdatedToTrackTable, @@ -39,12 +40,11 @@ def apply_migrations(): # run migrations after the previous migration version for migration in migrations[(version - 1) :]: for m in migration: - log.info("Running new migration: %s", m.name) - try: m.migrate() + log.info("Applied migration: %s", m.__name__) except: - log.error("Failed to run migration: %s", m.name) + log.error("Failed to run migration: %s", m.__name__) # bump migration version MigrationManager.set_version(len(migrations)) diff --git a/app/migrations/base.py b/app/migrations/base.py index 30879c2..7002021 100644 --- a/app/migrations/base.py +++ b/app/migrations/base.py @@ -3,14 +3,9 @@ class Migration: Base migration class. """ - name: str - """ - Name of the migration. - """ - @staticmethod def migrate(): """ - Code to run when migrating + Code to run when migrating, override this method. """ pass diff --git a/app/migrations/v1_3_0/__init__.py b/app/migrations/v1_3_0/__init__.py index 547911e..39a51de 100644 --- a/app/migrations/v1_3_0/__init__.py +++ b/app/migrations/v1_3_0/__init__.py @@ -1,11 +1,14 @@ import json -from sqlite3 import OperationalError +import os +import shutil import time from collections import OrderedDict +from sqlite3 import OperationalError from typing import Generator from app.db.sqlite.utils import SQLiteManager from app.migrations.base import Migration +from app.settings import Paths from app.utils.decorators import coroutine from app.utils.hashing import create_hash @@ -20,13 +23,30 @@ from app.utils.hashing import create_hash # 6: trackhashes +class RemoveSmallThumbnailFolder(Migration): + """ + Removes the small thumbnail folder. + + Because we are added a new folder "original" in the same directory, and the small thumbs folder is used to check if an album's thumbnail is already extracted. + + So we need to remove it, to force the app to extract thumbnails for all albums. + """ + + @staticmethod + def migrate(): + path = Paths.get_sm_thumb_path() + + if os.path.exists(path): + shutil.rmtree(path) + + os.mkdir(path) + + class RemovePlaylistArtistHashes(Migration): """ removes the artisthashes column from the playlists table. """ - name = "RemovePlaylistArtistHashes" - @staticmethod def migrate(): # remove artisthashes column @@ -47,8 +67,6 @@ class AddSettingsToPlaylistTable(Migration): to the playlists table. """ - name = "AddSettingsToPlaylistTable" - @staticmethod def migrate(): # existing_playlists = [] @@ -126,8 +144,6 @@ class AddLastUpdatedToTrackTable(Migration): adds the last modified column to the tracks table. """ - name = "AddLastUpdatedToTrackTable" - @staticmethod def migrate(): # add last_mod column and default to current timestamp @@ -148,8 +164,6 @@ class MovePlaylistsAndFavoritesTo10BitHashes(Migration): moves the playlists and favorites to 10 bit hashes. """ - name = "MovePlaylistsAndFavoritesTo10BitHashes" - @staticmethod def migrate(): def get_track_data_by_hash(trackhash: str, tracks: list[tuple]) -> tuple: @@ -257,8 +271,6 @@ class RemoveAllTracks(Migration): removes all tracks from the tracks table. """ - name = "RemoveAllTracks" - @staticmethod def migrate(): sql = "DELETE FROM tracks" diff --git a/app/models/album.py b/app/models/album.py index 4907fab..deeea2b 100644 --- a/app/models/album.py +++ b/app/models/album.py @@ -148,7 +148,7 @@ class Album: """ Checks if the album is a live album. """ - keywords = ["live from", "live at", "live in"] + keywords = ["live from", "live at", "live in", "live on", "mtv unplugged"] for keyword in keywords: if keyword in self.title.lower(): return True diff --git a/app/store/artists.py b/app/store/artists.py index 6f78906..ec610e5 100644 --- a/app/store/artists.py +++ b/app/store/artists.py @@ -23,8 +23,8 @@ class ArtistStore: # db_artists: list[tuple] = list(ardb.get_all_artists()) - for art in tqdm(ardb.get_all_artists(), desc="Loading artists"): - cls.map_artist_color(art) + for artist in tqdm(ardb.get_all_artists(), desc="Loading artists"): + cls.map_artist_color(artist) @classmethod def map_artist_color(cls, artist_tuple: tuple):