Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ 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 uuid
|
|
@@ -26,6 +26,7 @@ app = FastAPI()
|
|
| 26 |
|
| 27 |
# Temporary directory for file storage
|
| 28 |
global_download_dir = tempfile.mkdtemp()
|
|
|
|
| 29 |
|
| 30 |
def md5(string):
|
| 31 |
"""Generate MD5 hash of a string."""
|
|
@@ -47,11 +48,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)
|
|
@@ -63,10 +64,12 @@ async def fetch_data_for_url(request: Request):
|
|
| 63 |
|
| 64 |
response = requests.get(download_url, headers=HEADERS)
|
| 65 |
if response.status_code != 200:
|
|
|
|
| 66 |
raise HTTPException(status_code=500, detail="Failed to fetch the track file URL")
|
| 67 |
|
| 68 |
file_url = response.json().get('url')
|
| 69 |
if not file_url:
|
|
|
|
| 70 |
raise HTTPException(status_code=500, detail="No file URL returned")
|
| 71 |
|
| 72 |
# Download the FLAC file
|
|
@@ -76,14 +79,27 @@ async def fetch_data_for_url(request: Request):
|
|
| 76 |
with open(flac_file_path, 'wb') as f:
|
| 77 |
for chunk in r.iter_content(chunk_size=8192):
|
| 78 |
f.write(chunk)
|
|
|
|
| 79 |
|
| 80 |
# Convert FLAC to ALAC
|
| 81 |
alac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.m4a')
|
| 82 |
subprocess.run(['ffmpeg', '-i', flac_file_path, '-c:a', 'alac', alac_file_path], check=True)
|
|
|
|
| 83 |
|
| 84 |
-
# Generate
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
# Return the URL to the
|
| 89 |
return {"url": alac_file_url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import requests
|
| 6 |
import subprocess
|
| 7 |
from fastapi import FastAPI, Request, HTTPException
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
import uuid
|
|
|
|
| 26 |
|
| 27 |
# Temporary directory for file storage
|
| 28 |
global_download_dir = tempfile.mkdtemp()
|
| 29 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 30 |
|
| 31 |
def md5(string):
|
| 32 |
"""Generate MD5 hash of a string."""
|
|
|
|
| 48 |
if not url:
|
| 49 |
raise HTTPException(status_code=400, detail="URL is required")
|
| 50 |
|
| 51 |
+
logging.info(f'Fetching data for: {url}')
|
| 52 |
|
| 53 |
track_id_match = re.search(r'\d+$', url)
|
| 54 |
if not track_id_match:
|
| 55 |
+
logging.error('Track ID not found in your input.')
|
| 56 |
raise HTTPException(status_code=400, detail="Track ID not found in URL")
|
| 57 |
|
| 58 |
track_id = track_id_match.group(0)
|
|
|
|
| 64 |
|
| 65 |
response = requests.get(download_url, headers=HEADERS)
|
| 66 |
if response.status_code != 200:
|
| 67 |
+
logging.error(f"Failed to fetch the track file URL: {response.status_code}")
|
| 68 |
raise HTTPException(status_code=500, detail="Failed to fetch the track file URL")
|
| 69 |
|
| 70 |
file_url = response.json().get('url')
|
| 71 |
if not file_url:
|
| 72 |
+
logging.error("No file URL returned from Qobuz")
|
| 73 |
raise HTTPException(status_code=500, detail="No file URL returned")
|
| 74 |
|
| 75 |
# Download the FLAC file
|
|
|
|
| 79 |
with open(flac_file_path, 'wb') as f:
|
| 80 |
for chunk in r.iter_content(chunk_size=8192):
|
| 81 |
f.write(chunk)
|
| 82 |
+
logging.info(f"FLAC file downloaded at {flac_file_path}")
|
| 83 |
|
| 84 |
# Convert FLAC to ALAC
|
| 85 |
alac_file_path = os.path.join(global_download_dir, f'{uuid.uuid4()}.m4a')
|
| 86 |
subprocess.run(['ffmpeg', '-i', flac_file_path, '-c:a', 'alac', alac_file_path], check=True)
|
| 87 |
+
logging.info(f"Converted to ALAC: {alac_file_path}")
|
| 88 |
|
| 89 |
+
# Generate the file URL for ALAC
|
| 90 |
+
alac_file_url = f'{BASE_URL}/file/{os.path.basename(alac_file_path)}'
|
| 91 |
+
logging.info(f"Returning ALAC file URL: {alac_file_url}")
|
| 92 |
|
| 93 |
+
# Return the ALAC file URL to the client as JSON
|
| 94 |
return {"url": alac_file_url}
|
| 95 |
+
|
| 96 |
+
# Mount StaticFiles to serve files from the global download directory
|
| 97 |
+
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
|
| 98 |
+
|
| 99 |
+
# Middleware to set the correct MIME type
|
| 100 |
+
@app.middleware("http")
|
| 101 |
+
async def set_mime_type_middleware(request: Request, call_next):
|
| 102 |
+
response = await call_next(request)
|
| 103 |
+
if request.url.path.endswith(".m4a"):
|
| 104 |
+
response.headers["Content-Type"] = "audio/mp4"
|
| 105 |
+
return response
|