Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
-
import requests
|
| 2 |
import hashlib
|
| 3 |
import time
|
|
|
|
|
|
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
BASE_URL = 'https://www.qobuz.com/api.json/0.2/'
|
|
@@ -19,8 +21,6 @@ HEADERS = {
|
|
| 19 |
}
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
# Define a global temporary download directory
|
| 25 |
global_download_dir = tempfile.mkdtemp()
|
| 26 |
os.makedirs(global_download_dir, exist_ok=True)
|
|
@@ -30,27 +30,29 @@ BASE_URL = "https://tecuts-request.hf.space"
|
|
| 30 |
def md5(string):
|
| 31 |
"""Generate MD5 hash of a string."""
|
| 32 |
return hashlib.md5(string.encode('utf-8')).hexdigest()
|
| 33 |
-
|
| 34 |
@app.get('/')
|
| 35 |
def main():
|
| 36 |
return {"appStatus": "running"}
|
| 37 |
|
| 38 |
@app.post('/track')
|
| 39 |
-
|
| 40 |
-
def fetch_data_for_url:
|
| 41 |
try:
|
| 42 |
data = await request.json()
|
| 43 |
except Exception as e:
|
| 44 |
logging.error(f"Error parsing JSON: {e}")
|
| 45 |
raise HTTPException(status_code=400, detail="Invalid JSON")
|
|
|
|
| 46 |
url = data.get('url')
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
"""Fetch data for a specific track URL."""
|
| 49 |
print(f'Fetching data for: {url}')
|
|
|
|
| 50 |
track_id_match = re.search(r'\d+$', url)
|
| 51 |
if not track_id_match:
|
| 52 |
print('Track ID not found in your input.')
|
| 53 |
-
|
| 54 |
|
| 55 |
track_id = track_id_match.group(0)
|
| 56 |
timestamp = int(time.time())
|
|
@@ -58,21 +60,5 @@ def fetch_data_for_url:
|
|
| 58 |
rSig = md5(rSigRaw)
|
| 59 |
|
| 60 |
download_url = f'{BASE_URL}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
|
| 61 |
-
return {
|
| 62 |
-
"download_url": download_url
|
| 63 |
-
}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
# download_response = requests.get(download_url, headers=HEADERS)
|
| 67 |
-
# download_response.raise_for_status()
|
| 68 |
-
#
|
| 69 |
-
# file_url = download_response.json().get('url')
|
| 70 |
-
# if file_url:
|
| 71 |
-
# print(f'Download link: {file_url}')
|
| 72 |
-
# else:
|
| 73 |
-
# print('Error fetching download URL.')
|
| 74 |
-
#
|
| 75 |
-
#except requests.exceptions.RequestException:
|
| 76 |
-
# print('Error fetching data for the URL.')
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 1 |
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 requests
|
| 10 |
|
| 11 |
# Constants
|
| 12 |
BASE_URL = 'https://www.qobuz.com/api.json/0.2/'
|
|
|
|
| 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)
|
|
|
|
| 30 |
def md5(string):
|
| 31 |
"""Generate MD5 hash of a string."""
|
| 32 |
return hashlib.md5(string.encode('utf-8')).hexdigest()
|
| 33 |
+
|
| 34 |
@app.get('/')
|
| 35 |
def main():
|
| 36 |
return {"appStatus": "running"}
|
| 37 |
|
| 38 |
@app.post('/track')
|
| 39 |
+
async def fetch_data_for_url(request: Request):
|
|
|
|
| 40 |
try:
|
| 41 |
data = await request.json()
|
| 42 |
except Exception as e:
|
| 43 |
logging.error(f"Error parsing JSON: {e}")
|
| 44 |
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 45 |
+
|
| 46 |
url = data.get('url')
|
| 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)
|
| 58 |
timestamp = int(time.time())
|
|
|
|
| 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}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|