Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import hashlib
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# Constants
|
| 6 |
+
BASE_URL = 'https://www.qobuz.com/api.json/0.2/'
|
| 7 |
+
TOKEN = 'yn-HVbPSnrnLazYtBbhTNCHpny-JcyE5LqrHkJnLiv047BJO2SxS_lmDVVN6UnqLv4EvA_5F-lHGY56hIgpfJg'
|
| 8 |
+
APP_ID = '579939560'
|
| 9 |
+
|
| 10 |
+
# Custom Headers
|
| 11 |
+
HEADERS = {
|
| 12 |
+
'X-App-Id': APP_ID,
|
| 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 |
+
def fetch_results(input):
|
| 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 |
+
except requests.exceptions.RequestException:
|
| 42 |
+
print('Error fetching results.')
|
| 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)
|
| 48 |
+
if not track_id_match:
|
| 49 |
+
print('Track ID not found in your input.')
|
| 50 |
+
return
|
| 51 |
+
|
| 52 |
+
track_id = track_id_match.group(0)
|
| 53 |
+
timestamp = int(time.time())
|
| 54 |
+
rSigRaw = f'trackgetFileUrlformat_id27intentstreamtrack_id{track_id}{timestamp}fa31fc13e7a28e7d70bb61e91aa9e178'
|
| 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 |
+
download_response = requests.get(download_url, headers=HEADERS)
|
| 61 |
+
download_response.raise_for_status()
|
| 62 |
+
|
| 63 |
+
file_url = download_response.json().get('url')
|
| 64 |
+
if file_url:
|
| 65 |
+
print(f'Download link: {file_url}')
|
| 66 |
+
else:
|
| 67 |
+
print('Error fetching download URL.')
|
| 68 |
+
|
| 69 |
+
except requests.exceptions.RequestException:
|
| 70 |
+
print('Error fetching data for the URL.')
|
| 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)
|