Save complete tracks and albums to the db for faster startup

- refactor function locations
- add logger
- check for new tracks instead of re-processing all files
This commit is contained in:
geoffrey45
2022-04-21 10:16:45 +03:00
parent ef68cae625
commit d98cc0547e
22 changed files with 448 additions and 380 deletions
+65 -15
View File
@@ -1,14 +1,37 @@
"""
This library contains all the functions related to albums.
"""
import random
import urllib
from pprint import pprint
from typing import List
from progress.bar import Bar
from app import api
from app import functions
from app import models
from app.lib import trackslib
from app import instances
def get_all_albums() -> List[models.Album]:
"""
Returns a list of album objects for all albums in the database.
"""
print("Getting all albums...")
albums: List[models.Album] = []
db_albums = instances.album_instance.get_all_albums()
_bar = Bar("Creating albums", max=len(db_albums))
for album in db_albums:
aa = models.Album(album)
albums.append(aa)
_bar.next()
_bar.finish()
return albums
def create_everything() -> List[models.Track]:
@@ -16,15 +39,39 @@ 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: list[models.Album] = get_all_albums()
api.ALBUMS.clear()
api.ALBUMS.extend(albums)
api.ALBUMS = albums
api.ALBUMS.sort(key=lambda x: x.title)
tracks = trackslib.create_all_tracks()
api.TRACKS.clear()
api.TRACKS.extend(tracks)
api.TRACKS.sort(key=lambda x: x.title)
def find_album(albumtitle: str, artist: str) -> models.Album:
"""
Finds an album by album title and artist.
"""
left = 0
right = len(api.ALBUMS) - 1
iter = 0
while left <= right:
iter += 1
mid = (left + right) // 2
if api.ALBUMS[mid].title == albumtitle and api.ALBUMS[mid].artist == artist:
return mid
if api.ALBUMS[mid].title < albumtitle:
left = mid + 1
else:
right = mid - 1
return None
def get_album_duration(album: list) -> int:
@@ -40,32 +87,40 @@ def get_album_duration(album: list) -> int:
return album_duration
def use_defaults() -> str:
"""
Returns a path to a random image in the defaults directory.
"""
path = "defaults/" + str(random.randint(0, 20)) + ".webp"
return path
def get_album_image(album: list) -> str:
"""
Gets the image of an album.
"""
for track in album:
img_p = (track["album"] + track["albumartist"] + ".webp").replace(
"/", "::")
img_p = (track["album"] + track["albumartist"] + ".webp").replace("/", "::")
img = functions.extract_thumb(track["filepath"], webp_path=img_p)
if img is not None:
return img
return functions.use_defaults()
return use_defaults()
def get_album_tracks(album: str, artist: str) -> List:
tracks = []
for track in api.PRE_TRACKS:
for track in api.DB_TRACKS:
try:
if track["album"] == album and track["albumartist"] == artist:
tracks.append(track)
except TypeError:
pprint(track, indent=4)
print(album, artist)
return tracks
@@ -85,19 +140,14 @@ def create_album(track) -> models.Album:
album["date"] = album_tracks[0]["date"]
album["artistimage"] = urllib.parse.quote_plus(
album_tracks[0]["albumartist"] + ".webp")
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.
@@ -106,7 +156,7 @@ def search_albums_by_name(query: str) -> List[models.Album]:
artist_albums: List[models.Album] = []
for album in api.ALBUMS:
if query.lower() in album.album.lower():
if query.lower() in album.title.lower():
title_albums.append(album)
for album in api.ALBUMS: