get objects in place for apple music client

This commit is contained in:
cloudwithax 2023-02-05 13:08:49 -05:00
parent 809bb4aa3f
commit d08f07ffdc
4 changed files with 80 additions and 33 deletions

View File

@ -2,6 +2,8 @@ import re
import aiohttp import aiohttp
import json import json
from .objects import *
AM_URL_REGEX = re.compile(r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>[^?]+)") AM_URL_REGEX = re.compile(r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>[^?]+)")
AM_SINGLE_IN_ALBUM_REGEX = re.compile(r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>.+)(\?i=)(?P<id2>.+)") AM_SINGLE_IN_ALBUM_REGEX = re.compile(r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>.+)(\?i=)(?P<id2>.+)")
AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}" AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}"
@ -56,23 +58,6 @@ class Client:
print(self.token) 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: async with self.session.get(request_url, headers=self.headers) as resp:
print(resp.status) print(resp.status)
data = await resp.json() data = await resp.json()
@ -80,6 +65,21 @@ class Client:
with open('yes.txt', 'w') as file: with open('yes.txt', 'w') as file:
file.write(json.dumps(data)) 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)
await self.session.close() await self.session.close()

View File

@ -1,16 +1,50 @@
class Track: class Song:
def __init__(self) -> None: 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"<Pomice.applemusic.Track name={self.name} artists={self.artists} "
f"length={self.length} id={self.id} isrc={self.isrc}>"
)
class Playlist:
def __init__(self, data: dict) -> None:
pass pass
def __repr__(self) -> str:
return (
f"<Pomice.applemusic.Playlist name={self.name} owner={self.owner} id={self.id} "
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
)
class Album: class Album:
def __init__(self) -> None: def __init__(self, data: dict) -> None:
pass pass
class Playlist: def __repr__(self) -> str:
def __init__(self) -> None: return (
pass f"<Pomice.applemusic.Album name={self.name} artists={self.artists} id={self.id} "
f"total_tracks={self.total_tracks} tracks={self.tracks}>"
)
class Artist: class Artist:
def __init__(self) -> None: def __init__(self, data: dict) -> None:
pass pass
def __repr__(self) -> str:
return (
f"<Pomice.applemusic.Artist name={self.name} id={self.id} "
f"tracks={self.tracks}>"
)

View File

@ -75,6 +75,10 @@ class InvalidSpotifyClientAuthorization(PomiceException):
"""No Spotify client authorization was provided for track searching.""" """No Spotify client authorization was provided for track searching."""
pass pass
class AppleMusicNotEnabled(PomiceException):
"""An Apple Music Link was passed in when Apple Music functionality was not enabled."""
pass
class QueueException(Exception): class QueueException(Exception):
"""Base Pomice queue exception.""" """Base Pomice queue exception."""
pass pass

View File

@ -22,6 +22,7 @@ from . import (
from .enums import SearchType, NodeAlgorithm from .enums import SearchType, NodeAlgorithm
from .exceptions import ( from .exceptions import (
AppleMusicNotEnabled,
InvalidSpotifyClientAuthorization, InvalidSpotifyClientAuthorization,
NodeConnectionFailure, NodeConnectionFailure,
NodeCreationError, NodeCreationError,
@ -358,6 +359,12 @@ class Node:
if AM_URL_REGEX.match(query): 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) await self._apple_music_client.search(query=query)
@ -573,6 +580,7 @@ class NodePool:
spotify_client_id: Optional[str] = None, spotify_client_id: Optional[str] = None,
spotify_client_secret: Optional[str] = None, spotify_client_secret: Optional[str] = None,
session: Optional[aiohttp.ClientSession] = None, session: Optional[aiohttp.ClientSession] = None,
apple_music: bool = False
) -> Node: ) -> Node:
"""Creates a Node object to be then added into the node pool. """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, pool=cls, bot=bot, host=host, port=port, password=password,
identifier=identifier, secure=secure, heartbeat=heartbeat, identifier=identifier, secure=secure, heartbeat=heartbeat,
spotify_client_id=spotify_client_id, 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() await node.connect()