Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,14 @@ import hashlib
|
|
2 |
import time
|
3 |
import re
|
4 |
import logging
|
5 |
-
import requests
|
6 |
-
import subprocess
|
7 |
from fastapi import FastAPI, Request, HTTPException
|
8 |
-
from fastapi.
|
9 |
import tempfile
|
10 |
import os
|
11 |
-
import
|
12 |
|
13 |
# Constants
|
14 |
-
|
15 |
TOKEN = 'yn-HVbPSnrnLazYtBbhTNCHpny-JcyE5LqrHkJnLiv047BJO2SxS_lmDVVN6UnqLv4EvA_5F-lHGY56hIgpfJg'
|
16 |
APP_ID = '579939560'
|
17 |
|
@@ -23,9 +21,11 @@ HEADERS = {
|
|
23 |
}
|
24 |
app = FastAPI()
|
25 |
|
26 |
-
#
|
27 |
global_download_dir = tempfile.mkdtemp()
|
28 |
-
|
|
|
|
|
29 |
|
30 |
def md5(string):
|
31 |
"""Generate MD5 hash of a string."""
|
@@ -47,11 +47,11 @@ async def fetch_data_for_url(request: Request):
|
|
47 |
if not url:
|
48 |
raise HTTPException(status_code=400, detail="URL is required")
|
49 |
|
50 |
-
|
51 |
|
52 |
track_id_match = re.search(r'\d+$', url)
|
53 |
if not track_id_match:
|
54 |
-
|
55 |
raise HTTPException(status_code=400, detail="Track ID not found in URL")
|
56 |
|
57 |
track_id = track_id_match.group(0)
|
@@ -59,20 +59,6 @@ async def fetch_data_for_url(request: Request):
|
|
59 |
rSigRaw = f'trackgetFileUrlformat_id27intentstreamtrack_id{track_id}{timestamp}fa31fc13e7a28e7d70bb61e91aa9e178'
|
60 |
rSig = md5(rSigRaw)
|
61 |
|
62 |
-
download_url = f'{
|
63 |
-
|
64 |
-
response = requests.get(download_url, headers=HEADERS)
|
65 |
-
if response.status_code != 200:
|
66 |
-
logging.error(f"Failed to fetch the track file URL: {response.status_code}")
|
67 |
-
raise HTTPException(status_code=500, detail="Failed to fetch the track file URL")
|
68 |
|
69 |
-
|
70 |
-
if not file_url:
|
71 |
-
logging.error("No file URL returned from Qobuz")
|
72 |
-
raise HTTPException(status_code=500, detail="No file URL returned")
|
73 |
-
|
74 |
-
|
75 |
-
return {"download_url": file_url}
|
76 |
-
|
77 |
-
# Mount StaticFiles to serve files from the global download directory
|
78 |
-
|
|
|
2 |
import time
|
3 |
import re
|
4 |
import logging
|
|
|
|
|
5 |
from fastapi import FastAPI, Request, HTTPException
|
6 |
+
from fastapi.responses import FileResponse
|
7 |
import tempfile
|
8 |
import os
|
9 |
+
import requests
|
10 |
|
11 |
# Constants
|
12 |
+
BASE_URL = 'https://www.qobuz.com/api.json/0.2/'
|
13 |
TOKEN = 'yn-HVbPSnrnLazYtBbhTNCHpny-JcyE5LqrHkJnLiv047BJO2SxS_lmDVVN6UnqLv4EvA_5F-lHGY56hIgpfJg'
|
14 |
APP_ID = '579939560'
|
15 |
|
|
|
21 |
}
|
22 |
app = FastAPI()
|
23 |
|
24 |
+
# Define a global temporary download directory
|
25 |
global_download_dir = tempfile.mkdtemp()
|
26 |
+
os.makedirs(global_download_dir, exist_ok=True)
|
27 |
+
|
28 |
+
BASE_URL = "https://tecuts-request.hf.space"
|
29 |
|
30 |
def md5(string):
|
31 |
"""Generate MD5 hash of a string."""
|
|
|
47 |
if not url:
|
48 |
raise HTTPException(status_code=400, detail="URL is required")
|
49 |
|
50 |
+
print(f'Fetching data for: {url}')
|
51 |
|
52 |
track_id_match = re.search(r'\d+$', url)
|
53 |
if not track_id_match:
|
54 |
+
print('Track ID not found in your input.')
|
55 |
raise HTTPException(status_code=400, detail="Track ID not found in URL")
|
56 |
|
57 |
track_id = track_id_match.group(0)
|
|
|
59 |
rSigRaw = f'trackgetFileUrlformat_id27intentstreamtrack_id{track_id}{timestamp}fa31fc13e7a28e7d70bb61e91aa9e178'
|
60 |
rSig = md5(rSigRaw)
|
61 |
|
62 |
+
download_url = f'{BASE_URL}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
return {"download_url": download_url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|