[server] minor refactors

This commit is contained in:
geoffrey45
2022-03-27 18:15:11 +03:00
parent ac988b8a96
commit e8994ce9ae
6 changed files with 27 additions and 32 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ from app.lib import folderslib
from app.lib import playlistlib from app.lib import playlistlib
DB_TRACKS = instances.songs_instance.get_all_songs() PRE_TRACKS = instances.songs_instance.get_all_songs()
VALID_FOLDERS: Set[str] = set() VALID_FOLDERS: Set[str] = set()
ALBUMS: List[models.Album] = [] ALBUMS: List[models.Album] = []
+1 -1
View File
@@ -21,7 +21,7 @@ def get_albums():
"""returns all the albums""" """returns all the albums"""
albums = [] albums = []
for song in api.DB_TRACKS: for song in api.PRE_TRACKS:
al_obj = {"name": song["album"], "artist": song["artists"]} al_obj = {"name": song["album"], "artist": song["artists"]}
if al_obj not in albums: if al_obj not in albums:
+11 -8
View File
@@ -18,6 +18,7 @@ from mutagen.id3 import ID3
from mutagen.flac import FLAC from mutagen.flac import FLAC
from progress.bar import Bar from progress.bar import Bar
from PIL import Image from PIL import Image
# from pprint import pprint # from pprint import pprint
from app import helpers from app import helpers
@@ -64,9 +65,13 @@ def populate():
s, files = helpers.run_fast_scandir(settings.HOME_DIR, [".flac", ".mp3"], full=True) s, files = helpers.run_fast_scandir(settings.HOME_DIR, [".flac", ".mp3"], full=True)
_bar = Bar("Processing files", max=len(files)) _bar = Bar("Processing files", max=len(files))
for file in files: for file in files:
tags = get_tags(file) tags = get_tags(file)
if tags not in api.PRE_TRACKS:
api.PRE_TRACKS.append(tags)
if tags is not None: if tags is not None:
instances.songs_instance.insert_song(tags) instances.songs_instance.insert_song(tags)
@@ -75,7 +80,7 @@ def populate():
albumslib.create_everything() albumslib.create_everything()
folderslib.run_scandir() folderslib.run_scandir()
end = time.time() end = time.time()
print( print(
@@ -109,7 +114,7 @@ def populate_images():
artists = [] artists = []
for song in api.DB_TRACKS: for song in api.PRE_TRACKS:
this_artists = song["artists"].split(", ") this_artists = song["artists"].split(", ")
for artist in this_artists: for artist in this_artists:
@@ -178,9 +183,9 @@ def return_album_art(filepath):
def save_t_colors(): def save_t_colors():
_bar = Bar("Processing image colors", max=len(api.DB_TRACKS)) _bar = Bar("Processing image colors", max=len(api.PRE_TRACKS))
for track in api.DB_TRACKS: for track in api.PRE_TRACKS:
filepath = track["filepath"] filepath = track["filepath"]
album_art = return_album_art(filepath) album_art = return_album_art(filepath)
@@ -376,12 +381,10 @@ def get_all_albums() -> List[models.Album]:
""" """
Returns a list of album objects for all albums in the database. Returns a list of album objects for all albums in the database.
""" """
print("processing albums started ...")
albums: List[models.Album] = [] albums: List[models.Album] = []
_bar = Bar("Creating albums", max=len(api.DB_TRACKS)) _bar = Bar("Creating albums", max=len(api.PRE_TRACKS))
for track in api.DB_TRACKS: for track in api.PRE_TRACKS:
xx = albumslib.create_album(track) xx = albumslib.create_album(track)
if xx not in albums: if xx not in albums:
albums.append(xx) albums.append(xx)
+1 -1
View File
@@ -51,7 +51,7 @@ def get_album_image(album: list) -> str:
def get_album_tracks(album: str, artist: str) -> List: def get_album_tracks(album: str, artist: str) -> List:
return [ return [
track track
for track in api.DB_TRACKS for track in api.PRE_TRACKS
if track["album"] == album and track["albumartist"] == artist if track["album"] == album and track["albumartist"] == artist
] ]
+6 -13
View File
@@ -17,7 +17,6 @@ def get_folder_track_count(foldername: str) -> int:
return len(track_list) return len(track_list)
def create_folder(foldername: str) -> models.Folder: def create_folder(foldername: str) -> models.Folder:
"""Create a single Folder object""" """Create a single Folder object"""
folder = { folder = {
@@ -55,7 +54,6 @@ def get_subdirs(foldername: str) -> List[models.Folder]:
if str1 != "": if str1 != "":
subdirs.add(foldername + str1) subdirs.add(foldername + str1)
return [create_folder(dir) for dir in subdirs] return [create_folder(dir) for dir in subdirs]
@@ -65,16 +63,11 @@ def run_scandir():
Initiates the creation of all folder objects for each folder with a track in it. Initiates the creation of all folder objects for each folder with a track in it.
Runs in a background thread after every 5 minutes. Runs in a background thread after every 5 minutes.
It calls the It calls the
""" """
flag = False get_valid_folders()
folders_ = create_all_folders()
"""Create all the folder objects before clearing api.FOLDERS"""
while flag is False: api.FOLDERS.clear()
get_valid_folders() api.FOLDERS.extend(folders_)
folders_ = create_all_folders()
"""Create all the folder objects before clearing api.FOLDERS"""
api.FOLDERS.clear()
api.FOLDERS.extend(folders_)
time.sleep(300)
+7 -8
View File
@@ -7,7 +7,7 @@ from typing import List
from app import models, instances from app import models, instances
from app.lib import albumslib from app.lib import albumslib
from app.helpers import remove_duplicates from app.helpers import remove_duplicates
from app import api, helpers from app import api
from progress.bar import Bar from progress.bar import Bar
@@ -17,10 +17,9 @@ def create_all_tracks() -> List[models.Track]:
""" """
tracks: list[models.Track] = [] tracks: list[models.Track] = []
timer = helpers.Timer() _bar = Bar("Creating tracks", max=len(api.PRE_TRACKS))
_bar = Bar("Creating tracks", max=len(api.DB_TRACKS)) for track in api.PRE_TRACKS:
for track in api.DB_TRACKS:
try: try:
os.chmod(track["filepath"], 0o755) os.chmod(track["filepath"], 0o755)
except FileNotFoundError: except FileNotFoundError:
@@ -33,10 +32,10 @@ def create_all_tracks() -> List[models.Track]:
tracks.append(models.Track(track)) tracks.append(models.Track(track))
_bar.next() _bar.next()
_bar.finish()
api.TRACKS.clear() api.TRACKS.clear()
api.TRACKS.extend(tracks) api.TRACKS.extend(tracks)
_bar.finish()
print(f"Created all songs in {timer.stop()}")
def get_album_tracks(albumname, artist): def get_album_tracks(albumname, artist):
@@ -53,5 +52,5 @@ def get_album_tracks(albumname, artist):
def get_track_by_id(trackid: str) -> models.Track: def get_track_by_id(trackid: str) -> models.Track:
"""Returns api track matching an id""" """Returns api track matching an id"""
for track in api.TRACKS: for track in api.TRACKS:
if track.id == trackid: if track.trackid == trackid:
return track return track