fix spotify/am clients and fix grabbing sessionid

This commit is contained in:
cloudwithax 2023-03-10 06:54:20 -05:00
parent d9137f6b29
commit c58786ed3f
3 changed files with 22 additions and 12 deletions

View File

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

View File

@ -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,

View File

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