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 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_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}"
@ -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)

View File

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

View File

@ -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()