mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-06 19:25:34 +00:00
preset mimetypes
+ add db and api methods to remove tracks from playlists
This commit is contained in:
parent
8b895f5efb
commit
6439b512e9
@ -10,6 +10,7 @@ from PIL import UnidentifiedImageError
|
|||||||
from app import models
|
from app import models
|
||||||
from app.db.sqlite.playlists import SQLitePlaylistMethods
|
from app.db.sqlite.playlists import SQLitePlaylistMethods
|
||||||
from app.lib import playlistlib
|
from app.lib import playlistlib
|
||||||
|
from app.models.track import Track
|
||||||
from app.utils.dates import date_string_to_time_passed, create_new_date
|
from app.utils.dates import date_string_to_time_passed, create_new_date
|
||||||
from app.utils.remove_duplicates import remove_duplicates
|
from app.utils.remove_duplicates import remove_duplicates
|
||||||
|
|
||||||
@ -31,36 +32,44 @@ update_playlist = PL.update_playlist
|
|||||||
delete_playlist = PL.delete_playlist
|
delete_playlist = PL.delete_playlist
|
||||||
|
|
||||||
|
|
||||||
# get_tracks_by_trackhashes = SQLiteTrackMethods.get_tracks_by_trackhashes
|
|
||||||
def duplicate_images(images: list):
|
def duplicate_images(images: list):
|
||||||
if len(images) == 1:
|
if len(images) == 1:
|
||||||
images *= 4
|
images *= 4
|
||||||
elif len(images) == 2:
|
elif len(images) == 2:
|
||||||
images += list(reversed(images))
|
images += list(reversed(images))
|
||||||
elif len(images) == 3:
|
elif len(images) == 3:
|
||||||
images = images + images[:1]
|
images = images + images[:]
|
||||||
|
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
|
||||||
def get_first_4_images(trackhashes: list[str]) -> list[dict['str', str]]:
|
def get_first_4_images(
|
||||||
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
|
tracks: list[Track] = [], trackhashes: list[str] = []
|
||||||
|
) -> list[dict["str", str]]:
|
||||||
|
if len(trackhashes) > 0:
|
||||||
|
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
|
||||||
|
|
||||||
albums = []
|
albums = []
|
||||||
|
|
||||||
for track in tracks:
|
for track in tracks:
|
||||||
if track.albumhash not in albums:
|
if track.albumhash not in albums:
|
||||||
albums.append(track.albumhash)
|
albums.append(track.albumhash)
|
||||||
|
|
||||||
if len(albums) == 4:
|
if len(albums) == 4:
|
||||||
break
|
break
|
||||||
|
|
||||||
albums = AlbumStore.get_albums_by_hashes(albums)
|
albums = AlbumStore.get_albums_by_hashes(albums)
|
||||||
images = [
|
images = [
|
||||||
{
|
{
|
||||||
'image': album.image,
|
"image": album.image,
|
||||||
'color': ''.join(album.colors),
|
"color": "".join(album.colors),
|
||||||
}
|
}
|
||||||
for album in albums
|
for album in albums
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if len(images) == 4:
|
||||||
|
return images
|
||||||
|
|
||||||
return duplicate_images(images)
|
return duplicate_images(images)
|
||||||
|
|
||||||
|
|
||||||
@ -77,8 +86,8 @@ def send_all_playlists():
|
|||||||
|
|
||||||
for playlist in playlists:
|
for playlist in playlists:
|
||||||
if not no_images:
|
if not no_images:
|
||||||
playlist.images = get_first_4_images(playlist.trackhashes)
|
playlist.images = get_first_4_images(trackhashes=playlist.trackhashes)
|
||||||
playlist.images = [img['image'] for img in playlist.images]
|
playlist.images = [img["image"] for img in playlist.images]
|
||||||
|
|
||||||
playlist.clear_lists()
|
playlist.clear_lists()
|
||||||
|
|
||||||
@ -151,6 +160,9 @@ def get_playlist(playlistid: str):
|
|||||||
"""
|
"""
|
||||||
Gets a playlist by id, and if it exists, it gets all the tracks in the playlist and returns them.
|
Gets a playlist by id, and if it exists, it gets all the tracks in the playlist and returns them.
|
||||||
"""
|
"""
|
||||||
|
no_tracks = request.args.get("no_tracks", False)
|
||||||
|
no_tracks = no_tracks == "true"
|
||||||
|
|
||||||
playlist = get_playlist_by_id(int(playlistid))
|
playlist = get_playlist_by_id(int(playlistid))
|
||||||
|
|
||||||
if playlist is None:
|
if playlist is None:
|
||||||
@ -165,15 +177,18 @@ def get_playlist(playlistid: str):
|
|||||||
playlist.set_duration(duration)
|
playlist.set_duration(duration)
|
||||||
|
|
||||||
if not playlist.has_image:
|
if not playlist.has_image:
|
||||||
playlist.images = get_first_4_images(playlist.trackhashes)
|
playlist.images = get_first_4_images(tracks)
|
||||||
|
|
||||||
if len(playlist.images) > 2:
|
if len(playlist.images) > 2:
|
||||||
# swap 3rd image with first (3rd image is the visible image in UI)
|
# swap 3rd image with first (3rd image is the visible image in UI)
|
||||||
playlist.images[2], playlist.images[0] = playlist.images[0], playlist.images[2]
|
playlist.images[2], playlist.images[0] = (
|
||||||
|
playlist.images[0],
|
||||||
|
playlist.images[2],
|
||||||
|
)
|
||||||
|
|
||||||
playlist.clear_lists()
|
playlist.clear_lists()
|
||||||
|
|
||||||
return {"info": playlist, "tracks": tracks}
|
return {"info": playlist, "tracks": tracks if not no_tracks else []}
|
||||||
|
|
||||||
|
|
||||||
@api.route("/playlist/<playlistid>/update", methods=["PUT"])
|
@api.route("/playlist/<playlistid>/update", methods=["PUT"])
|
||||||
@ -267,3 +282,21 @@ def update_image_position(pid: int):
|
|||||||
PL.update_banner_pos(pid, pos)
|
PL.update_banner_pos(pid, pos)
|
||||||
|
|
||||||
return {"msg": "Image position saved"}, 200
|
return {"msg": "Image position saved"}, 200
|
||||||
|
|
||||||
|
|
||||||
|
@api.route("/playlist/<pid>/remove-tracks", methods=["POST"])
|
||||||
|
def remove_tracks_from_playlist(pid: int):
|
||||||
|
data = request.get_json()
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
return {"error": "Track index not provided"}, 400
|
||||||
|
|
||||||
|
# {
|
||||||
|
# trackhash: str;
|
||||||
|
# index: int;
|
||||||
|
# }
|
||||||
|
|
||||||
|
tracks = data["tracks"]
|
||||||
|
PL.remove_tracks_from_playlist(pid, tracks)
|
||||||
|
|
||||||
|
return {"msg": "Done"}, 200
|
||||||
|
@ -176,3 +176,34 @@ class SQLitePlaylistMethods:
|
|||||||
|
|
||||||
with SQLiteManager(userdata_db=True) as cur:
|
with SQLiteManager(userdata_db=True) as cur:
|
||||||
cur.execute(sql, (pos, playlistid))
|
cur.execute(sql, (pos, playlistid))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_tracks_from_playlist(playlistid: int, tracks: list[dict[str, int]]):
|
||||||
|
"""
|
||||||
|
Removes tracks from a playlist by trackhash and position.
|
||||||
|
"""
|
||||||
|
|
||||||
|
sql = """UPDATE playlists SET trackhashes = ? WHERE id = ?"""
|
||||||
|
|
||||||
|
with SQLiteManager(userdata_db=True) as cur:
|
||||||
|
cur.execute("SELECT trackhashes FROM playlists WHERE id = ?", (playlistid,))
|
||||||
|
data = cur.fetchone()
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
trackhashes: list[str] = json.loads(data[0])
|
||||||
|
print(tracks)
|
||||||
|
|
||||||
|
for track in tracks:
|
||||||
|
# {
|
||||||
|
# trackhash: str;
|
||||||
|
# index: int;
|
||||||
|
# }
|
||||||
|
|
||||||
|
index = trackhashes.index(track["trackhash"])
|
||||||
|
|
||||||
|
if index == track["index"]:
|
||||||
|
trackhashes.remove(track["trackhash"])
|
||||||
|
|
||||||
|
cur.execute(sql, (json.dumps(trackhashes), playlistid))
|
||||||
|
@ -69,6 +69,11 @@ class Album:
|
|||||||
if "super_deluxe" in self.versions:
|
if "super_deluxe" in self.versions:
|
||||||
self.versions.remove("deluxe")
|
self.versions.remove("deluxe")
|
||||||
|
|
||||||
|
t_ = "taylors_version"
|
||||||
|
if t_ in self.versions:
|
||||||
|
self.versions.remove(t_)
|
||||||
|
self.versions.insert(0, "taylor's version")
|
||||||
|
|
||||||
self.versions = [v.replace("_", " ") for v in self.versions]
|
self.versions = [v.replace("_", " ") for v in self.versions]
|
||||||
else:
|
else:
|
||||||
self.base_title = get_base_title_and_versions(
|
self.base_title = get_base_title_and_versions(
|
||||||
|
@ -120,12 +120,13 @@ class AlbumVersionEnum(Enum):
|
|||||||
|
|
||||||
Explicit = ("explicit",)
|
Explicit = ("explicit",)
|
||||||
|
|
||||||
ANNIVERSARY = ("anniversary",)
|
ANNIVERSARY_EDITION = ("anniversary",)
|
||||||
DIAMOND = ("diamond",)
|
DIAMOND_EDITION = ("diamond",)
|
||||||
Centennial = ("centennial",)
|
Centennial_EDITION = ("centennial",)
|
||||||
GOLDEN = ("gold",)
|
GOLDEN_EDITION = ("gold",)
|
||||||
PLATINUM = ("platinum",)
|
PLATINUM_EDITION = ("platinum",)
|
||||||
SILVER = ("silver",)
|
SILVER_EDITION = ("silver",)
|
||||||
|
ULTIMATE_EDITION = ("ultimate",)
|
||||||
|
|
||||||
EXPANDED = ("expanded",)
|
EXPANDED = ("expanded",)
|
||||||
EXTENDED = ("extended",)
|
EXTENDED = ("extended",)
|
||||||
@ -134,19 +135,20 @@ class AlbumVersionEnum(Enum):
|
|||||||
SUPER_DELUXE = ("super deluxe",)
|
SUPER_DELUXE = ("super deluxe",)
|
||||||
COMPLETE = ("complete",)
|
COMPLETE = ("complete",)
|
||||||
|
|
||||||
LEGACY = ("legacy",)
|
LEGACY_EDITION = ("legacy",)
|
||||||
SPECIAL = ("special",)
|
SPECIAL_EDITION = ("special",)
|
||||||
COLLECTORS_EDITION = ("collector",)
|
COLLECTORS_EDITION = ("collector",)
|
||||||
ARCHIVE = ("archive",)
|
ARCHIVE_EDITION = ("archive",)
|
||||||
|
|
||||||
Acoustic = ("acoustic",)
|
Acoustic = ("acoustic",)
|
||||||
DOUBLE_DISC = ("double disc", "double disk")
|
DOUBLE_DISC = ("double disc", "double disk")
|
||||||
|
|
||||||
SUMMER = ("summer",)
|
SUMMER_EDITION = ("summer",)
|
||||||
WINTER = ("winter",)
|
WINTER_EDITION = ("winter",)
|
||||||
SPRING = ("spring",)
|
SPRING_EDITION = ("spring",)
|
||||||
FALL = ("fall",)
|
FALL_EDITION = ("fall",)
|
||||||
|
|
||||||
|
BONUS_EDITION = ("bonus",)
|
||||||
BONUS_TRACK = ("bonus track",)
|
BONUS_TRACK = ("bonus track",)
|
||||||
|
|
||||||
ORIGINAL = ("original",)
|
ORIGINAL = ("original",)
|
||||||
@ -154,7 +156,7 @@ class AlbumVersionEnum(Enum):
|
|||||||
UK_VERSION = ("uk version",)
|
UK_VERSION = ("uk version",)
|
||||||
US_VERSION = ("us version",)
|
US_VERSION = ("us version",)
|
||||||
|
|
||||||
Limited = ("limited",)
|
Limited_EDITION = ("limited",)
|
||||||
|
|
||||||
MONO = ("mono",)
|
MONO = ("mono",)
|
||||||
STEREO = ("stereo",)
|
STEREO = ("stereo",)
|
||||||
@ -163,7 +165,8 @@ class AlbumVersionEnum(Enum):
|
|||||||
RE_MIX = ("re-mix",)
|
RE_MIX = ("re-mix",)
|
||||||
RE_RECORDED = ("re-recorded", "rerecorded")
|
RE_RECORDED = ("re-recorded", "rerecorded")
|
||||||
REISSUE = ("reissue",)
|
REISSUE = ("reissue",)
|
||||||
REMASTER = ("remaster",)
|
REMASTERED = ("remaster",)
|
||||||
|
TAYLORS_VERSION = ("taylor's version",)
|
||||||
|
|
||||||
|
|
||||||
def get_anniversary(text: str) -> str | None:
|
def get_anniversary(text: str) -> str | None:
|
||||||
@ -208,7 +211,7 @@ def get_base_title_and_versions(
|
|||||||
Extracts the base album title and version info from an album title string using regex.
|
Extracts the base album title and version info from an album title string using regex.
|
||||||
"""
|
"""
|
||||||
album_title, version_block = get_base_album_title(original_album_title)
|
album_title, version_block = get_base_album_title(original_album_title)
|
||||||
|
|
||||||
if version_block is None:
|
if version_block is None:
|
||||||
return original_album_title, []
|
return original_album_title, []
|
||||||
|
|
||||||
|
13
manage.py
13
manage.py
@ -2,6 +2,7 @@
|
|||||||
This file is used to run the application.
|
This file is used to run the application.
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
from app.api import create_api
|
from app.api import create_api
|
||||||
from app.arg_handler import HandleArgs
|
from app.arg_handler import HandleArgs
|
||||||
@ -14,6 +15,18 @@ from app.utils.filesystem import get_home_res_path
|
|||||||
from app.utils.threading import background
|
from app.utils.threading import background
|
||||||
from alive_progress import config_handler
|
from alive_progress import config_handler
|
||||||
|
|
||||||
|
mimetypes.add_type("text/css", ".css")
|
||||||
|
mimetypes.add_type("text/javascript", ".js")
|
||||||
|
mimetypes.add_type("text/plain", ".txt")
|
||||||
|
mimetypes.add_type("text/html", ".html")
|
||||||
|
mimetypes.add_type("image/webp", ".webp")
|
||||||
|
mimetypes.add_type("image/svg+xml", ".svg")
|
||||||
|
mimetypes.add_type("image/png", ".png")
|
||||||
|
mimetypes.add_type("image/vnd.microsoft.icon", ".ico")
|
||||||
|
mimetypes.add_type("image/gif", ".gif")
|
||||||
|
mimetypes.add_type("font/woff", ".woff")
|
||||||
|
mimetypes.add_type("application/manifest+json", ".webmanifest")
|
||||||
|
|
||||||
werkzeug = logging.getLogger("werkzeug")
|
werkzeug = logging.getLogger("werkzeug")
|
||||||
werkzeug.setLevel(logging.ERROR)
|
werkzeug.setLevel(logging.ERROR)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user