From 3df9f2e0a7d537da02c51d7b5a46c84484deb6d8 Mon Sep 17 00:00:00 2001 From: oskvr37 Date: Sat, 20 Jul 2024 21:59:19 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20move=20auth=20to=20new=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tiddl/api.py | 39 +++++++++++++++++---------------------- tiddl/auth.py | 28 ++++++++++++++++++++++++++++ tiddl/types.py | 14 ++++++++++++++ 3 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 tiddl/auth.py diff --git a/tiddl/api.py b/tiddl/api.py index 4471573..88adb41 100644 --- a/tiddl/api.py +++ b/tiddl/api.py @@ -1,40 +1,35 @@ from requests import request, Response from typing import Literal -from .types import DeviceAuthData, AuthData - -AUTH_URL = "https://auth.tidal.com/v1/oauth2" -CLIENT_ID = "7m7Ap0JC9j1cOM3n" -CLIENT_SECRET = "vRAdA108tlvkJpTsGZS8rGZ7xTlbJ0qaZ2K9saEzsgY=" +from .types import SessionData class TidalApi: def request( - self, method: Literal["GET", "POST"], url: str, data=None, auth=None + self, + method: Literal["GET", "POST"], + url: str, + data=None, + auth=None, + headers=None, ) -> Response: - req = request(method, url, data=data, auth=auth) + req = request(method, url, data=data, auth=auth, headers=headers) if req.status_code != 200: - raise Exception(f"Bad response - {req.status_code}") + raise Exception(f"{req.text} - {req.status_code}") return req - def getDeviceAuth(self) -> DeviceAuthData: + def getSession(self, token: str) -> SessionData: return self.request( - "POST", - f"{AUTH_URL}/device_authorization", - {"client_id": CLIENT_ID, "scope": "r_usr+w_usr+w_sub"}, + "GET", + "https://api.tidal.com/v1/sessions", + headers={"authorization": f"Bearer {token}"}, ).json() - def getToken(self, device_code: str) -> AuthData: + def getPlaylists(self, token: str, user_id: int, country_code: str): return self.request( - "POST", - f"{AUTH_URL}/token", - { - "client_id": CLIENT_ID, - "device_code": device_code, - "grant_type": "urn:ietf:params:oauth:grant-type:device_code", - "scope": "r_usr+w_usr+w_sub", - }, - (CLIENT_ID, CLIENT_SECRET), + "GET", + f"https://api.tidalhifi.com/v1/users/{user_id}/playlists?countryCode={country_code}", + headers={"authorization": f"Bearer {token}"}, ).json() diff --git a/tiddl/auth.py b/tiddl/auth.py new file mode 100644 index 0000000..1dc0f60 --- /dev/null +++ b/tiddl/auth.py @@ -0,0 +1,28 @@ +from requests import request +from .types import DeviceAuthData, AuthData + +AUTH_URL = "https://auth.tidal.com/v1/oauth2" +CLIENT_ID = "7m7Ap0JC9j1cOM3n" +CLIENT_SECRET = "vRAdA108tlvkJpTsGZS8rGZ7xTlbJ0qaZ2K9saEzsgY=" + + +def getDeviceAuth() -> DeviceAuthData: + return request( + "POST", + f"{AUTH_URL}/device_authorization", + data={"client_id": CLIENT_ID, "scope": "r_usr+w_usr+w_sub"}, + ).json() + + +def getToken(device_code: str) -> AuthData: + return request( + "POST", + f"{AUTH_URL}/token", + data={ + "client_id": CLIENT_ID, + "device_code": device_code, + "grant_type": "urn:ietf:params:oauth:grant-type:device_code", + "scope": "r_usr+w_usr+w_sub", + }, + auth=(CLIENT_ID, CLIENT_SECRET), + ).json() diff --git a/tiddl/types.py b/tiddl/types.py index 5370c97..22c2578 100644 --- a/tiddl/types.py +++ b/tiddl/types.py @@ -47,3 +47,17 @@ class AuthData(TypedDict): refresh_token: str expires_in: int user_id: int + +class Client(TypedDict): + id: int + name: str + authorizedForOffline: bool + authorizedForOfflineDate: Optional[str] + +class SessionData(TypedDict): + sessionId: str + userId: int + countryCode: str + channelId: int + partnerId: int + client: Client