Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import requests
|
| 2 |
import hashlib
|
| 3 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Constants
|
| 6 |
BASE_URL = 'https://www.qobuz.com/api.json/0.2/'
|
|
@@ -13,35 +17,34 @@ HEADERS = {
|
|
| 13 |
'X-User-Auth-Token': TOKEN,
|
| 14 |
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6'
|
| 15 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def md5(string):
|
| 18 |
"""Generate MD5 hash of a string."""
|
| 19 |
return hashlib.md5(string.encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
"""Fetch results for a search query."""
|
| 23 |
-
print('Fetching results...')
|
| 24 |
-
try:
|
| 25 |
-
search_url = f'{BASE_URL}catalog/search?query={input}&offset=0&limit=10'
|
| 26 |
-
response = requests.get(search_url, headers=HEADERS)
|
| 27 |
-
response.raise_for_status()
|
| 28 |
-
|
| 29 |
-
tracks = response.json().get('tracks', {}).get('items', [])
|
| 30 |
-
if tracks:
|
| 31 |
-
results = [{
|
| 32 |
-
'url': f'https://open.qobuz.com/track/{item["id"]}',
|
| 33 |
-
'title': f'{item["title"]} by {item["performer"]["name"]}'
|
| 34 |
-
} for item in tracks]
|
| 35 |
-
|
| 36 |
-
# Display the results
|
| 37 |
-
display_results(results)
|
| 38 |
-
else:
|
| 39 |
-
print('No results found.')
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def fetch_data_for_url(url):
|
| 45 |
"""Fetch data for a specific track URL."""
|
| 46 |
print(f'Fetching data for: {url}')
|
| 47 |
track_id_match = re.search(r'\d+$', url)
|
|
@@ -55,28 +58,21 @@ def fetch_data_for_url(url):
|
|
| 55 |
rSig = md5(rSigRaw)
|
| 56 |
|
| 57 |
download_url = f'{BASE_URL}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
except requests.exceptions.RequestException:
|
| 70 |
-
|
| 71 |
|
| 72 |
-
def display_results(results):
|
| 73 |
-
"""Display search results."""
|
| 74 |
-
for result in results:
|
| 75 |
-
print(f'{result["title"]} - {result["url"]}')
|
| 76 |
|
| 77 |
-
# Example usage:
|
| 78 |
-
input_query = input('Enter your search query or URL: ').strip()
|
| 79 |
-
if input_query.startswith('http://') or input_query.startswith('https://'):
|
| 80 |
-
fetch_data_for_url(input_query)
|
| 81 |
-
else:
|
| 82 |
-
fetch_results(input_query)
|
|
|
|
| 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/'
|
|
|
|
| 17 |
'X-User-Auth-Token': TOKEN,
|
| 18 |
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6'
|
| 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)
|
| 27 |
+
|
| 28 |
+
BASE_URL = "https://tecuts-request.hf.space"
|
| 29 |
|
| 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)
|
|
|
|
| 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 |
+
# try:
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|