tecuts commited on
Commit
535c780
·
verified ·
1 Parent(s): 518d0b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -2,14 +2,16 @@ 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 = 'https://www.qobuz.com/api.json/0.2/'
13
  TOKEN = 'yn-HVbPSnrnLazYtBbhTNCHpny-JcyE5LqrHkJnLiv047BJO2SxS_lmDVVN6UnqLv4EvA_5F-lHGY56hIgpfJg'
14
  APP_ID = '579939560'
15
 
@@ -21,11 +23,8 @@ HEADERS = {
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."""
@@ -59,20 +58,27 @@ 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'{BASE}track/getFileUrl?format_id=27&intent=stream&track_id={track_id}&request_ts={timestamp}&request_sig={rSig}'
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
- return {"download_url": file_url}
 
 
 
 
 
 
 
 
78
 
 
 
 
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_URL = 'https://www.qobuz.com/api.json/0.2/'
15
  TOKEN = 'yn-HVbPSnrnLazYtBbhTNCHpny-JcyE5LqrHkJnLiv047BJO2SxS_lmDVVN6UnqLv4EvA_5F-lHGY56hIgpfJg'
16
  APP_ID = '579939560'
17
 
 
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."""
 
58
  rSigRaw = f'trackgetFileUrlformat_id27intentstreamtrack_id{track_id}{timestamp}fa31fc13e7a28e7d70bb61e91aa9e178'
59
  rSig = md5(rSigRaw)
60
 
61
+ download_url = f'{BASE_URL}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
+ # Serve the ALAC file
84
+ return FileResponse(alac_file_path, filename=os.path.basename(alac_file_path))