mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
modularize src
+ merge main.py and manage.py + move start logic to swingmusic/__main__.py + add a run.py on the project root
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from dataclasses import asdict
|
||||
from swingmusic.models import Album
|
||||
|
||||
|
||||
def album_serializer(album: Album, to_remove: set[str]) -> dict:
|
||||
try:
|
||||
album_dict = asdict(album)
|
||||
except TypeError:
|
||||
return {}
|
||||
|
||||
to_remove.update(key for key in album_dict.keys() if key.startswith("is_"))
|
||||
for key in to_remove:
|
||||
album_dict.pop(key, None)
|
||||
|
||||
# remove artist images
|
||||
for artist in album_dict["albumartists"]:
|
||||
artist.pop("image", None)
|
||||
|
||||
album_dict["type"] = "album"
|
||||
return album_dict
|
||||
|
||||
|
||||
def serialize_for_card(album: Album):
|
||||
props_to_remove = {
|
||||
"duration",
|
||||
"count",
|
||||
"artisthashes",
|
||||
"albumartists_hashes",
|
||||
"created_date",
|
||||
"og_title",
|
||||
"base_title",
|
||||
"genres",
|
||||
"playcount",
|
||||
"trackcount",
|
||||
"type",
|
||||
"playduration",
|
||||
"genrehashes",
|
||||
"fav_userids",
|
||||
"extra",
|
||||
"id",
|
||||
"lastplayed",
|
||||
"weakhash",
|
||||
}
|
||||
|
||||
return album_serializer(album, props_to_remove)
|
||||
|
||||
|
||||
def serialize_for_card_many(albums: list[Album]):
|
||||
return [serialize_for_card(a) for a in albums]
|
||||
@@ -0,0 +1,42 @@
|
||||
from dataclasses import asdict
|
||||
|
||||
from swingmusic.models.artist import Artist
|
||||
|
||||
|
||||
def serialize_for_card(artist: Artist, include: set[str] = set()):
|
||||
try:
|
||||
artist_dict = asdict(artist)
|
||||
except TypeError:
|
||||
return {}
|
||||
|
||||
props_to_remove = {
|
||||
"is_favorite",
|
||||
"trackcount",
|
||||
"duration",
|
||||
"albumcount",
|
||||
"playcount",
|
||||
"playduration",
|
||||
"playcount",
|
||||
"lastplayed",
|
||||
"id",
|
||||
"genres",
|
||||
"genrehashes",
|
||||
"extra",
|
||||
"created_date",
|
||||
"date",
|
||||
"fav_userids",
|
||||
"_score",
|
||||
}
|
||||
|
||||
if include:
|
||||
props_to_remove = props_to_remove - include
|
||||
|
||||
for key in props_to_remove:
|
||||
artist_dict.pop(key, None)
|
||||
|
||||
artist_dict["type"] = "artist"
|
||||
return artist_dict
|
||||
|
||||
|
||||
def serialize_for_cards(artists: list[Artist]):
|
||||
return [serialize_for_card(a) for a in artists]
|
||||
@@ -0,0 +1,13 @@
|
||||
from dataclasses import asdict
|
||||
from swingmusic.models.playlist import Playlist
|
||||
|
||||
|
||||
def serialize_for_card(playlist: Playlist, to_remove=set()):
|
||||
p_dict = asdict(playlist)
|
||||
|
||||
props = {"trackhashes"}.union(to_remove)
|
||||
|
||||
for key in props:
|
||||
p_dict.pop(key, None)
|
||||
|
||||
return p_dict
|
||||
@@ -0,0 +1,53 @@
|
||||
from dataclasses import asdict
|
||||
|
||||
from swingmusic.models.track import Track
|
||||
|
||||
|
||||
def serialize_track(track: Track, to_remove: set = set(), remove_disc=True) -> dict:
|
||||
album_dict = asdict(track)
|
||||
# is_favorite @property is not included in asdict
|
||||
album_dict["is_favorite"] = track.is_favorite
|
||||
|
||||
props = {
|
||||
"date",
|
||||
"genre",
|
||||
"last_mod",
|
||||
"og_title",
|
||||
"og_album",
|
||||
"copyright",
|
||||
"config",
|
||||
"disc",
|
||||
"track",
|
||||
"artist_hashes",
|
||||
"created_date",
|
||||
"fav_userids",
|
||||
"playcount",
|
||||
"genrehashes",
|
||||
"id",
|
||||
"lastplayed",
|
||||
"playduration",
|
||||
"genres",
|
||||
}.union(to_remove)
|
||||
|
||||
if not remove_disc:
|
||||
props.remove("disc")
|
||||
props.remove("track")
|
||||
|
||||
props.update(key for key in album_dict.keys() if key.startswith(("is_", "_")))
|
||||
props.remove("is_favorite")
|
||||
|
||||
for key in props:
|
||||
album_dict.pop(key, None)
|
||||
|
||||
to_remove_images = ["artists", "albumartists"]
|
||||
for key in to_remove_images:
|
||||
for artist in album_dict[key]:
|
||||
artist.pop("image", None)
|
||||
|
||||
return album_dict
|
||||
|
||||
|
||||
def serialize_tracks(
|
||||
tracks: list[Track], _remove: set = set(), remove_disc=True
|
||||
) -> list[dict]:
|
||||
return [serialize_track(t, _remove, remove_disc) for t in tracks]
|
||||
Reference in New Issue
Block a user