mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-07 03:35:35 +00:00
server: move all stores to api.py
This commit is contained in:
parent
29124ce717
commit
f8710a1596
@ -1,21 +1,20 @@
|
||||
from typing import List
|
||||
from app import models, functions
|
||||
from app import trackslib
|
||||
|
||||
ALBUMS: List[models.Album] = []
|
||||
from app import models, functions, helpers
|
||||
from app import trackslib, api
|
||||
|
||||
|
||||
def create_all_albums() -> List[models.Track]:
|
||||
@helpers.background
|
||||
def create_everything() -> List[models.Track]:
|
||||
"""
|
||||
Creates album objects for all albums and returns
|
||||
a list of track objects
|
||||
"""
|
||||
albums: list[models.Album] = functions.get_all_albums()
|
||||
|
||||
ALBUMS.clear()
|
||||
ALBUMS.extend(albums)
|
||||
api.ALBUMS.clear()
|
||||
api.ALBUMS.extend(albums)
|
||||
trackslib.create_all_tracks()
|
||||
return trackslib.TRACKS
|
||||
|
||||
|
||||
|
||||
def get_album_duration(album: list) -> int:
|
||||
@ -46,7 +45,7 @@ def get_album_image(album: list) -> str:
|
||||
|
||||
|
||||
def find_album(albumtitle, artist):
|
||||
for album in ALBUMS:
|
||||
for album in api.ALBUMS:
|
||||
if album.album == albumtitle and album.artist == artist:
|
||||
return album
|
||||
|
||||
@ -58,11 +57,11 @@ def search_albums_by_name(query: str) -> List[models.Album]:
|
||||
title_albums: List[models.Album] = []
|
||||
artist_albums: List[models.Album] = []
|
||||
|
||||
for album in ALBUMS:
|
||||
for album in api.ALBUMS:
|
||||
if query.lower() in album.album.lower():
|
||||
title_albums.append(album)
|
||||
|
||||
for album in ALBUMS:
|
||||
for album in api.ALBUMS:
|
||||
if query.lower() in album.artist.lower():
|
||||
artist_albums.append(album)
|
||||
|
||||
|
@ -8,19 +8,21 @@ from app import albumslib, searchlib
|
||||
from app import trackslib
|
||||
|
||||
bp = Blueprint("api", __name__, url_prefix="")
|
||||
functions.start_watchdog()
|
||||
|
||||
ALBUMS: List[models.Album] = []
|
||||
TRACKS: List[models.Track] = []
|
||||
|
||||
home_dir = helpers.home_dir
|
||||
all_the_f_music = albumslib.create_all_albums()
|
||||
|
||||
|
||||
@helpers.background
|
||||
def initialize() -> None:
|
||||
"""
|
||||
Runs all the necessary setup functions.
|
||||
"""
|
||||
albumslib.create_everything()
|
||||
prep.create_config_dir()
|
||||
functions.reindex_tracks()
|
||||
functions.start_watchdog()
|
||||
|
||||
|
||||
initialize()
|
||||
@ -124,7 +126,7 @@ def get_albumartists():
|
||||
|
||||
tracks = []
|
||||
|
||||
for track in all_the_f_music:
|
||||
for track in TRACKS:
|
||||
if track.album == album and track.albumartist == artist:
|
||||
tracks.append(track)
|
||||
|
||||
@ -236,7 +238,7 @@ def get_folder_tree(folder: str):
|
||||
songs = []
|
||||
|
||||
for entry in files:
|
||||
for track in all_the_f_music:
|
||||
for track in TRACKS:
|
||||
if track.filepath == entry.path:
|
||||
songs.append(track)
|
||||
|
||||
|
@ -66,7 +66,7 @@ def populate():
|
||||
if tags is not None:
|
||||
instances.songs_instance.insert_song(tags)
|
||||
|
||||
albumslib.create_all_albums()
|
||||
albumslib.create_everything()
|
||||
|
||||
print("\n check done")
|
||||
end = time.time()
|
||||
|
@ -4,19 +4,10 @@ This module contains mimi functions for the server.
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from typing import List
|
||||
import requests
|
||||
import colorgram
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from app import instances
|
||||
from app import functions
|
||||
from app import watchdoge
|
||||
from app import models, settings
|
||||
from app import models
|
||||
|
||||
home_dir = os.path.expanduser("~") + "/"
|
||||
app_dir = os.path.join(home_dir, ".musicx")
|
||||
@ -34,8 +25,6 @@ def background(func):
|
||||
return background_func
|
||||
|
||||
|
||||
|
||||
|
||||
def run_fast_scandir(_dir: str, ext: list):
|
||||
"""
|
||||
Scans a directory for files with a specific extension. Returns a list of files and folders in the directory.
|
||||
@ -117,13 +106,14 @@ def extract_image_colors(image) -> list:
|
||||
|
||||
return formatted_colors
|
||||
|
||||
|
||||
def check_artist_image(image: str) -> str:
|
||||
"""
|
||||
Checks if the artist image is valid.
|
||||
"""
|
||||
img_name = image.replace("/", "::")+ ".webp"
|
||||
img_name = image.replace("/", "::") + ".webp"
|
||||
|
||||
if not os.path.exists(os.path.join(app_dir, "images", "artists", img_name)):
|
||||
return "http://0.0.0.0:8900/images/artists/0.webp"
|
||||
else:
|
||||
return "http://0.0.0.0:8900/images/artists/" + img_name,
|
||||
return ("http://0.0.0.0:8900/images/artists/" + img_name,)
|
||||
|
@ -1,14 +1,12 @@
|
||||
from typing import List
|
||||
from app import models, trackslib, albumslib, helpers
|
||||
|
||||
TRACKS = trackslib.TRACKS
|
||||
from app import models, api, albumslib, helpers
|
||||
|
||||
|
||||
def get_tracks(query: str) -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs with a given title.
|
||||
"""
|
||||
tracks = [track for track in TRACKS if query.lower() in track.title.lower()]
|
||||
tracks = [track for track in api.TRACKS if query.lower() in track.title.lower()]
|
||||
return helpers.remove_duplicates(tracks)
|
||||
|
||||
|
||||
@ -23,4 +21,4 @@ def get_artists(artist: str) -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs with a given artist.
|
||||
"""
|
||||
return [track for track in TRACKS if artist.lower() in str(track.artists).lower()]
|
||||
return [track for track in api.TRACKS if artist.lower() in str(track.artists).lower()]
|
||||
|
@ -2,13 +2,9 @@ import os
|
||||
from trace import Trace
|
||||
from typing import List
|
||||
from app import models, instances
|
||||
from app import albumslib
|
||||
from app import albumslib, api
|
||||
from app.helpers import remove_duplicates
|
||||
|
||||
|
||||
TRACKS: List[models.Track] = []
|
||||
|
||||
|
||||
def create_all_tracks() -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs under the ~/ directory.
|
||||
@ -32,15 +28,15 @@ def create_all_tracks() -> List[models.Track]:
|
||||
|
||||
tracks.append(models.Track(track))
|
||||
|
||||
TRACKS.clear()
|
||||
TRACKS.extend(tracks)
|
||||
api.TRACKS.clear()
|
||||
api.TRACKS.extend(tracks)
|
||||
|
||||
|
||||
def get_album_tracks(albumname, artist):
|
||||
"""Returns all tracks matching an album"""
|
||||
_tracks: List[models.Track] = []
|
||||
|
||||
for track in TRACKS:
|
||||
for track in api.TRACKS:
|
||||
if track.album == albumname and track.albumartist == artist:
|
||||
_tracks.append(track)
|
||||
|
||||
|
@ -6,7 +6,7 @@ from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from app import instances, functions
|
||||
from app import api
|
||||
from app import api, models
|
||||
|
||||
|
||||
class OnMyWatch:
|
||||
@ -39,10 +39,10 @@ def add_track(filepath: str) -> None:
|
||||
|
||||
if tags is not None:
|
||||
instances.songs_instance.insert_song(tags)
|
||||
track = instances.songs_instance.get_song_by_path(tags["filepath"])
|
||||
track_obj = instances.songs_instance.get_song_by_path(tags["filepath"])
|
||||
|
||||
track_obj = functions.create_track_class(track)
|
||||
api.all_the_f_music.append(track_obj)
|
||||
track = models.Track(track_obj)
|
||||
api.TRACKS.extend(track)
|
||||
|
||||
|
||||
def remove_track(filepath: str) -> None:
|
||||
@ -52,16 +52,16 @@ def remove_track(filepath: str) -> None:
|
||||
trackid = instances.songs_instance.get_song_by_path(filepath)["_id"]["$oid"]
|
||||
instances.songs_instance.remove_song_by_id(trackid)
|
||||
|
||||
for track in api.all_the_f_music:
|
||||
for track in api.TRACKS:
|
||||
if track.trackid == trackid:
|
||||
api.all_the_f_music.remove(track)
|
||||
api.TRACKS.remove(track)
|
||||
|
||||
|
||||
class Handler(PatternMatchingEventHandler):
|
||||
files_to_process = []
|
||||
|
||||
def __init__(self):
|
||||
print("💠 started watchdog")
|
||||
print("💠 started watchdog 💠")
|
||||
PatternMatchingEventHandler.__init__(
|
||||
self,
|
||||
patterns=["*.flac", "*.mp3"],
|
||||
@ -93,7 +93,7 @@ class Handler(PatternMatchingEventHandler):
|
||||
if tr in event.dest_path:
|
||||
print("trash ++")
|
||||
remove_track(event.src_path)
|
||||
|
||||
|
||||
elif tr in event.src_path:
|
||||
add_track(event.dest_path)
|
||||
|
||||
@ -101,7 +101,6 @@ class Handler(PatternMatchingEventHandler):
|
||||
add_track(event.dest_path)
|
||||
remove_track(event.src_path)
|
||||
|
||||
|
||||
def on_closed(self, event):
|
||||
"""
|
||||
Fired when a created file is closed.
|
||||
|
Loading…
x
Reference in New Issue
Block a user