🚚 move auth to new file

This commit is contained in:
oskvr37
2024-07-20 21:59:19 +02:00
parent 67a326dd8c
commit 3df9f2e0a7
3 changed files with 59 additions and 22 deletions
+17 -22
View File
@@ -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()
+28
View File
@@ -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()
+14
View File
@@ -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