From c58786ed3f96f04aee0cbe21f6e37c2fe2e5cf04 Mon Sep 17 00:00:00 2001 From: cloudwithax Date: Fri, 10 Mar 2023 06:54:20 -0500 Subject: [PATCH] fix spotify/am clients and fix grabbing sessionid --- pomice/applemusic/client.py | 8 +++++--- pomice/pool.py | 14 ++++++++++---- pomice/spotify/client.py | 12 +++++++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pomice/applemusic/client.py b/pomice/applemusic/client.py index 655f9af..643076d 100644 --- a/pomice/applemusic/client.py +++ b/pomice/applemusic/client.py @@ -24,15 +24,17 @@ class Client: and translating it to a valid Lavalink track. No client auth is required here. """ - def __init__(self, node: Node) -> None: + def __init__(self) -> None: self.token: str = None self.expiry: datetime = None - self.node = node - self.session = self.node._session + self.session: aiohttp.ClientSession = None self.headers = None async def request_token(self): + if not self.session: + self.session = aiohttp.ClientSession() + async with self.session.get("https://music.apple.com/assets/index.919fe17f.js") as resp: if resp.status != 200: raise AppleMusicRequestException( diff --git a/pomice/pool.py b/pomice/pool.py index 8fca43b..01416a5 100644 --- a/pomice/pool.py +++ b/pomice/pool.py @@ -107,11 +107,11 @@ class Node: if self._spotify_client_id and self._spotify_client_secret: self._spotify_client: spotify.Client = spotify.Client( - self, self._spotify_client_id, self._spotify_client_secret + self._spotify_client_id, self._spotify_client_secret ) if apple_music: - self._apple_music_client = applemusic.Client(self) + self._apple_music_client = applemusic.Client() self._bot.add_listener(self._update_handler, "on_socket_response") @@ -212,7 +212,7 @@ class Node: return if op == "ready": - self._session_id = data.get("sessionId") + self._session_id = data["sessionId"] if "guildId" in data: if not (player := self._players.get(int(data["guildId"]))): @@ -322,6 +322,12 @@ class Node: await self._websocket.close() await self._session.close() + if self._spotify_client: + await self._spotify_client.session.close() + + if self._apple_music_client: + await self._apple_music_client.session.close() + del self._pool._nodes[self._identifier] self.available = False self._task.cancel() @@ -339,7 +345,7 @@ class Node: """ data: dict = await self.send(method="GET", path="decodetrack", query=f"encodedTrack={identifier}") - return Track(track_id=identifier, ctx=ctx, info=data) + return Track(track_id=identifier, ctx=ctx, info=data, track_type=TrackType(data['sourceName'])) async def get_tracks( self, diff --git a/pomice/spotify/client.py b/pomice/spotify/client.py index 063c262..f3669a2 100644 --- a/pomice/spotify/client.py +++ b/pomice/spotify/client.py @@ -2,6 +2,7 @@ from __future__ import annotations import re import time +import aiohttp import orjson as json from base64 import b64encode @@ -9,8 +10,7 @@ from typing import TYPE_CHECKING from .exceptions import InvalidSpotifyURL, SpotifyRequestException from .objects import * -if TYPE_CHECKING: - from ..pool import Node + GRANT_URL = "https://accounts.spotify.com/api/token" @@ -26,12 +26,11 @@ class Client: for any Spotify URL you throw at it. """ - def __init__(self, node: Node, client_id: str, client_secret: str) -> None: + def __init__(self, client_id: str, client_secret: str) -> None: self._client_id = client_id self._client_secret = client_secret - self.node = node - self.session = self.node._session + self.session: aiohttp.ClientSession = None self._bearer_token: str = None self._expiry = 0 @@ -42,6 +41,9 @@ class Client: async def _fetch_bearer_token(self) -> None: _data = {"grant_type": "client_credentials"} + if not self.session: + self.session = aiohttp.ClientSession() + async with self.session.post(GRANT_URL, data=_data, headers=self._grant_headers) as resp: if resp.status != 200: raise SpotifyRequestException(