Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,13 @@ import hashlib
|
|
| 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
|
| 10 |
-
|
| 11 |
-
BASE_URL = 'https://tecuts-request.hf.space'
|
| 12 |
|
| 13 |
# Constants
|
| 14 |
BASE = 'https://www.qobuz.com/api.json/0.2/'
|
|
@@ -23,11 +23,8 @@ HEADERS = {
|
|
| 23 |
}
|
| 24 |
app = FastAPI()
|
| 25 |
|
| 26 |
-
#
|
| 27 |
global_download_dir = tempfile.mkdtemp()
|
| 28 |
-
os.makedirs(global_download_dir, exist_ok=True)
|
| 29 |
-
|
| 30 |
-
BASE_URL = "https://tecuts-request.hf.space"
|
| 31 |
|
| 32 |
def md5(string):
|
| 33 |
"""Generate MD5 hash of a string."""
|
|
@@ -63,4 +60,29 @@ async def fetch_data_for_url(request: Request):
|
|
| 63 |
|
| 64 |
download_url = f'{BASE}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.responses import FileResponse
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
+
import uuid
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Constants
|
| 14 |
BASE = 'https://www.qobuz.com/api.json/0.2/'
|
|
|
|
| 23 |
}
|
| 24 |
app = FastAPI()
|
| 25 |
|
| 26 |
+
# Temporary directory for file storage
|
| 27 |
global_download_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def md5(string):
|
| 30 |
"""Generate MD5 hash of a string."""
|
|
|
|
| 60 |
|
| 61 |
download_url = f'{BASE}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
|
| 62 |
|
| 63 |
+
response = requests.get(download_url, headers=HEADERS)
|
| 64 |
+
if response.status_code != 200:
|
| 65 |
+
raise HTTPException(status_code=500, detail="Failed to fetch the track file URL")
|
| 66 |
+
|
| 67 |
+
file_url = response.json().get('url')
|
| 68 |
+
if not file_url:
|
| 69 |
+
raise HTTPException(status_code=500, detail="No file URL returned")
|
| 70 |
+
|
| 71 |
+
# Download the FLAC file
|
| 72 |
+
flac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.flac')
|
| 73 |
+
with requests.get(file_url, stream=True) as r:
|
| 74 |
+
r.raise_for_status()
|
| 75 |
+
with open(flac_file_path, 'wb') as f:
|
| 76 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 77 |
+
f.write(chunk)
|
| 78 |
+
|
| 79 |
+
# Convert FLAC to ALAC
|
| 80 |
+
alac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.m4a')
|
| 81 |
+
subprocess.run(['ffmpeg', '-i', flac_file_path, '-c:a', 'alac', alac_file_path], check=True)
|
| 82 |
+
|
| 83 |
+
# Generate a URL for the converted ALAC file
|
| 84 |
+
# You can use a URL or an endpoint that serves the file, e.g., your server file path or public URL
|
| 85 |
+
alac_file_url = f'/files/{os.path.basename(alac_file_path)}' # Adjust this depending on your setup
|
| 86 |
+
|
| 87 |
+
# Return the URL to the ALAC file as a JSON response
|
| 88 |
+
return {"url": alac_file_url}
|