Merge Spotify objects
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"<Voicelink.spotify.Album name={self.name} artists={self.artists} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
@@ -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"<Voicelink.spotify.Artist name={self.name} owner={self.owner} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
@@ -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/(?P<type>album|playlist|track|artist)/(?P<id>[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
|
||||
|
||||
85
voicelink/spotify/objects.py
Normal file
85
voicelink/spotify/objects.py
Normal file
@@ -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"<Voicelink.spotify.Track name={self.name} artists={self.artists} "
|
||||
f"length={self.length} id={self.id}>"
|
||||
)
|
||||
|
||||
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"<Voicelink.spotify.Album name={self.name} artists={self.artists} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
|
||||
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"<Voicelink.spotify.Artist name={self.name} owner={self.owner} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
|
||||
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"<Voicelink.spotify.Playlist name={self.name} owner={self.owner} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
@@ -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"<Voicelink.spotify.Playlist name={self.name} owner={self.owner} id={self.id} "
|
||||
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
|
||||
)
|
||||
@@ -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"<Voicelink.spotify.Track name={self.name} artists={self.artists} "
|
||||
f"length={self.length} id={self.id}>"
|
||||
)
|
||||
Reference in New Issue
Block a user