mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 12:33:03 +00:00
refactor most things to use the database directly
This commit is contained in:
@@ -3,25 +3,10 @@ This module contains all the Flask Blueprints and API routes. It also contains a
|
||||
that are used through-out the app. It handles the initialization of the watchdog,
|
||||
checking and creating config dirs and starting the re-indexing process using a background thread.
|
||||
"""
|
||||
from typing import List
|
||||
from typing import Set
|
||||
|
||||
from app import functions
|
||||
from app import helpers
|
||||
from app import instances
|
||||
from app import models
|
||||
from app import prep
|
||||
from app.lib import albumslib
|
||||
from app.lib import folderslib
|
||||
from app.lib import playlistlib
|
||||
|
||||
DB_TRACKS = instances.tracks_instance.get_all_tracks()
|
||||
VALID_FOLDERS: Set[str] = set()
|
||||
|
||||
ALBUMS: List[models.Album] = []
|
||||
TRACKS: List[models.Track] = []
|
||||
PLAYLISTS: List[models.Playlist] = []
|
||||
FOLDERS: List[models.Folder] = List
|
||||
|
||||
|
||||
@helpers.background
|
||||
@@ -31,9 +16,6 @@ def initialize() -> None:
|
||||
"""
|
||||
functions.start_watchdog()
|
||||
prep.create_config_dir()
|
||||
albumslib.create_everything()
|
||||
folderslib.run_scandir()
|
||||
playlistlib.create_all_playlists()
|
||||
functions.reindex_tracks()
|
||||
|
||||
|
||||
|
||||
+20
-22
@@ -1,16 +1,17 @@
|
||||
"""
|
||||
Contains all the album routes.
|
||||
"""
|
||||
from pprint import pprint
|
||||
from typing import List
|
||||
|
||||
from app import api
|
||||
from app import helpers
|
||||
from app import models
|
||||
from app.lib import albumslib
|
||||
from app.lib import trackslib
|
||||
from flask import Blueprint
|
||||
from flask import request
|
||||
from app.functions import FetchAlbumBio
|
||||
from app import instances
|
||||
|
||||
album_bp = Blueprint("album", __name__, url_prefix="")
|
||||
|
||||
@@ -35,31 +36,31 @@ def get_albums():
|
||||
return {"albums": albums}
|
||||
|
||||
|
||||
@album_bp.route("/album/tracks", methods=["POST"])
|
||||
@album_bp.route("/album", methods=["POST"])
|
||||
def get_album():
|
||||
"""Returns all the tracks in the given album."""
|
||||
data = request.get_json()
|
||||
|
||||
album = data["album"]
|
||||
artist = data["artist"]
|
||||
|
||||
songs = trackslib.get_album_tracks(album, artist)
|
||||
album, artist = data["album"], data["artist"]
|
||||
albumhash = helpers.create_album_hash(album, artist)
|
||||
index = albumslib.find_album(api.ALBUMS, albumhash)
|
||||
album: models.Album = api.ALBUMS[index]
|
||||
|
||||
album.count = len(songs)
|
||||
album.duration = albumslib.get_album_duration(songs)
|
||||
tracks = instances.tracks_instance.find_tracks_by_hash(albumhash)
|
||||
tracks = [models.Track(t) for t in tracks]
|
||||
|
||||
album = instances.album_instance.find_album_by_hash(albumhash)
|
||||
album = models.Album(album)
|
||||
|
||||
album.count = len(tracks)
|
||||
album.duration = albumslib.get_album_duration(tracks)
|
||||
|
||||
if (
|
||||
album.count == 1
|
||||
and songs[0].title == album.title
|
||||
and songs[0].tracknumber == 1
|
||||
and songs[0].disknumber == 1
|
||||
and tracks[0].title == album.title
|
||||
and tracks[0].tracknumber == 1
|
||||
and tracks[0].disknumber == 1
|
||||
):
|
||||
album.is_single = True
|
||||
|
||||
return {"songs": songs, "info": album}
|
||||
return {"tracks": tracks, "info": album}
|
||||
|
||||
|
||||
@album_bp.route("/album/bio", methods=["POST"])
|
||||
@@ -80,14 +81,11 @@ def get_albumartists():
|
||||
"""Returns a list of artists featured in a given album."""
|
||||
data = request.get_json()
|
||||
|
||||
album = data["album"]
|
||||
artist = data["artist"]
|
||||
album, artist = data["album"], data["artist"]
|
||||
albumhash = helpers.create_album_hash(album, artist)
|
||||
|
||||
tracks: List[models.Track] = []
|
||||
|
||||
for track in api.TRACKS:
|
||||
if track.album == album and track.albumartist == artist:
|
||||
tracks.append(track)
|
||||
tracks = instances.tracks_instance.find_tracks_by_hash(albumhash)
|
||||
tracks = [models.Track(t) for t in tracks]
|
||||
|
||||
artists = []
|
||||
|
||||
|
||||
+32
-33
@@ -22,8 +22,12 @@ TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist
|
||||
|
||||
@playlist_bp.route("/playlists", methods=["GET"])
|
||||
def get_all_playlists():
|
||||
"""Returns all the playlists."""
|
||||
dbplaylists = instances.playlist_instance.get_all_playlists()
|
||||
dbplaylists = [models.Playlist(p) for p in dbplaylists]
|
||||
|
||||
playlists = [
|
||||
serializer.Playlist(p, construct_last_updated=False) for p in api.PLAYLISTS
|
||||
serializer.Playlist(p, construct_last_updated=False) for p in dbplaylists
|
||||
]
|
||||
playlists.sort(
|
||||
key=lambda p: datetime.strptime(p.lastUpdated, "%Y-%m-%d %H:%M:%S"),
|
||||
@@ -36,7 +40,7 @@ def get_all_playlists():
|
||||
def create_playlist():
|
||||
data = request.get_json()
|
||||
|
||||
playlist = {
|
||||
data = {
|
||||
"name": data["name"],
|
||||
"description": "",
|
||||
"pre_tracks": [],
|
||||
@@ -45,21 +49,16 @@ def create_playlist():
|
||||
"thumb": "",
|
||||
}
|
||||
|
||||
try:
|
||||
for pl in api.PLAYLISTS:
|
||||
if pl.name == playlist["name"]:
|
||||
raise PlaylistExists("Playlist already exists.")
|
||||
dbp = instances.playlist_instance.get_playlist_by_name(data["name"])
|
||||
|
||||
except PlaylistExists as e:
|
||||
return {"error": str(e)}, 409
|
||||
if dbp is not None:
|
||||
return {"message": "Playlist already exists."}, 409
|
||||
|
||||
upsert_id = instances.playlist_instance.insert_playlist(playlist)
|
||||
upsert_id = instances.playlist_instance.insert_playlist(data)
|
||||
p = instances.playlist_instance.get_playlist_by_id(upsert_id)
|
||||
pp = models.Playlist(p)
|
||||
playlist = models.Playlist(p)
|
||||
|
||||
api.PLAYLISTS.append(pp)
|
||||
|
||||
return {"playlist": pp}, 201
|
||||
return {"playlist": playlist}, 201
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlist_id>/add", methods=["POST"])
|
||||
@@ -70,22 +69,22 @@ def add_track_to_playlist(playlist_id: str):
|
||||
|
||||
try:
|
||||
playlistlib.add_track(playlist_id, trackid)
|
||||
except TrackExistsInPlaylist as e:
|
||||
except TrackExistsInPlaylist:
|
||||
return {"error": "Track already exists in playlist"}, 409
|
||||
|
||||
return {"msg": "I think It's done"}, 200
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlistid>")
|
||||
def get_single_p_info(playlistid: str):
|
||||
p = UseBisection(api.PLAYLISTS, "playlistid", [playlistid])()
|
||||
playlist: models.Playlist = p[0]
|
||||
def get_playlist(playlistid: str):
|
||||
p = instances.playlist_instance.get_playlist_by_id(playlistid)
|
||||
if p is None:
|
||||
return {"info": {}, "tracks": []}
|
||||
|
||||
if playlist is not None:
|
||||
tracks = playlist.get_tracks()
|
||||
return {"info": serializer.Playlist(playlist), "tracks": tracks}
|
||||
playlist = models.Playlist(p)
|
||||
|
||||
return {"info": {}, "tracks": []}
|
||||
tracks = playlistlib.create_playlist_tracks(playlist.pretracks)
|
||||
return {"info": serializer.Playlist(playlist), "tracks": tracks}
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlistid>/update", methods=["PUT"])
|
||||
@@ -109,21 +108,21 @@ def update_playlist(playlistid: str):
|
||||
p: models.Playlist = p[0]
|
||||
|
||||
if playlist is not None:
|
||||
if image:
|
||||
image_, thumb_ = playlistlib.save_p_image(image, playlistid)
|
||||
playlist["image"] = image_
|
||||
playlist["thumb"] = thumb_
|
||||
if image:
|
||||
image_, thumb_ = playlistlib.save_p_image(image, playlistid)
|
||||
playlist["image"] = image_
|
||||
playlist["thumb"] = thumb_
|
||||
|
||||
else:
|
||||
playlist["image"] = p.image.split("/")[-1]
|
||||
playlist["thumb"] = p.thumb.split("/")[-1]
|
||||
else:
|
||||
playlist["image"] = p.image.split("/")[-1]
|
||||
playlist["thumb"] = p.thumb.split("/")[-1]
|
||||
|
||||
p.update_playlist(playlist)
|
||||
instances.playlist_instance.update_playlist(playlistid, playlist)
|
||||
p.update_playlist(playlist)
|
||||
instances.playlist_instance.update_playlist(playlistid, playlist)
|
||||
|
||||
return {
|
||||
"data": serializer.Playlist(p),
|
||||
}
|
||||
return {
|
||||
"data": serializer.Playlist(p),
|
||||
}
|
||||
|
||||
return {"msg": "Something shady happened"}, 500
|
||||
|
||||
|
||||
@@ -95,18 +95,9 @@ def search():
|
||||
|
||||
return {
|
||||
"data": [
|
||||
{
|
||||
"tracks": tracks[:5],
|
||||
"more": len(tracks) > 5
|
||||
},
|
||||
{
|
||||
"albums": albums[:6],
|
||||
"more": len(albums) > 6
|
||||
},
|
||||
{
|
||||
"artists": artists_dicts[:6],
|
||||
"more": len(artists_dicts) > 6
|
||||
},
|
||||
{"tracks": tracks[:5], "more": len(tracks) > 5},
|
||||
{"albums": albums[:6], "more": len(albums) > 6},
|
||||
{"artists": artists_dicts[:6], "more": len(artists_dicts) > 6},
|
||||
]
|
||||
}
|
||||
|
||||
@@ -121,18 +112,18 @@ def search_load_more():
|
||||
|
||||
if type == "tracks":
|
||||
return {
|
||||
"tracks": SEARCH_RESULTS["tracks"][index:index + 5],
|
||||
"tracks": SEARCH_RESULTS["tracks"][index : index + 5],
|
||||
"more": len(SEARCH_RESULTS["tracks"]) > index + 5,
|
||||
}
|
||||
|
||||
elif type == "albums":
|
||||
return {
|
||||
"albums": SEARCH_RESULTS["albums"][index:index + 6],
|
||||
"albums": SEARCH_RESULTS["albums"][index : index + 6],
|
||||
"more": len(SEARCH_RESULTS["albums"]) > index + 6,
|
||||
}
|
||||
|
||||
elif type == "artists":
|
||||
return {
|
||||
"artists": SEARCH_RESULTS["artists"][index:index + 6],
|
||||
"artists": SEARCH_RESULTS["artists"][index : index + 6],
|
||||
"more": len(SEARCH_RESULTS["artists"]) > index + 6,
|
||||
}
|
||||
|
||||
+8
-12
@@ -6,6 +6,8 @@ from app import instances
|
||||
from flask import Blueprint
|
||||
from flask import send_file
|
||||
|
||||
from app import models
|
||||
|
||||
track_bp = Blueprint("track", __name__, url_prefix="/")
|
||||
|
||||
|
||||
@@ -14,21 +16,15 @@ def send_track_file(trackid):
|
||||
"""
|
||||
Returns an audio file that matches the passed id to the client.
|
||||
"""
|
||||
try:
|
||||
files = []
|
||||
for f in api.DB_TRACKS:
|
||||
try:
|
||||
if f["_id"]["$oid"] == trackid:
|
||||
files.append(f["filepath"])
|
||||
except KeyError:
|
||||
# Bug: some albums are not found although they exist in `api.ALBUMS`. It has something to do with the bisection method used or sorting. Not sure yet.
|
||||
pass
|
||||
track = instances.tracks_instance.get_track_by_id(trackid)
|
||||
|
||||
filepath = files[0]
|
||||
except IndexError:
|
||||
if track is None:
|
||||
return "File not found", 404
|
||||
|
||||
return send_file(filepath, mimetype="audio/mp3")
|
||||
track = models.Track(track)
|
||||
type = track.filepath.split(".")[-1]
|
||||
|
||||
return send_file(track.filepath, mimetype=f"audio/{type}")
|
||||
|
||||
|
||||
@track_bp.route("/sample")
|
||||
|
||||
Reference in New Issue
Block a user