first commit

This commit is contained in:
Tomas Dvorak
2026-04-13 17:46:58 +02:00
commit 6e8fedf534
234 changed files with 53808 additions and 0 deletions
View File
+50
View File
@@ -0,0 +1,50 @@
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 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]
+43
View File
@@ -0,0 +1,43 @@
from dataclasses import asdict
from swingmusic.models.artist import Artist
def serialize_for_card(artist: Artist, include: set[str] = None):
if include is None:
include = set()
try:
artist_dict = asdict(artist)
except TypeError:
return {}
props_to_remove = {
"is_favorite",
"trackcount",
"duration",
"albumcount",
"playcount",
"playduration",
"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]
+16
View File
@@ -0,0 +1,16 @@
from dataclasses import asdict
from swingmusic.models.playlist import Playlist
def serialize_for_card(playlist: Playlist, to_remove=None):
if to_remove is None:
to_remove = set()
p_dict = asdict(playlist)
props = {"trackhashes"}.union(to_remove)
for key in props:
p_dict.pop(key, None)
return p_dict
+71
View File
@@ -0,0 +1,71 @@
from dataclasses import asdict
from swingmusic.models.track import Track
def serialize_track(
track: Track, to_remove: set = None, remove_disc: bool = True
) -> dict:
"""
Convert `Track` to dict
:params track: Track to convert
:params to_remove: custom tags which should also be removed from Track.
:params remove_disc:
"""
if to_remove is None:
to_remove = set()
album_dict = asdict(track)
album_keys = (key for key in album_dict if key.startswith(("is_", "_")))
# add all keys from album_dict starting with "is_" or "_"
props = {
"date",
"genre",
"last_mod",
"og_title",
"og_album",
"copyright",
"config",
"artist_hashes",
"created_date",
"fav_userids",
"playcount",
"genrehashes",
"id",
"lastplayed",
"playduration",
"genres",
*to_remove,
*album_keys,
}
if remove_disc:
props.add("disc")
props.add("track")
for key in props:
album_dict.pop(key, None)
# remove images
for key in ["artists", "albumartists"]:
for artist in album_dict[key]:
artist.pop("image", None)
# is_favorite @property is not included in `asdict`
album_dict["is_favorite"] = track.is_favorite
return album_dict
def serialize_tracks(
tracks: list[Track], _remove: set = None, remove_disc=True
) -> list[dict]:
"""
wrapper for iterable type with Tracks.
convert Tracks to dict and return as list[dict]
"""
if _remove is None:
_remove = set()
return [serialize_track(t, _remove, remove_disc) for t in tracks]