diff --git a/pomice/applemusic/client.py b/pomice/applemusic/client.py index 74478a0..118059b 100644 --- a/pomice/applemusic/client.py +++ b/pomice/applemusic/client.py @@ -2,6 +2,8 @@ import re import aiohttp import json +from .objects import * + AM_URL_REGEX = re.compile(r"https?://music.apple.com/(?P[a-zA-Z]{2})/(?Palbum|playlist|song|artist)/(?P.+)/(?P[^?]+)") AM_SINGLE_IN_ALBUM_REGEX = re.compile(r"https?://music.apple.com/(?P[a-zA-Z]{2})/(?Palbum|playlist|song|artist)/(?P.+)/(?P.+)(\?i=)(?P.+)") AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}" @@ -56,29 +58,27 @@ class Client: print(self.token) - - if type == "playlist": - async with self.session.get(request_url, headers=self.headers) as resp: - print(resp.status) - data = await resp.json() - - elif type == "album": - async with self.session.get(request_url, headers=self.headers) as resp: - print(resp.status) - data = await resp.json() - - elif type == "song": - async with self.session.get(request_url, headers=self.headers) as resp: - print(resp.status) - data = await resp.json() - - elif type == "artist": - async with self.session.get(request_url, headers=self.headers) as resp: - print(resp.status) - data = await resp.json() + async with self.session.get(request_url, headers=self.headers) as resp: + print(resp.status) + data = await resp.json() with open('yes.txt', 'w') as file: file.write(json.dumps(data)) + + if type == "playlist": + return Playlist(data) + + elif type == "album": + return Album(data) + + elif type == "song": + return Song(data) + + elif type == "artist": + return Artist(data) + + + diff --git a/pomice/applemusic/objects.py b/pomice/applemusic/objects.py index 19d158e..5271b28 100644 --- a/pomice/applemusic/objects.py +++ b/pomice/applemusic/objects.py @@ -1,16 +1,50 @@ -class Track: - def __init__(self) -> None: - pass - - -class Album: - def __init__(self) -> None: - pass - +class Song: + def __init__(self, data: dict) -> None: + self.track_data = ["data"][0] + self.name = self.track_data["attributes"]["name"] + self.url = self.track_data["atrributes"]["url"] + self.isrc = self.track_data["atrributes"]["isrc"] + self.length = self.track_data["atrributes"]["durationInMillis"] + self.id = self.track_data["id"] + self.artists = self.track_data["atrributes"]["artistName"] + self.image = self.track_data["atrributes"]["artwork"]["url"].replace("{w}x{h}", f'{self.track_data["atrributes"]["artwork"]["width"]}x{self.track_data["atrributes"]["artwork"]["height"]}') + + def __repr__(self) -> str: + return ( + f"" + ) + + class Playlist: - def __init__(self) -> None: + def __init__(self, data: dict) -> None: pass + def __repr__(self) -> str: + return ( + f"" + ) + + +class Album: + def __init__(self, data: dict) -> None: + pass + + def __repr__(self) -> str: + return ( + f"" + ) + + + class Artist: - def __init__(self) -> None: - pass \ No newline at end of file + def __init__(self, data: dict) -> None: + pass + + def __repr__(self) -> str: + return ( + f"" + ) \ No newline at end of file diff --git a/pomice/exceptions.py b/pomice/exceptions.py index 4af483f..8d2cb24 100644 --- a/pomice/exceptions.py +++ b/pomice/exceptions.py @@ -75,6 +75,10 @@ class InvalidSpotifyClientAuthorization(PomiceException): """No Spotify client authorization was provided for track searching.""" pass +class AppleMusicNotEnabled(PomiceException): + """An Apple Music Link was passed in when Apple Music functionality was not enabled.""" + pass + class QueueException(Exception): """Base Pomice queue exception.""" pass diff --git a/pomice/pool.py b/pomice/pool.py index 465fc4f..07bfe35 100644 --- a/pomice/pool.py +++ b/pomice/pool.py @@ -22,6 +22,7 @@ from . import ( from .enums import SearchType, NodeAlgorithm from .exceptions import ( + AppleMusicNotEnabled, InvalidSpotifyClientAuthorization, NodeConnectionFailure, NodeCreationError, @@ -358,6 +359,12 @@ class Node: if AM_URL_REGEX.match(query): + if not self._apple_music_client: + raise AppleMusicNotEnabled( + "You must have Apple Music functionality enabled in order to play Apple Music tracks." + "Please set apple_music to True in your Node class." + ) + await self._apple_music_client.search(query=query) @@ -573,6 +580,7 @@ class NodePool: spotify_client_id: Optional[str] = None, spotify_client_secret: Optional[str] = None, session: Optional[aiohttp.ClientSession] = None, + apple_music: bool = False ) -> Node: """Creates a Node object to be then added into the node pool. @@ -585,7 +593,8 @@ class NodePool: pool=cls, bot=bot, host=host, port=port, password=password, identifier=identifier, secure=secure, heartbeat=heartbeat, spotify_client_id=spotify_client_id, - session=session, spotify_client_secret=spotify_client_secret + session=session, spotify_client_secret=spotify_client_secret, + apple_music=apple_music ) await node.connect()