Integrate nav

- other minor refactors
This commit is contained in:
geoffrey45
2022-04-14 11:30:19 +03:00
parent 90d646d674
commit 85c59b4cba
28 changed files with 266 additions and 141 deletions
+7 -1
View File
@@ -40,6 +40,7 @@ def create_playlist():
"pre_tracks": [],
"lastUpdated": data["lastUpdated"],
"image": "",
"thumb": ""
}
try:
@@ -100,15 +101,20 @@ def update_playlist(playlistid: str):
"description": str(data.get("description").strip()),
"lastUpdated": str(data.get("lastUpdated")),
"image": None,
"thumb": None
}
for p in api.PLAYLISTS:
if p.playlistid == playlistid:
if image:
playlist["image"] = playlistlib.save_p_image(image, playlistid)
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]
p.update_playlist(playlist)
instances.playlist_instance.update_playlist(playlistid, playlist)
+1 -1
View File
@@ -19,7 +19,7 @@ def send_track_file(trackid):
file["filepath"] for file in api.PRE_TRACKS
if file["_id"]["$oid"] == trackid
][0]
except (FileNotFoundError, IndexError):
except (FileNotFoundError, IndexError) as e:
return "File not found", 404
return send_file(filepath, mimetype="audio/mp3")
+2 -2
View File
@@ -16,8 +16,8 @@ class Albums(db.Mongo):
"""
def __init__(self):
super(Albums, self).__init__("ALBUMS")
self.collection = self.db["ALBUMS"]
super(Albums, self).__init__("ALICE_ALBUMS")
self.collection = self.db["ALL_ALBUMS"]
def insert_album(self, album: dict) -> None:
"""
+2 -2
View File
@@ -12,8 +12,8 @@ class Artists(db.Mongo):
"""
def __init__(self):
super(Artists, self).__init__("ALL_ARTISTS")
self.collection = self.db["THEM_ARTISTS"]
super(Artists, self).__init__("ALICE_ARTISTS")
self.collection = self.db["ALL_ARTISTS"]
def insert_artist(self, artist_obj: dict) -> None:
"""
+2 -2
View File
@@ -15,8 +15,8 @@ class Playlists(db.Mongo):
"""
def __init__(self):
super(Playlists, self).__init__("PLAYLISTS")
self.collection = self.db["PLAYLISTS"]
super(Playlists, self).__init__("ALICE_PLAYLISTS")
self.collection = self.db["ALL_PLAYLISTS"]
def insert_playlist(self, playlist: dict) -> None:
"""
+1 -1
View File
@@ -11,7 +11,7 @@ class TrackColors(db.Mongo):
"""
def __init__(self):
super(TrackColors, self).__init__("TRACK_COLORS")
super(TrackColors, self).__init__("ALICE_TRACK_COLORS")
self.collection = self.db["TRACK_COLORS"]
def insert_track_color(self, track_color: dict) -> None:
+2 -3
View File
@@ -15,8 +15,8 @@ class AllSongs(db.Mongo):
"""
def __init__(self):
super(AllSongs, self).__init__("ALL_SONGS")
self.collection = self.db["ALL_SONGS"]
super(AllSongs, self).__init__("ALICE_MUSIC_TRACKS")
self.collection = self.db["ALL_TRACKS"]
# def drop_db(self):
# self.collection.drop()
@@ -67,7 +67,6 @@ class AllSongs(db.Mongo):
"""
Finds all the tracks matching the title in the query params.
"""
self.collection.create_index([("title", db.pymongo.TEXT)])
song = self.collection.find({"title": {"$regex": query, "$options": "i"}})
return convert_many(song)
-1
View File
@@ -58,7 +58,6 @@ def get_subdirs(foldername: str) -> List[models.Folder]:
if str1 is not None:
subdirs.add(foldername + "/" + str1)
return [create_folder(dir) for dir in subdirs]
+51 -9
View File
@@ -35,8 +35,7 @@ def add_track(playlistid: str, trackid: str):
try:
playlist.add_track(track)
instances.playlist_instance.add_track_to_playlist(
playlistid, track)
instances.playlist_instance.add_track_to_playlist(playlistid, track)
return
except TrackExistsInPlaylist as error:
raise error
@@ -61,6 +60,25 @@ def create_all_playlists():
_bar.next()
_bar.finish()
validate_images()
def create_thumbnail(image: any, img_path: str) -> str:
"""
Creates a 250 x 250 thumbnail from a playlist image
"""
thumb_path = "thumb_" + img_path
full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists", thumb_path)
aspect_ratio = image.width / image.height
new_w = round(250 * aspect_ratio)
thumb = image.resize((new_w, 250), Image.ANTIALIAS)
thumb.save(full_thumb_path, "webp")
return thumb_path
def save_p_image(file: datastructures.FileStorage, pid: str):
"""
@@ -68,11 +86,11 @@ def save_p_image(file: datastructures.FileStorage, pid: str):
"""
img = Image.open(file)
random_str = "".join(
random.choices(string.ascii_letters + string.digits, k=5))
random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5))
img_path = pid + str(random_str) + ".webp"
full_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
if file.content_type == "image/gif":
frames = []
@@ -80,9 +98,33 @@ def save_p_image(file: datastructures.FileStorage, pid: str):
for frame in ImageSequence.Iterator(img):
frames.append(frame.copy())
frames[0].save(full_path, save_all=True, append_images=frames[1:])
return img_path
frames[0].save(full_img_path, save_all=True, append_images=frames[1:])
thumb_path = create_thumbnail(img, img_path=img_path)
img.save(full_path, "webp")
return img_path, thumb_path
return img_path
img.save(full_img_path, "webp")
thumb_path = create_thumbnail(img, img_path=img_path)
return img_path, thumb_path
def validate_images():
"""
Removes all unused images in the images/playlists folder.
"""
images = []
for playlist in api.PLAYLISTS:
if playlist.image:
img_path = playlist.image.split("/")[-1]
thumb_path = playlist.thumb.split("/")[-1]
images.append(img_path)
images.append(thumb_path)
p_path = os.path.join(settings.APP_DIR, "images", "playlists")
for image in os.listdir(p_path):
if image not in images:
os.remove(os.path.join(p_path, image))
+8 -3
View File
@@ -73,9 +73,11 @@ class Album:
def get_p_track(ptrack):
for track in api.TRACKS:
if (track.title == ptrack["title"]
and track.artists == ptrack["artists"]
and ptrack["album"] == track.album):
if (
track.title == ptrack["title"]
and track.artists == ptrack["artists"]
and ptrack["album"] == track.album
):
return track
@@ -103,6 +105,7 @@ class Playlist:
_pre_tracks: list = field(init=False, repr=False)
lastUpdated: int
image: str
thumb: str
description: str = ""
count: int = 0
"""A list of track objects in the playlist"""
@@ -112,6 +115,7 @@ class Playlist:
self.name = data["name"]
self.description = data["description"]
self.image = self.create_img_link(data["image"])
self.thumb = self.create_img_link(data["thumb"])
self._pre_tracks = data["pre_tracks"]
self.tracks = []
self.lastUpdated = data["lastUpdated"]
@@ -149,6 +153,7 @@ class Playlist:
if data["image"]:
self.image = self.create_img_link(data["image"])
self.thumb = self.create_img_link(data["thumb"])
@dataclass
+3
View File
@@ -0,0 +1,3 @@
"""
This module contains patch functions to modify existing data in the database.
"""
+2
View File
@@ -58,6 +58,7 @@ class Playlist:
playlistid: str
name: str
image: str
thumb: str
lastUpdated: int
description: str
count: int = 0
@@ -68,6 +69,7 @@ class Playlist:
self.playlistid = p.playlistid
self.name = p.name
self.image = p.image
self.thumb = p.thumb
self.lastUpdated = p.lastUpdated
self.description = p.description
self.count = p.count