From 46d183efbcffc369e726309c06850c856c538676 Mon Sep 17 00:00:00 2001 From: Choco <94597336+ChocoMeow@users.noreply.github.com> Date: Mon, 20 Feb 2023 21:16:45 +0800 Subject: [PATCH] Merge Spotify objects --- voicelink/spotify/__init__.py | 5 +-- voicelink/spotify/album.py | 20 --------- voicelink/spotify/artist.py | 20 --------- voicelink/spotify/client.py | 6 +-- voicelink/spotify/objects.py | 85 +++++++++++++++++++++++++++++++++++ voicelink/spotify/playlist.py | 24 ---------- voicelink/spotify/track.py | 25 ----------- 7 files changed, 87 insertions(+), 98 deletions(-) delete mode 100644 voicelink/spotify/album.py delete mode 100644 voicelink/spotify/artist.py create mode 100644 voicelink/spotify/objects.py delete mode 100644 voicelink/spotify/playlist.py delete mode 100644 voicelink/spotify/track.py diff --git a/voicelink/spotify/__init__.py b/voicelink/spotify/__init__.py index 48611a5..a7a4a10 100644 --- a/voicelink/spotify/__init__.py +++ b/voicelink/spotify/__init__.py @@ -22,8 +22,5 @@ SOFTWARE. """ from .exceptions import InvalidSpotifyURL, SpotifyRequestException -from .track import Track -from .playlist import Playlist -from .album import Album -from .artist import Artist +from .objects import * from .client import Client diff --git a/voicelink/spotify/album.py b/voicelink/spotify/album.py deleted file mode 100644 index bde4a95..0000000 --- a/voicelink/spotify/album.py +++ /dev/null @@ -1,20 +0,0 @@ -from .track import Track - - -class Album: - """The base class for a Spotify album""" - - def __init__(self, data: dict) -> None: - self.name = data.get('name', 'Unknown') - self.artists = ", ".join(artist["name"] for artist in data.get('artists')) - self.image = data["images"][0]["url"] - self.tracks = [Track(track, image=self.image) for track in data["tracks"]["items"]] - self.total_tracks = data["total_tracks"] - self.id = data.get('id') - self.uri = data["external_urls"]["spotify"] - - def __repr__(self) -> str: - return ( - f"" - ) diff --git a/voicelink/spotify/artist.py b/voicelink/spotify/artist.py deleted file mode 100644 index 1a3dffc..0000000 --- a/voicelink/spotify/artist.py +++ /dev/null @@ -1,20 +0,0 @@ -from .track import Track - -class Artist: - """The base class for a Spotify playlist""" - - def __init__(self, data: dict) -> None: - self.tracks = [ Track(track) for track in data['tracks'] ] - if self.tracks: - self.image = self.tracks[0].image - self.total_tracks = len(self.tracks) - self.owner = self.tracks[0].artists - self.id = self.tracks[0].artistId - self.uri = data['tracks'][0]['album']['artists'][0]['external_urls']['spotify'] - self.name = f"Top tracks - {self.owner}" - - def __repr__(self) -> str: - return ( - f"" - ) diff --git a/voicelink/spotify/client.py b/voicelink/spotify/client.py index 5f8758e..28afdfa 100644 --- a/voicelink/spotify/client.py +++ b/voicelink/spotify/client.py @@ -27,11 +27,8 @@ from base64 import b64encode import aiohttp -from .album import Album +from .objects import Track, Album, Artist, Playlist from .exceptions import InvalidSpotifyURL, SpotifyRequestException -from .playlist import Playlist -from .track import Track -from .artist import Artist GRANT_URL = "https://accounts.spotify.com/api/token" REQUEST_URL = "https://api.spotify.com/v1/{type}s/{id}" @@ -41,7 +38,6 @@ SPOTIFY_URL_REGEX = re.compile( r"https?://open.spotify.com/(?Palbum|playlist|track|artist)/(?P[a-zA-Z0-9]+)" ) - class Client: """The base client for the Spotify module of Voicelink. This class will do all the heavy lifting of getting all the metadata diff --git a/voicelink/spotify/objects.py b/voicelink/spotify/objects.py new file mode 100644 index 0000000..5608b82 --- /dev/null +++ b/voicelink/spotify/objects.py @@ -0,0 +1,85 @@ +from typing import List + +class Track: + """The base class for a Spotify Track""" + + def __init__(self, data: dict, image=None) -> None: + self.name = data.get('name', 'Unknown') + self.artists = ", ".join(artist["name"] for artist in data.get('artists')) + self.artistId = [artist['id'] for artist in data.get('artists')] + self.length = data.get('duration_ms') + self.id = data.get('id') + + if data.get("album") and data["album"].get("images"): + self.image = data["album"]["images"][0]["url"] + else: + self.image = image + + if data["is_local"]: + self.uri = None + else: + self.uri = data["external_urls"]["spotify"] + + def __repr__(self) -> str: + return ( + f"" + ) + +class Album: + """The base class for a Spotify album""" + + def __init__(self, data: dict) -> None: + self.name = data.get('name', 'Unknown') + self.artists = ", ".join(artist["name"] for artist in data.get('artists')) + self.image = data["images"][0]["url"] + self.tracks = [Track(track, image=self.image) for track in data["tracks"]["items"]] + self.total_tracks = data["total_tracks"] + self.id = data.get('id') + self.uri = data["external_urls"]["spotify"] + + def __repr__(self) -> str: + return ( + f"" + ) + +class Artist: + """The base class for a Spotify playlist""" + + def __init__(self, data: dict) -> None: + self.tracks = [Track(track) for track in data['tracks']] + if self.tracks: + self.image = self.tracks[0].image + self.total_tracks = len(self.tracks) + self.owner = self.tracks[0].artists + self.id = self.tracks[0].artistId + self.uri = data['tracks'][0]['album']['artists'][0]['external_urls']['spotify'] + self.name = f"Top tracks - {self.owner}" + + def __repr__(self) -> str: + return ( + f"" + ) + +class Playlist: + """The base class for a Spotify playlist""" + + def __init__(self, data: dict, tracks: List[Track]) -> None: + self.name = data.get('name', 'Unknown') + self.tracks = tracks + self.owner = data["owner"]["display_name"] + self.total_tracks = data["tracks"]["total"] + self.id = data.get('id') + if data.get("images") and len(data["images"]): + self.image = data["images"][0]["url"] + else: + self.image = None + self.uri = data["external_urls"]["spotify"] + + def __repr__(self) -> str: + return ( + f"" + ) \ No newline at end of file diff --git a/voicelink/spotify/playlist.py b/voicelink/spotify/playlist.py deleted file mode 100644 index d82236b..0000000 --- a/voicelink/spotify/playlist.py +++ /dev/null @@ -1,24 +0,0 @@ -from .track import Track -from typing import List - - -class Playlist: - """The base class for a Spotify playlist""" - - def __init__(self, data: dict, tracks: List[Track]) -> None: - self.name = data.get('name', 'Unknown') - self.tracks = tracks - self.owner = data["owner"]["display_name"] - self.total_tracks = data["tracks"]["total"] - self.id = data.get('id') - if data.get("images") and len(data["images"]): - self.image = data["images"][0]["url"] - else: - self.image = None - self.uri = data["external_urls"]["spotify"] - - def __repr__(self) -> str: - return ( - f"" - ) diff --git a/voicelink/spotify/track.py b/voicelink/spotify/track.py deleted file mode 100644 index 325f920..0000000 --- a/voicelink/spotify/track.py +++ /dev/null @@ -1,25 +0,0 @@ -class Track: - """The base class for a Spotify Track""" - - def __init__(self, data: dict, image = None) -> None: - self.name = data.get('name', 'Unknown') - self.artists = ", ".join(artist["name"] for artist in data.get('artists')) - self.artistId = [ artist['id'] for artist in data.get('artists') ] - self.length = data.get('duration_ms') - self.id = data.get('id') - - if data.get("album") and data["album"].get("images"): - self.image = data["album"]["images"][0]["url"] - else: - self.image = image - - if data["is_local"]: - self.uri = None - else: - self.uri = data["external_urls"]["spotify"] - - def __repr__(self) -> str: - return ( - f"" - )