Upload 3 files
Browse files- app.py +27 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -16,6 +16,26 @@ from urllib.parse import urljoin
|
|
| 16 |
from requests.adapters import HTTPAdapter
|
| 17 |
from urllib3.util.retry import Retry
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
app = FastAPI(
|
| 20 |
title="Tidal FLAC Download API",
|
| 21 |
description="Get FLAC download URLs for Tidal tracks",
|
|
@@ -67,7 +87,10 @@ r_session = create_requests_session()
|
|
| 67 |
class TidalSession:
|
| 68 |
def __init__(self, session_data, client_id=None, client_secret=None):
|
| 69 |
self.session_data = session_data
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
self.access_token = session_data.get('access_token')
|
| 72 |
self.refresh_token = session_data.get('refresh_token')
|
| 73 |
self.token_type = 'Bearer'
|
|
@@ -76,6 +99,8 @@ class TidalSession:
|
|
| 76 |
# TV Client ID - Best for DASH XML and FLAC
|
| 77 |
self.client_id = client_id or 'fX2JxdmntZWK0ixT'
|
| 78 |
self.client_secret = client_secret or '1nqpgx8uvBdZigrx4hUPDV2hOwgYAAAG5DYXOr6uNf8='
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def auth_headers(self):
|
| 81 |
return {
|
|
@@ -118,7 +143,7 @@ class TidalSession:
|
|
| 118 |
|
| 119 |
def get_stream_url(self, track_id, quality):
|
| 120 |
# We use the standard params for v4 API
|
| 121 |
-
print(f"DEBUG: Requesting stream for {track_id} with quality {quality}")
|
| 122 |
return self._get(
|
| 123 |
f"tracks/{track_id}/playbackinfopostpaywall/v4",
|
| 124 |
{
|
|
|
|
| 16 |
from requests.adapters import HTTPAdapter
|
| 17 |
from urllib3.util.retry import Retry
|
| 18 |
|
| 19 |
+
# Try to load .env file for local development
|
| 20 |
+
try:
|
| 21 |
+
from dotenv import load_dotenv
|
| 22 |
+
load_dotenv()
|
| 23 |
+
except ImportError:
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
def parse_jwt_country_code(token):
|
| 27 |
+
"""Extract country code from Tidal access token"""
|
| 28 |
+
try:
|
| 29 |
+
if not token or '.' not in token:
|
| 30 |
+
return None
|
| 31 |
+
payload = token.split('.')[1]
|
| 32 |
+
# Fix padding
|
| 33 |
+
payload += '=' * (-len(payload) % 4)
|
| 34 |
+
decoded = json.loads(base64.b64decode(payload))
|
| 35 |
+
return decoded.get('cc')
|
| 36 |
+
except Exception:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
app = FastAPI(
|
| 40 |
title="Tidal FLAC Download API",
|
| 41 |
description="Get FLAC download URLs for Tidal tracks",
|
|
|
|
| 87 |
class TidalSession:
|
| 88 |
def __init__(self, session_data, client_id=None, client_secret=None):
|
| 89 |
self.session_data = session_data
|
| 90 |
+
# Extract from token if possible, fallback to session_data, then env, then US
|
| 91 |
+
token_cc = parse_jwt_country_code(session_data.get('access_token'))
|
| 92 |
+
self.country_code = token_cc or session_data.get('country_code') or os.environ.get('TIDAL_COUNTRY_CODE', 'US')
|
| 93 |
+
|
| 94 |
self.access_token = session_data.get('access_token')
|
| 95 |
self.refresh_token = session_data.get('refresh_token')
|
| 96 |
self.token_type = 'Bearer'
|
|
|
|
| 99 |
# TV Client ID - Best for DASH XML and FLAC
|
| 100 |
self.client_id = client_id or 'fX2JxdmntZWK0ixT'
|
| 101 |
self.client_secret = client_secret or '1nqpgx8uvBdZigrx4hUPDV2hOwgYAAAG5DYXOr6uNf8='
|
| 102 |
+
|
| 103 |
+
print(f"✓ Tidal session initialized for region: {self.country_code}")
|
| 104 |
|
| 105 |
def auth_headers(self):
|
| 106 |
return {
|
|
|
|
| 143 |
|
| 144 |
def get_stream_url(self, track_id, quality):
|
| 145 |
# We use the standard params for v4 API
|
| 146 |
+
print(f"DEBUG: Requesting stream for {track_id} with quality {quality} (Region: {self.country_code})")
|
| 147 |
return self._get(
|
| 148 |
f"tracks/{track_id}/playbackinfopostpaywall/v4",
|
| 149 |
{
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ uvicorn
|
|
| 3 |
requests
|
| 4 |
urllib3
|
| 5 |
python-multipart
|
| 6 |
-
httpx
|
|
|
|
|
|
| 3 |
requests
|
| 4 |
urllib3
|
| 5 |
python-multipart
|
| 6 |
+
httpx
|
| 7 |
+
python-dotenv
|