mirror of
https://github.com/Dvorinka/SpotifyRecAlg.git
synced 2026-06-04 20:43:04 +00:00
first commit
This commit is contained in:
@@ -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]
|
||||
Reference in New Issue
Block a user