mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-07 11:45:35 +00:00
refactor references
This commit is contained in:
parent
cf14610509
commit
2bb5a89a8f
@ -1,101 +0,0 @@
|
||||
import urllib
|
||||
from typing import List
|
||||
from app import models, functions, helpers
|
||||
from app import trackslib
|
||||
from app import api
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
api.ALBUMS.clear()
|
||||
api.ALBUMS.extend(albums)
|
||||
trackslib.create_all_tracks()
|
||||
|
||||
|
||||
|
||||
def get_album_duration(album: list) -> int:
|
||||
"""
|
||||
Gets the duration of an album.
|
||||
"""
|
||||
|
||||
album_duration = 0
|
||||
|
||||
for track in album:
|
||||
album_duration += track["length"]
|
||||
|
||||
return album_duration
|
||||
|
||||
|
||||
def get_album_image(album: list) -> str:
|
||||
"""
|
||||
Gets the image of an album.
|
||||
"""
|
||||
|
||||
for track in album:
|
||||
img = functions.extract_thumb(track["filepath"])
|
||||
|
||||
if img is not None:
|
||||
return img
|
||||
|
||||
return functions.use_defaults()
|
||||
|
||||
|
||||
def get_album_tracks(album: str, artist: str) -> List:
|
||||
return [
|
||||
track
|
||||
for track in api.DB_TRACKS
|
||||
if track["album"] == album and track["albumartist"] == artist
|
||||
]
|
||||
|
||||
|
||||
def create_album(track) -> models.Album:
|
||||
"""
|
||||
Generates and returns an album object from a track object.
|
||||
"""
|
||||
album = {
|
||||
"album": track["album"],
|
||||
"artist": track["albumartist"],
|
||||
}
|
||||
|
||||
album_tracks = get_album_tracks(album["album"], album["artist"])
|
||||
|
||||
album["count"] = len(album_tracks)
|
||||
album["duration"] = get_album_duration(album_tracks)
|
||||
album["date"] = album_tracks[0]["date"]
|
||||
album["artistimage"] = urllib.parse.quote_plus(
|
||||
album_tracks[0]["albumartist"] + ".webp"
|
||||
)
|
||||
|
||||
album["image"] = get_album_image(album_tracks)
|
||||
|
||||
return models.Album(album)
|
||||
|
||||
|
||||
def find_album(albumtitle, artist):
|
||||
for album in api.ALBUMS:
|
||||
if album.album == albumtitle and album.artist == artist:
|
||||
return album
|
||||
|
||||
|
||||
def search_albums_by_name(query: str) -> List[models.Album]:
|
||||
"""
|
||||
Searches albums by album name.
|
||||
"""
|
||||
title_albums: List[models.Album] = []
|
||||
artist_albums: List[models.Album] = []
|
||||
|
||||
for album in api.ALBUMS:
|
||||
if query.lower() in album.album.lower():
|
||||
title_albums.append(album)
|
||||
|
||||
for album in api.ALBUMS:
|
||||
if query.lower() in album.artist.lower():
|
||||
artist_albums.append(album)
|
||||
|
||||
return [*title_albums, *artist_albums]
|
@ -1,12 +1,14 @@
|
||||
from typing import List
|
||||
|
||||
from app import models, instances
|
||||
from app import functions, helpers, albumslib, prep
|
||||
from app import functions, helpers, prep
|
||||
from app.lib import albumslib
|
||||
|
||||
|
||||
DB_TRACKS = instances.songs_instance.get_all_songs()
|
||||
ALBUMS: List[models.Album] = []
|
||||
TRACKS: List[models.Track] = []
|
||||
PLAYLISTS: List[models.Playlist] = []
|
||||
|
||||
|
||||
@helpers.background
|
||||
|
@ -1,8 +1,8 @@
|
||||
from flask import Blueprint, request
|
||||
from app import api
|
||||
from app import helpers, cache
|
||||
from app import albumslib, functions, trackslib
|
||||
|
||||
from app import functions
|
||||
from app.lib import albumslib, trackslib
|
||||
album_bp = Blueprint("album", __name__, url_prefix="")
|
||||
|
||||
|
||||
|
@ -3,9 +3,7 @@ import urllib
|
||||
from typing import List
|
||||
from flask import request, send_file
|
||||
|
||||
from app import functions, instances, helpers, cache, models, prep
|
||||
from app import albumslib, searchlib
|
||||
from app import trackslib
|
||||
from app import functions, instances, helpers, cache, db, prep
|
||||
from app import api
|
||||
|
||||
|
||||
|
@ -2,13 +2,13 @@ import os
|
||||
from flask import Blueprint
|
||||
|
||||
from app import api
|
||||
from app import settings
|
||||
|
||||
folder_bp = Blueprint("folder", __name__, url_prefix="/")
|
||||
from app import helpers
|
||||
|
||||
|
||||
@folder_bp.route("/f/<folder>")
|
||||
# @cache.cached()
|
||||
def get_folder_tree(folder: str):
|
||||
"""
|
||||
Returns a list of all the folders and tracks in the given folder.
|
||||
@ -16,9 +16,9 @@ def get_folder_tree(folder: str):
|
||||
req_dir = folder.replace("|", "/")
|
||||
|
||||
if folder == "home":
|
||||
req_dir = helpers.home_dir
|
||||
req_dir = settings.HOME_DIR
|
||||
|
||||
dir_content = os.scandir(os.path.join(helpers.home_dir, req_dir))
|
||||
dir_content = os.scandir(os.path.join(settings.HOME_DIR, req_dir))
|
||||
|
||||
folders = []
|
||||
files = []
|
||||
@ -31,7 +31,7 @@ def get_folder_tree(folder: str):
|
||||
_dir = {
|
||||
"name": entry.name,
|
||||
"count": len(files_in_dir),
|
||||
"path": entry.path.replace(helpers.home_dir, ""),
|
||||
"path": entry.path.replace(settings.HOME_DIR, ""),
|
||||
}
|
||||
|
||||
folders.append(_dir)
|
||||
|
@ -1,35 +1,36 @@
|
||||
from flask import Blueprint, request
|
||||
from app import instances
|
||||
from app import instances, api
|
||||
from app.lib import playlistlib
|
||||
|
||||
playlist_bp = Blueprint("playlist", __name__, url_prefix="/")
|
||||
|
||||
|
||||
@playlist_bp.route("/playlists", methods=["GET"])
|
||||
def get_all_playlists():
|
||||
playlists = []
|
||||
for playlist in api.PLAYLISTS:
|
||||
del playlist.tracks
|
||||
playlists.append(playlist)
|
||||
|
||||
return playlists
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/new")
|
||||
def create_playlist():
|
||||
data = request.get_json()
|
||||
|
||||
playlist = {
|
||||
"name": data["name"],
|
||||
"description": data["description"],
|
||||
}
|
||||
playlist = {"name": data["name"], "description": data["description"], "tracks": []}
|
||||
|
||||
instances.playlist_instance.insert_playlist(playlist)
|
||||
return 200
|
||||
|
||||
@playlist_bp.route("/playlist/<playlist_id>")
|
||||
def get_playlist(playlist_id):
|
||||
pass
|
||||
|
||||
@playlist_bp.route("/playlist/tracks/get", methods=["POST"])
|
||||
def add_tracks_to_playlist():
|
||||
pass
|
||||
@playlist_bp.route("/playlist/<playlist_id>/add", methods=["POST"])
|
||||
def add_track_to_playlist(playlist_id):
|
||||
data = request.get_json()
|
||||
|
||||
@playlist_bp.route("/playlist/tracks/remove", methods=["POST"])
|
||||
def remove_single_track():
|
||||
pass
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlist_id>/update/desc", methods=["POST"])
|
||||
def update_playlist():
|
||||
pass
|
||||
pid = data["playlist"]
|
||||
trackid = data["track"]
|
||||
|
||||
playlistlib.add_track(pid, trackid)
|
||||
return 200
|
||||
|
@ -1,6 +1,6 @@
|
||||
from flask import Blueprint, request
|
||||
|
||||
from app import searchlib
|
||||
from app.lib import searchlib
|
||||
from app import helpers
|
||||
|
||||
search_bp = Blueprint("search", __name__, url_prefix="/")
|
||||
|
@ -20,8 +20,8 @@ from PIL import Image
|
||||
|
||||
from app import helpers
|
||||
from app import instances
|
||||
from app import settings, watchdoge, models, trackslib
|
||||
from app import albumslib
|
||||
from app import settings, watchdoge, models
|
||||
from app.lib import albumslib
|
||||
from app import api
|
||||
|
||||
|
||||
@ -59,7 +59,7 @@ def populate():
|
||||
start = time.time()
|
||||
print("\nchecking for new tracks")
|
||||
|
||||
files = helpers.run_fast_scandir(helpers.home_dir, [".flac", ".mp3"])[1]
|
||||
files = helpers.run_fast_scandir(settings.HOME_DIR, [".flac", ".mp3"])[1]
|
||||
|
||||
for file in files:
|
||||
tags = get_tags(file)
|
||||
@ -189,7 +189,7 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
Extracts the thumbnail from an audio file. Returns the path to the thumbnail.
|
||||
"""
|
||||
webp_path = audio_file_path.split("/")[-1] + ".webp"
|
||||
img_path = os.path.join(helpers.app_dir, "images", "thumbnails", webp_path)
|
||||
img_path = os.path.join(settings.THUMBS_PATH, webp_path)
|
||||
|
||||
if os.path.exists(img_path):
|
||||
return urllib.parse.quote(webp_path)
|
||||
@ -332,7 +332,7 @@ def get_tags(fullpath: str) -> dict:
|
||||
"length": round(audio.info.length),
|
||||
"bitrate": round(int(audio.info.bitrate) / 1000),
|
||||
"filepath": fullpath,
|
||||
"folder": os.path.dirname(fullpath).replace(helpers.home_dir, ""),
|
||||
"folder": os.path.dirname(fullpath).replace(settings.HOME_DIR, ""),
|
||||
}
|
||||
|
||||
return tags
|
||||
@ -377,3 +377,4 @@ def get_all_albums() -> List[models.Album]:
|
||||
albums.append(xx)
|
||||
|
||||
return albums
|
||||
|
||||
|
@ -7,10 +7,9 @@ import threading
|
||||
from typing import List
|
||||
import colorgram
|
||||
|
||||
from app import models
|
||||
from app import models, settings
|
||||
|
||||
home_dir = os.path.expanduser("~") + "/"
|
||||
app_dir = os.path.join(home_dir, ".musicx")
|
||||
app_dir = settings.APP_DIR
|
||||
|
||||
|
||||
def background(func):
|
||||
@ -114,6 +113,6 @@ def check_artist_image(image: str) -> str:
|
||||
img_name = image.replace("/", "::") + ".webp"
|
||||
|
||||
if not os.path.exists(os.path.join(app_dir, "images", "artists", img_name)):
|
||||
return "http://10.5.8.182:8900/images/artists/0.webp"
|
||||
return settings.DEFAULT_ARTIST_IMG
|
||||
else:
|
||||
return ("http://10.5.8.182:8900/images/artists/" + img_name,)
|
||||
return (settings.IMG_ARTIST_URI + img_name,)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from app.models import AllSongs, Artists, TrackColors, Albums, Playlists
|
||||
from app.db import artists, albums, trackcolors, tracks, playlists
|
||||
|
||||
songs_instance = AllSongs()
|
||||
artist_instance = Artists()
|
||||
track_color_instance = TrackColors()
|
||||
album_instance = Albums()
|
||||
playlist_instance = Playlists()
|
||||
songs_instance = tracks.AllSongs()
|
||||
artist_instance = artists.Artists()
|
||||
track_color_instance = trackcolors.TrackColors()
|
||||
album_instance = albums.Albums()
|
||||
playlist_instance = playlists.Playlists()
|
@ -15,7 +15,12 @@ def create_config_dir() -> None:
|
||||
_home_dir = os.path.expanduser("~")
|
||||
config_folder = os.path.join(_home_dir, settings.CONFIG_FOLDER)
|
||||
|
||||
dirs = ["", "images", "images/artists", "images/thumbnails"]
|
||||
dirs = [
|
||||
"",
|
||||
"images",
|
||||
os.path.join("images", "artists"),
|
||||
os.path.join("images", "thumbnails"),
|
||||
]
|
||||
|
||||
for _dir in dirs:
|
||||
path = os.path.join(config_folder, _dir)
|
||||
|
@ -1,25 +0,0 @@
|
||||
from typing import List
|
||||
from app import models, albumslib, helpers
|
||||
from app import api
|
||||
|
||||
|
||||
def get_tracks(query: str) -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs with a given title.
|
||||
"""
|
||||
tracks = [track for track in api.TRACKS if query.lower() in track.title.lower()]
|
||||
return helpers.remove_duplicates(tracks)
|
||||
|
||||
|
||||
def get_search_albums(query: str) -> List[models.Album]:
|
||||
"""
|
||||
Gets all songs with a given album.
|
||||
"""
|
||||
return albumslib.search_albums_by_name(query)
|
||||
|
||||
|
||||
def get_artists(artist: str) -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs with a given artist.
|
||||
"""
|
||||
return [track for track in api.TRACKS if artist.lower() in str(track.artists).lower()]
|
@ -1,40 +0,0 @@
|
||||
import os
|
||||
from trace import Trace
|
||||
from typing import List
|
||||
from app import models, instances
|
||||
from app import albumslib
|
||||
from app.helpers import remove_duplicates
|
||||
from app import api
|
||||
|
||||
def create_all_tracks() -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs under the ~/ directory.
|
||||
"""
|
||||
print("Getting all songs...")
|
||||
tracks: list[models.Track] = []
|
||||
|
||||
for track in api.DB_TRACKS:
|
||||
try:
|
||||
os.chmod(track["filepath"], 0o755)
|
||||
except FileNotFoundError:
|
||||
instances.songs_instance.remove_song_by_filepath(track["filepath"])
|
||||
|
||||
album = albumslib.find_album(track["album"], track["albumartist"])
|
||||
|
||||
track["image"] = album.image
|
||||
|
||||
tracks.append(models.Track(track))
|
||||
|
||||
api.TRACKS.clear()
|
||||
api.TRACKS.extend(tracks)
|
||||
|
||||
|
||||
def get_album_tracks(albumname, artist):
|
||||
"""Returns api tracks matching an album"""
|
||||
_tracks: List[models.Track] = []
|
||||
|
||||
for track in api.TRACKS:
|
||||
if track.album == albumname and track.albumartist == artist:
|
||||
_tracks.append(track)
|
||||
|
||||
return remove_duplicates(_tracks)
|
@ -6,8 +6,8 @@ from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from app import instances, functions
|
||||
from app import models
|
||||
from app import albumslib
|
||||
from app import db
|
||||
from app.lib import albumslib
|
||||
from app import api
|
||||
|
||||
|
||||
@ -47,7 +47,7 @@ def add_track(filepath: str) -> None:
|
||||
api.ALBUMS.append(album)
|
||||
|
||||
track["image"] = album.image
|
||||
api.TRACKS.append(models.Track(track))
|
||||
api.TRACKS.append(db.Track(track))
|
||||
|
||||
|
||||
def remove_track(filepath: str) -> None:
|
||||
|
@ -1,29 +0,0 @@
|
||||
import os
|
||||
|
||||
def fast_scandir(dirname):
|
||||
subfolders= [f.path for f in os.scandir(dirname) if f.is_dir()]
|
||||
|
||||
for dirname in list(subfolders):
|
||||
subfolders.extend(fast_scandir(dirname))
|
||||
|
||||
return subfolders
|
||||
|
||||
|
||||
list = fast_scandir('/home/cwilvx/Music')
|
||||
|
||||
def remove_rejects(folders):
|
||||
rejects = []
|
||||
|
||||
for item in folders:
|
||||
if item.find(".thumbnails") != -1:
|
||||
rejects.append(item)
|
||||
|
||||
if len(os.listdir(item)) == 0 and item not in rejects:
|
||||
rejects.append(item)
|
||||
|
||||
for item in rejects:
|
||||
folders.remove(item)
|
||||
|
||||
print(len(folders))
|
||||
|
||||
remove_rejects(list)
|
Loading…
x
Reference in New Issue
Block a user