Skip to content

Commit

Permalink
Add cached service metadata to tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyssion committed Aug 14, 2024
1 parent abc4ffb commit b8d1bfc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions playlist_sync/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ def remove_from_playlist(self, url: str, tracks: list[Track]):
def add_to_playlist(self, url: str, tracks: list[Track]):
raise NotImplementedError

def _extract_track_metadata(self, track: Track) -> Any | None:
return track._service_metadata.get(str(self))

def __str__(self) -> str:
return self.__class__.__name__
4 changes: 3 additions & 1 deletion playlist_sync/services/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def fetch_playlist(self, url: str) -> list[Track]:

for item in items:
sp_track = item['track']
track = Track(title=sp_track['name'], artist='') # TODO: artist
track = Track(
title=sp_track['name'], artist='', service=str(self), metadata=item['track']
) # TODO: artist
tracks.append(track)

return tracks
Expand Down
4 changes: 3 additions & 1 deletion playlist_sync/services/ytmusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def fetch_playlist(self, url: str) -> list[Track]:
tracks = []

for yt_track in yt_tracks:
track = Track(title=yt_track['title'], artist='') # TODO: artist
track = Track(
title=yt_track['title'], artist='', service=str(self), metadata=yt_track
) # TODO: artist
tracks.append(track)

return tracks
Expand Down
24 changes: 17 additions & 7 deletions playlist_sync/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@
class Track:
title: str
artist: str
_service_metadata: dict[str, Any]

def __init__(self, title: str, artist: str):
def __init__(
self, title: str, artist: str, *, service: str | None = None, metadata: Any = None
):
self.title = title
self.artist = artist
self._service_metadata = {}

def __eq__(self, other: Any) -> bool:
if not isinstance(other, Track):
return False
if service is not None:
self._service_metadata[service] = metadata

if self.title != other.title or self.artist != other.artist:
return False
def add_metadata(self, service: str, metadata: Any):
self._service_metadata[service] = metadata

return True
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Track):
return False

if self.title != other.title or self.artist != other.artist:
return False

return True

def __repr__(self) -> str:
return f'Track(title={self.title!r}, artist={self.artist!r})'

0 comments on commit b8d1bfc

Please sign in to comment.