Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ceb727adb9 | |||
| bbea8ca493 | |||
| f567dd19bf | |||
| 3651833e2a | |||
| 8403b96306 | |||
| d977829e36 | |||
| 2aaf123c98 | |||
| 13567802a0 | |||
| d704782519 | |||
| 884c02278f | |||
| d6abe2bae3 | |||
| 7b858dd0ce | |||
| cfeb9a2ef2 | |||
| 81a78832ff | |||
| 071f20deff | |||
| 03de68ac7b | |||
| 77363f9e61 | |||
| 2df77120cf | |||
| e6e953b2ed | |||
| 72f17479e8 | |||
| 9286fba63c | |||
| 6bf7084959 | |||
| 03cc3d82a7 | |||
| 2acd6fcba1 | |||
| 85a5bb2321 | |||
| 70a955f531 | |||
| 71c8070ec0 | |||
| 177bc06b79 | |||
| 2aec9c0185 | |||
| a6a84cf869 | |||
| 3577574ad8 | |||
| 3696fc95a7 | |||
| effa462810 | |||
| 921faefecf | |||
| a4168450d1 | |||
| 7ba3efb75b | |||
| d18ba28864 | |||
| f8da9ecfd2 | |||
| 2588680846 | |||
| f3366f0554 | |||
| 2da2ea64ee | |||
| 7e52b8ab35 | |||
| 4b89a5e678 | |||
| c0677b3cb7 | |||
| 176e4566df | |||
| f319d0dcbb | |||
| 61c53655ff | |||
| 77bbc70c9b |
@@ -1,99 +0,0 @@
|
||||
import asyncio
|
||||
import zendriver as zd
|
||||
|
||||
async def get_metadata(page):
|
||||
max_attempts = 40
|
||||
attempts = 0
|
||||
|
||||
await asyncio.sleep(2)
|
||||
|
||||
await page.evaluate("""
|
||||
window.downloadInfo = null;
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async function(...args) {
|
||||
const [url, config] = args;
|
||||
if (url.includes('/api/load?url=%2Fapi%2Ffetch%2Fstream%2Fv2')) {
|
||||
const payload = JSON.parse(config.body);
|
||||
const title = document.querySelector('h1.svelte-6pt9ji').textContent;
|
||||
const artists = Array.from(document.querySelectorAll('h2.svelte-6pt9ji a.normal'))
|
||||
.map(a => a.textContent)
|
||||
.join(', ');
|
||||
const cover = document.querySelector('.svelte-6pt9ji .meta.svelte-6pt9ji a').href;
|
||||
|
||||
window.downloadInfo = {
|
||||
url: payload.url,
|
||||
cover: cover,
|
||||
title: title,
|
||||
artists: artists,
|
||||
token: payload.token.primary,
|
||||
expiry: payload.token.expiry
|
||||
};
|
||||
}
|
||||
return originalFetch.apply(this, args);
|
||||
};
|
||||
""")
|
||||
|
||||
await page.evaluate("""
|
||||
function waitForElement(selector) {
|
||||
return new Promise(resolve => {
|
||||
if (document.querySelector(selector)) {
|
||||
return resolve(document.querySelector(selector));
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(mutations => {
|
||||
if (document.querySelector(selector)) {
|
||||
observer.disconnect();
|
||||
resolve(document.querySelector(selector));
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(document.documentElement, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
(async () => {
|
||||
if (!window.location.hostname.includes('lucida.')) return;
|
||||
|
||||
await Promise.race([
|
||||
waitForElement('.d1-track button'),
|
||||
waitForElement('button[class*="download-button"]')
|
||||
]);
|
||||
|
||||
const clickDownloadButton = () => {
|
||||
const button = document.querySelector('.d1-track button') ||
|
||||
document.querySelector('button[class*="download-button"]');
|
||||
if (button) button.click();
|
||||
};
|
||||
|
||||
clickDownloadButton();
|
||||
})();
|
||||
""")
|
||||
|
||||
while attempts < max_attempts:
|
||||
download_info = await page.evaluate("window.downloadInfo")
|
||||
if download_info:
|
||||
return download_info
|
||||
|
||||
await asyncio.sleep(0.5)
|
||||
attempts += 1
|
||||
|
||||
raise TimeoutError("Timeout")
|
||||
|
||||
async def main():
|
||||
browser = await zd.start(headless=False)
|
||||
try:
|
||||
track_id = "2plbrEY59IikOBgBGLjaoe"
|
||||
url = f"https://lucida.to/?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F{track_id}&country=auto&to=tidal"
|
||||
|
||||
page = await browser.get(url)
|
||||
download_info = await get_metadata(page)
|
||||
print(download_info)
|
||||
return download_info
|
||||
finally:
|
||||
await browser.stop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,120 +0,0 @@
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
import time
|
||||
import os
|
||||
import asyncio
|
||||
from GetMetadata import main as get_metadata
|
||||
|
||||
class TrackDownloader:
|
||||
def __init__(self):
|
||||
self.client = requests.Session()
|
||||
self.headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
|
||||
async def get_track_info(self):
|
||||
metadata = await get_metadata()
|
||||
return metadata
|
||||
|
||||
def sanitize_filename(self, filename):
|
||||
invalid_chars = '<>:"/\\|?*'
|
||||
for char in invalid_chars:
|
||||
filename = filename.replace(char, '')
|
||||
|
||||
filename = ' '.join(filename.split())
|
||||
filename = filename.replace(' ,', ',')
|
||||
filename = filename.replace(',', ', ')
|
||||
while ' ' in filename:
|
||||
filename = filename.replace(' ', ' ')
|
||||
filename = filename.rsplit('.', 1)
|
||||
filename[0] = filename[0].strip()
|
||||
return '.'.join(filename)
|
||||
|
||||
def download(self, metadata, output_dir):
|
||||
track_url = metadata['url']
|
||||
primary_token = metadata['token']
|
||||
expiry = metadata['expiry']
|
||||
|
||||
print(f"Starting download for: {track_url}")
|
||||
|
||||
initial_request = {
|
||||
"account": {"id": "auto", "type": "country"},
|
||||
"compat": "false",
|
||||
"downscale": "original",
|
||||
"handoff": True,
|
||||
"metadata": True,
|
||||
"private": True,
|
||||
"token": {
|
||||
"expiry": expiry,
|
||||
"primary": primary_token
|
||||
},
|
||||
"upload": {"enabled": False, "service": "pixeldrain"},
|
||||
"url": track_url
|
||||
}
|
||||
|
||||
response = self.client.post("https://lucida.to/api/load?url=/api/fetch/stream/v2",
|
||||
json=initial_request,
|
||||
headers=self.headers)
|
||||
|
||||
csrf_token = response.cookies.get('csrf_token')
|
||||
if csrf_token:
|
||||
self.headers['X-CSRF-Token'] = csrf_token
|
||||
|
||||
initial_response = response.json()
|
||||
|
||||
if not initial_response.get("success", False):
|
||||
raise Exception(f"Initial request failed: {initial_response.get('error', 'Unknown error')}")
|
||||
|
||||
handoff = initial_response["handoff"]
|
||||
server = initial_response["server"]
|
||||
|
||||
file_name = f"{metadata['title']} - {metadata['artists']}.flac"
|
||||
file_name = self.sanitize_filename(file_name)
|
||||
|
||||
completion_url = f"https://{server}.lucida.to/api/fetch/request/{handoff}"
|
||||
|
||||
print("Waiting for track processing to complete")
|
||||
while True:
|
||||
completion_response = self.client.get(completion_url, headers=self.headers).json()
|
||||
if completion_response["status"] == "completed":
|
||||
break
|
||||
elif completion_response["status"] == "error":
|
||||
raise Exception(f"API request failed: {completion_response.get('message', 'Unknown error')}")
|
||||
time.sleep(1)
|
||||
|
||||
download_url = f"https://{server}.lucida.to/api/fetch/request/{handoff}/download"
|
||||
print(f"Starting download of: {file_name}")
|
||||
|
||||
response = self.client.get(download_url, stream=True, headers=self.headers)
|
||||
total_size = int(response.headers.get('content-length', 0))
|
||||
|
||||
file_path = os.path.join(output_dir, file_name)
|
||||
|
||||
with open(file_path, 'wb') as file, tqdm(
|
||||
desc=file_name,
|
||||
total=total_size,
|
||||
unit='iB',
|
||||
unit_scale=True,
|
||||
unit_divisor=1024,
|
||||
) as progress_bar:
|
||||
for data in response.iter_content(chunk_size=1024):
|
||||
size = file.write(data)
|
||||
progress_bar.update(size)
|
||||
|
||||
print(f"Download completed: {file_path}")
|
||||
return file_path
|
||||
|
||||
async def main():
|
||||
downloader = TrackDownloader()
|
||||
output_dir = "."
|
||||
|
||||
try:
|
||||
metadata = await downloader.get_track_info()
|
||||
|
||||
downloaded_file = downloader.download(metadata, output_dir)
|
||||
print(f"File downloaded successfully: {downloaded_file}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,141 +0,0 @@
|
||||
import requests
|
||||
import time
|
||||
import os
|
||||
import asyncio
|
||||
from GetMetadata import main as get_metadata
|
||||
|
||||
class TrackDownloader:
|
||||
def __init__(self):
|
||||
self.client = requests.Session()
|
||||
self.headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
self.progress_callback = None
|
||||
self.filename_format = 'title_artist'
|
||||
|
||||
def set_progress_callback(self, callback):
|
||||
self.progress_callback = callback
|
||||
|
||||
def set_filename_format(self, format_type):
|
||||
self.filename_format = format_type
|
||||
|
||||
def generate_filename(self, metadata):
|
||||
if self.filename_format == 'artist_title':
|
||||
filename = f"{metadata['artists']} - {metadata['title']}.flac"
|
||||
else:
|
||||
filename = f"{metadata['title']} - {metadata['artists']}.flac"
|
||||
return self.sanitize_filename(filename)
|
||||
|
||||
async def get_track_info(self):
|
||||
metadata = await get_metadata()
|
||||
return metadata
|
||||
|
||||
def sanitize_filename(self, filename):
|
||||
invalid_chars = '<>:"/\\|?*'
|
||||
for char in invalid_chars:
|
||||
filename = filename.replace(char, '')
|
||||
|
||||
filename = ' '.join(filename.split())
|
||||
filename = filename.replace(' ,', ',')
|
||||
filename = filename.replace(',', ', ')
|
||||
while ' ' in filename:
|
||||
filename = filename.replace(' ', ' ')
|
||||
filename = filename.rsplit('.', 1)
|
||||
filename[0] = filename[0].strip()
|
||||
return '.'.join(filename)
|
||||
|
||||
def download(self, metadata, output_dir):
|
||||
track_url = metadata['url']
|
||||
primary_token = metadata['token']
|
||||
expiry = metadata['expiry']
|
||||
|
||||
print(f"Starting download for: {track_url}")
|
||||
|
||||
initial_request = {
|
||||
"account": {"id": "auto", "type": "country"},
|
||||
"compat": "false",
|
||||
"downscale": "original",
|
||||
"handoff": True,
|
||||
"metadata": True,
|
||||
"private": True,
|
||||
"token": {
|
||||
"expiry": expiry,
|
||||
"primary": primary_token
|
||||
},
|
||||
"upload": {"enabled": False, "service": "pixeldrain"},
|
||||
"url": track_url
|
||||
}
|
||||
|
||||
response = self.client.post("https://lucida.to/api/load?url=/api/fetch/stream/v2",
|
||||
json=initial_request,
|
||||
headers=self.headers)
|
||||
|
||||
csrf_token = response.cookies.get('csrf_token')
|
||||
if csrf_token:
|
||||
self.headers['X-CSRF-Token'] = csrf_token
|
||||
|
||||
initial_response = response.json()
|
||||
|
||||
if not initial_response.get("success", False):
|
||||
raise Exception(f"Initial request failed: {initial_response.get('error', 'Unknown error')}")
|
||||
|
||||
handoff = initial_response["handoff"]
|
||||
server = initial_response["server"]
|
||||
|
||||
file_name = self.generate_filename(metadata)
|
||||
|
||||
completion_url = f"https://{server}.lucida.to/api/fetch/request/{handoff}"
|
||||
|
||||
print("Waiting for track processing to complete")
|
||||
while True:
|
||||
completion_response = self.client.get(completion_url, headers=self.headers).json()
|
||||
if completion_response["status"] == "completed":
|
||||
break
|
||||
elif completion_response["status"] == "error":
|
||||
raise Exception(f"API request failed: {completion_response.get('message', 'Unknown error')}")
|
||||
time.sleep(1)
|
||||
|
||||
download_url = f"https://{server}.lucida.to/api/fetch/request/{handoff}/download"
|
||||
print(f"Starting download of: {file_name}")
|
||||
|
||||
response = self.client.get(download_url, stream=True, headers=self.headers)
|
||||
total_size = int(response.headers.get('content-length', 0))
|
||||
downloaded_size = 0
|
||||
|
||||
file_path = os.path.join(output_dir, file_name)
|
||||
|
||||
try:
|
||||
with open(file_path, 'wb') as file:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
file.write(chunk)
|
||||
downloaded_size += len(chunk)
|
||||
if self.progress_callback:
|
||||
self.progress_callback(downloaded_size, total_size)
|
||||
|
||||
if downloaded_size == 0:
|
||||
raise Exception("No data received from server")
|
||||
|
||||
return file_path
|
||||
|
||||
except Exception as e:
|
||||
if os.path.exists(file_path) and os.path.getsize(file_path) == 0:
|
||||
try:
|
||||
os.remove(file_path)
|
||||
except:
|
||||
pass
|
||||
raise e
|
||||
|
||||
async def main():
|
||||
downloader = TrackDownloader()
|
||||
output_dir = "."
|
||||
|
||||
try:
|
||||
metadata = await downloader.get_track_info()
|
||||
downloaded_file = downloader.download(metadata, output_dir)
|
||||
print(f"File downloaded successfully: {downloaded_file}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,22 +1,31 @@
|
||||
[](https://github.com/afkarxyz/SpotifyFLAC/releases)
|
||||
[](https://github.com/afkarxyz/SpotiFLAC/releases)
|
||||
|
||||
**Spotify FLAC** allows you to download Spotify tracks in true, lossless FLAC format, providing the highest audio quality for an exceptional listening experience.
|
||||

|
||||
|
||||
> [!NOTE]
|
||||
> Requires **Google Chrome**
|
||||
<div align="center">
|
||||
<b>SpotiFLAC</b> allows you to download Spotify tracks in true FLAC format through services like Tidal, Amazon Music and Deezer with the help of Lucida.
|
||||
</div>
|
||||
|
||||
#### [Download](https://github.com/afkarxyz/SpotifyFLAC/releases/download/v1.1/SpotifyFLAC.exe) Spotify FLAC
|
||||
### [Download](https://github.com/afkarxyz/SpotiFLAC/releases/download/v2.2/SpotiFLAC.exe)
|
||||
|
||||
#
|
||||
|
||||
> [!WARNING]
|
||||
Sometimes, the **download speed** from Lucida can be fast or slow; it varies unpredictably.
|
||||
|
||||
## Screenshots
|
||||
|
||||

|
||||

|
||||
|
||||
> - When **Headless** is enabled, the browser runs in the background without a graphical interface, improving performance and allowing seamless automation.
|
||||
> - When **Fallback** is enabled, it will use another server.
|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||
|
||||
> When **Fallback** is enabled, it will use the backup server `Lucida.su`
|
||||
|
||||
## Lossless Audio Check
|
||||
|
||||
@@ -24,4 +33,4 @@
|
||||
|
||||

|
||||
|
||||
#### [Download](https://github.com/afkarxyz/SpotifyFLAC/releases/download/v0/FLAC-Checker.zip) FLAC Checker
|
||||
#### [Download](https://github.com/afkarxyz/SpotiFLAC/releases/download/v0/FLAC-Checker.zip) FLAC Checker
|
||||
|
||||
+1156
File diff suppressed because it is too large
Load Diff
-503
@@ -1,503 +0,0 @@
|
||||
import sys
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
|
||||
QHBoxLayout, QLabel, QLineEdit, QPushButton,
|
||||
QProgressBar, QFileDialog, QCheckBox, QRadioButton,
|
||||
QGroupBox)
|
||||
from PyQt6.QtCore import QThread, pyqtSignal, Qt, QSettings, QTimer
|
||||
from PyQt6.QtGui import QIcon, QPixmap, QCursor
|
||||
from GetMetadata import get_metadata
|
||||
from LucidaDownloader import TrackDownloader
|
||||
|
||||
class ImageDownloader(QThread):
|
||||
finished = pyqtSignal(bytes)
|
||||
|
||||
def __init__(self, url):
|
||||
super().__init__()
|
||||
self.url = url
|
||||
|
||||
def run(self):
|
||||
import requests
|
||||
response = requests.get(self.url)
|
||||
if response.status_code == 200:
|
||||
self.finished.emit(response.content)
|
||||
|
||||
class MetadataFetcher(QThread):
|
||||
finished = pyqtSignal(dict)
|
||||
error = pyqtSignal(str)
|
||||
|
||||
def __init__(self, url, headless=True, use_fallback=False):
|
||||
super().__init__()
|
||||
self.url = url
|
||||
self.headless_mode = headless
|
||||
self.use_fallback = use_fallback
|
||||
self.max_retries = 3
|
||||
|
||||
def extract_track_id(self, url):
|
||||
if "track/" in url:
|
||||
return url.split("track/")[1].split("?")[0]
|
||||
return None
|
||||
|
||||
async def fetch_metadata(self, track_id):
|
||||
import zendriver as zd
|
||||
from asyncio import sleep
|
||||
|
||||
for attempt in range(self.max_retries):
|
||||
try:
|
||||
platform = "amazon" if self.use_fallback else "tidal"
|
||||
lucida_url = f"https://lucida.to/?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F{track_id}&country=auto&to={platform}"
|
||||
browser = await zd.start(headless=self.headless_mode)
|
||||
try:
|
||||
page = await browser.get(lucida_url)
|
||||
return await get_metadata(page)
|
||||
finally:
|
||||
await browser.stop()
|
||||
except Exception as e:
|
||||
if "refused" in str(e).lower() and attempt < self.max_retries - 1:
|
||||
await sleep(2 * (attempt + 1))
|
||||
continue
|
||||
raise e
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
track_id = self.extract_track_id(self.url)
|
||||
if not track_id:
|
||||
self.error.emit("Invalid Spotify URL")
|
||||
return
|
||||
|
||||
metadata = asyncio.run(self.fetch_metadata(track_id))
|
||||
if metadata:
|
||||
self.finished.emit(metadata)
|
||||
else:
|
||||
self.error.emit("Failed to fetch track metadata")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
if "refused" in error_msg.lower():
|
||||
self.error.emit("Connection refused. Please check your internet connection and try again.")
|
||||
elif "timeout" in error_msg.lower():
|
||||
self.error.emit("Connection timed out. Please check your internet connection and try again.")
|
||||
else:
|
||||
self.error.emit(f"Error: {error_msg}")
|
||||
|
||||
class DownloaderWorker(QThread):
|
||||
progress = pyqtSignal(int)
|
||||
status = pyqtSignal(str)
|
||||
finished = pyqtSignal(str)
|
||||
error = pyqtSignal(str)
|
||||
|
||||
def __init__(self, metadata, output_dir, filename_format='title_artist'):
|
||||
super().__init__()
|
||||
self.metadata = metadata
|
||||
self.output_dir = output_dir
|
||||
self.filename_format = filename_format
|
||||
self.downloader = TrackDownloader()
|
||||
self.last_update_time = 0
|
||||
self.last_downloaded_size = 0
|
||||
|
||||
def format_size(self, size_bytes):
|
||||
return f"{size_bytes / (1024 * 1024):.2f}MB"
|
||||
|
||||
def format_speed(self, speed_bytes):
|
||||
return f"{speed_bytes * 8 / (1024 * 1024):.2f}Mbps"
|
||||
|
||||
def progress_callback(self, downloaded_size, total_size):
|
||||
current_time = time.time()
|
||||
if current_time - self.last_update_time >= 0.5: # Update every 0.5 seconds
|
||||
progress = int((downloaded_size / total_size) * 100) if total_size > 0 else 0
|
||||
self.progress.emit(progress)
|
||||
|
||||
# Calculate speed
|
||||
time_diff = current_time - self.last_update_time
|
||||
if time_diff > 0:
|
||||
speed = (downloaded_size - self.last_downloaded_size) / time_diff
|
||||
status = f"Downloading... {self.format_size(downloaded_size)}/{self.format_size(total_size)} | {self.format_speed(speed)}"
|
||||
self.status.emit(status)
|
||||
|
||||
self.last_update_time = current_time
|
||||
self.last_downloaded_size = downloaded_size
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.status.emit("Preparing...")
|
||||
self.downloader.set_progress_callback(self.progress_callback)
|
||||
self.downloader.set_filename_format(self.filename_format)
|
||||
self.progress.emit(0)
|
||||
downloaded_file = self.downloader.download(self.metadata, self.output_dir)
|
||||
self.progress.emit(100)
|
||||
self.finished.emit("Download complete!")
|
||||
except Exception as e:
|
||||
self.error.emit(f"Error: {str(e)}")
|
||||
|
||||
class SpotifyFlacGUI(QMainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.settings = QSettings('SpotifyFlac', 'Settings')
|
||||
self.setWindowTitle("Spotify FLAC")
|
||||
|
||||
icon_path = os.path.join(os.path.dirname(__file__), "icon.svg")
|
||||
if os.path.exists(icon_path):
|
||||
self.setWindowIcon(QIcon(icon_path))
|
||||
|
||||
self.setFixedWidth(600)
|
||||
self.setFixedHeight(180)
|
||||
|
||||
self.default_music_dir = str(Path.home() / "Music")
|
||||
if not os.path.exists(self.default_music_dir):
|
||||
os.makedirs(self.default_music_dir)
|
||||
|
||||
self.metadata = None
|
||||
self.init_ui()
|
||||
self.url_input.textChanged.connect(self.validate_url)
|
||||
self.load_settings()
|
||||
self.setup_settings_persistence()
|
||||
|
||||
def load_settings(self):
|
||||
headless = self.settings.value('headless', True, type=bool)
|
||||
fallback = self.settings.value('fallback', False, type=bool)
|
||||
format_type = self.settings.value('format', 'title_artist')
|
||||
output_dir = self.settings.value('output_dir', self.default_music_dir)
|
||||
self.headless_checkbox.setChecked(headless)
|
||||
self.fallback_checkbox.setChecked(fallback)
|
||||
self.format_title_artist.setChecked(format_type == 'title_artist')
|
||||
self.format_artist_title.setChecked(format_type == 'artist_title')
|
||||
self.dir_input.setText(output_dir)
|
||||
|
||||
def setup_settings_persistence(self):
|
||||
self.headless_checkbox.stateChanged.connect(
|
||||
lambda x: self.settings.setValue('headless', bool(x)))
|
||||
self.fallback_checkbox.stateChanged.connect(
|
||||
lambda x: self.settings.setValue('fallback', bool(x)))
|
||||
self.format_title_artist.toggled.connect(
|
||||
lambda x: self.settings.setValue('format', 'title_artist' if x else 'artist_title'))
|
||||
self.dir_input.textChanged.connect(
|
||||
lambda x: self.settings.setValue('output_dir', x))
|
||||
|
||||
def init_ui(self):
|
||||
central_widget = QWidget()
|
||||
self.setCentralWidget(central_widget)
|
||||
self.main_layout = QVBoxLayout(central_widget)
|
||||
self.main_layout.setContentsMargins(10, 10, 10, 10)
|
||||
|
||||
self.input_widget = QWidget()
|
||||
input_layout = QVBoxLayout(self.input_widget)
|
||||
input_layout.setSpacing(10)
|
||||
|
||||
url_layout = QHBoxLayout()
|
||||
url_label = QLabel("Track URL:")
|
||||
url_label.setFixedWidth(100)
|
||||
self.url_input = QLineEdit()
|
||||
self.url_input.setPlaceholderText("Please enter track URL")
|
||||
self.url_input.setClearButtonEnabled(True)
|
||||
self.fetch_button = QPushButton("Fetch")
|
||||
self.fetch_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.fetch_button.setFixedWidth(100)
|
||||
self.fetch_button.setEnabled(False)
|
||||
self.fetch_button.clicked.connect(self.fetch_track_info)
|
||||
url_layout.addWidget(url_label)
|
||||
url_layout.addWidget(self.url_input)
|
||||
url_layout.addWidget(self.fetch_button)
|
||||
input_layout.addLayout(url_layout)
|
||||
|
||||
dir_layout = QHBoxLayout()
|
||||
dir_label = QLabel("Output Directory:")
|
||||
dir_label.setFixedWidth(100)
|
||||
self.dir_input = QLineEdit(self.default_music_dir)
|
||||
self.dir_button = QPushButton("Browse")
|
||||
self.dir_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.dir_button.setFixedWidth(100)
|
||||
dir_layout.addWidget(dir_label)
|
||||
dir_layout.addWidget(self.dir_input)
|
||||
dir_layout.addWidget(self.dir_button)
|
||||
self.dir_button.clicked.connect(self.select_directory)
|
||||
input_layout.addLayout(dir_layout)
|
||||
|
||||
settings_group = QGroupBox("Settings")
|
||||
settings_layout = QHBoxLayout(settings_group)
|
||||
settings_layout.setContentsMargins(10, 0, 10, 10)
|
||||
settings_layout.setSpacing(20)
|
||||
|
||||
settings_container = QWidget()
|
||||
settings_container_layout = QHBoxLayout(settings_container)
|
||||
settings_container_layout.setContentsMargins(0, 0, 0, 0)
|
||||
settings_container_layout.setSpacing(20)
|
||||
|
||||
self.headless_checkbox = QCheckBox("Headless")
|
||||
self.headless_checkbox.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.headless_checkbox.setChecked(True)
|
||||
settings_container_layout.addWidget(self.headless_checkbox)
|
||||
|
||||
self.fallback_checkbox = QCheckBox("Fallback")
|
||||
self.fallback_checkbox.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.fallback_checkbox.setChecked(False)
|
||||
settings_container_layout.addWidget(self.fallback_checkbox)
|
||||
|
||||
format_widget = QWidget()
|
||||
format_layout = QHBoxLayout(format_widget)
|
||||
format_layout.setContentsMargins(0, 0, 0, 0)
|
||||
format_layout.setSpacing(15)
|
||||
|
||||
format_label = QLabel("Filename Format:")
|
||||
self.format_title_artist = QRadioButton("Title - Artist")
|
||||
self.format_artist_title = QRadioButton("Artist - Title")
|
||||
self.format_title_artist.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.format_artist_title.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.format_title_artist.setChecked(True)
|
||||
|
||||
format_layout.addWidget(format_label)
|
||||
format_layout.addWidget(self.format_title_artist)
|
||||
format_layout.addWidget(self.format_artist_title)
|
||||
|
||||
settings_container_layout.addWidget(format_widget)
|
||||
|
||||
settings_layout.addStretch()
|
||||
settings_layout.addWidget(settings_container)
|
||||
settings_layout.addStretch()
|
||||
|
||||
input_layout.addWidget(settings_group)
|
||||
self.main_layout.addWidget(self.input_widget)
|
||||
|
||||
self.track_widget = QWidget()
|
||||
self.track_widget.hide()
|
||||
track_layout = QHBoxLayout(self.track_widget)
|
||||
track_layout.setContentsMargins(0, 0, 0, 0)
|
||||
track_layout.setSpacing(10)
|
||||
|
||||
cover_container = QWidget()
|
||||
cover_layout = QVBoxLayout(cover_container)
|
||||
cover_layout.setContentsMargins(0, 0, 0, 0)
|
||||
cover_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
|
||||
self.cover_label = QLabel()
|
||||
self.cover_label.setFixedSize(100, 100)
|
||||
self.cover_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
cover_layout.addWidget(self.cover_label)
|
||||
track_layout.addWidget(cover_container)
|
||||
|
||||
track_details_container = QWidget()
|
||||
track_details_layout = QVBoxLayout(track_details_container)
|
||||
track_details_layout.setContentsMargins(0, 0, 0, 0)
|
||||
track_details_layout.setSpacing(2)
|
||||
track_details_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
|
||||
self.title_label = QLabel()
|
||||
self.title_label.setStyleSheet("font-size: 14px; font-weight: bold;")
|
||||
self.title_label.setWordWrap(True)
|
||||
self.title_label.setMinimumWidth(400)
|
||||
|
||||
self.artist_label = QLabel()
|
||||
self.artist_label.setStyleSheet("font-size: 12px;")
|
||||
self.artist_label.setWordWrap(True)
|
||||
self.artist_label.setMinimumWidth(400)
|
||||
|
||||
track_details_layout.addWidget(self.title_label)
|
||||
track_details_layout.addWidget(self.artist_label)
|
||||
track_layout.addWidget(track_details_container, stretch=1)
|
||||
track_layout.addStretch()
|
||||
self.main_layout.addWidget(self.track_widget)
|
||||
|
||||
self.download_button = QPushButton("Download")
|
||||
self.download_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.download_button.setFixedWidth(100)
|
||||
self.download_button.clicked.connect(self.button_clicked)
|
||||
self.download_button.hide()
|
||||
|
||||
self.cancel_button = QPushButton("Cancel")
|
||||
self.cancel_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.cancel_button.setFixedWidth(100)
|
||||
self.cancel_button.clicked.connect(self.cancel_clicked)
|
||||
self.cancel_button.hide()
|
||||
|
||||
self.open_button = QPushButton("Open")
|
||||
self.open_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||
self.open_button.setFixedWidth(100)
|
||||
self.open_button.clicked.connect(self.open_output_directory)
|
||||
self.open_button.hide()
|
||||
|
||||
download_layout = QHBoxLayout()
|
||||
download_layout.addStretch()
|
||||
download_layout.addWidget(self.open_button)
|
||||
download_layout.addWidget(self.download_button)
|
||||
download_layout.addWidget(self.cancel_button)
|
||||
download_layout.addStretch()
|
||||
self.main_layout.addLayout(download_layout)
|
||||
|
||||
self.progress_bar = QProgressBar()
|
||||
self.progress_bar.hide()
|
||||
self.main_layout.addWidget(self.progress_bar)
|
||||
|
||||
self.status_label = QLabel("")
|
||||
self.main_layout.addWidget(self.status_label)
|
||||
|
||||
def validate_url(self, url):
|
||||
url = url.strip()
|
||||
self.fetch_button.setEnabled(False)
|
||||
if not url:
|
||||
self.status_label.clear()
|
||||
return
|
||||
if "open.spotify.com/" not in url:
|
||||
self.status_label.setText("Please enter a valid Spotify URL")
|
||||
return
|
||||
if "/album/" in url:
|
||||
self.status_label.setText("Album URLs are not supported. Please enter a track URL.")
|
||||
return
|
||||
if "/playlist/" in url:
|
||||
self.status_label.setText("Playlist URLs are not supported. Please enter a track URL.")
|
||||
return
|
||||
if "/track/" not in url:
|
||||
self.status_label.setText("Please enter a valid Spotify track URL")
|
||||
return
|
||||
self.fetch_button.setEnabled(True)
|
||||
self.status_label.clear()
|
||||
|
||||
def fetch_track_info(self):
|
||||
url = self.url_input.text().strip()
|
||||
if not url:
|
||||
self.status_label.setText("Please enter a Track URL")
|
||||
return
|
||||
self.fetch_button.setEnabled(False)
|
||||
self.status_label.setText("Fetching track information...")
|
||||
headless = self.headless_checkbox.isChecked()
|
||||
use_fallback = self.fallback_checkbox.isChecked()
|
||||
self.fetcher = MetadataFetcher(url, headless=headless, use_fallback=use_fallback)
|
||||
self.fetcher.finished.connect(self.handle_track_info)
|
||||
self.fetcher.error.connect(self.handle_fetch_error)
|
||||
self.fetcher.start()
|
||||
|
||||
def handle_track_info(self, metadata):
|
||||
self.metadata = metadata
|
||||
self.fetch_button.setEnabled(True)
|
||||
self.title_label.setText(metadata['title'].strip())
|
||||
self.artist_label.setText(metadata['artists'].strip())
|
||||
self.image_downloader = ImageDownloader(metadata['cover'])
|
||||
self.image_downloader.finished.connect(self.update_cover_art)
|
||||
self.image_downloader.start()
|
||||
self.input_widget.hide()
|
||||
self.track_widget.show()
|
||||
self.download_button.show()
|
||||
self.cancel_button.show()
|
||||
self.status_label.clear()
|
||||
self.adjustWindowHeight()
|
||||
|
||||
def adjustWindowHeight(self):
|
||||
title_height = self.title_label.sizeHint().height()
|
||||
artist_height = self.artist_label.sizeHint().height()
|
||||
base_height = 180
|
||||
additional_height = max(0, (title_height + artist_height) - 40)
|
||||
new_height = min(300, base_height + additional_height)
|
||||
self.setFixedHeight(int(new_height))
|
||||
|
||||
def update_cover_art(self, image_data):
|
||||
pixmap = QPixmap()
|
||||
pixmap.loadFromData(image_data)
|
||||
scaled_pixmap = pixmap.scaled(100, 100, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
|
||||
self.cover_label.setPixmap(scaled_pixmap)
|
||||
|
||||
def handle_fetch_error(self, error):
|
||||
self.fetch_button.setEnabled(True)
|
||||
self.status_label.setText(f"Error fetching track info: {error}")
|
||||
|
||||
def select_directory(self):
|
||||
directory = QFileDialog.getExistingDirectory(self, "Select Output Directory")
|
||||
if directory:
|
||||
self.dir_input.setText(directory)
|
||||
|
||||
def open_output_directory(self):
|
||||
output_dir = self.dir_input.text().strip() or self.default_music_dir
|
||||
os.startfile(output_dir)
|
||||
|
||||
def cancel_clicked(self):
|
||||
self.track_widget.hide()
|
||||
self.input_widget.show()
|
||||
self.download_button.hide()
|
||||
self.cancel_button.hide()
|
||||
self.progress_bar.hide()
|
||||
self.progress_bar.setValue(0)
|
||||
self.status_label.clear()
|
||||
self.metadata = None
|
||||
self.fetch_button.setEnabled(True)
|
||||
self.setFixedHeight(180)
|
||||
|
||||
def button_clicked(self):
|
||||
if self.download_button.text() == "Clear":
|
||||
self.clear_form()
|
||||
else:
|
||||
self.start_download()
|
||||
|
||||
def clear_form(self):
|
||||
self.url_input.clear()
|
||||
self.progress_bar.hide()
|
||||
self.progress_bar.setValue(0)
|
||||
self.status_label.clear()
|
||||
self.download_button.setText("Download")
|
||||
self.download_button.hide()
|
||||
self.cancel_button.hide()
|
||||
self.open_button.hide()
|
||||
self.track_widget.hide()
|
||||
self.input_widget.show()
|
||||
self.metadata = None
|
||||
self.setFixedHeight(180)
|
||||
|
||||
def start_download(self):
|
||||
output_dir = self.dir_input.text().strip()
|
||||
if not self.metadata:
|
||||
self.status_label.setText("Please fetch track information first")
|
||||
return
|
||||
if not output_dir:
|
||||
output_dir = self.default_music_dir
|
||||
self.dir_input.setText(output_dir)
|
||||
|
||||
self.download_button.hide()
|
||||
self.cancel_button.hide()
|
||||
self.progress_bar.show()
|
||||
self.progress_bar.setValue(0)
|
||||
self.status_label.setText("Preparing...")
|
||||
|
||||
format_type = 'artist_title' if self.format_artist_title.isChecked() else 'title_artist'
|
||||
self.worker = DownloaderWorker(
|
||||
metadata=self.metadata,
|
||||
output_dir=output_dir,
|
||||
filename_format=format_type
|
||||
)
|
||||
|
||||
self.worker.progress.connect(self.update_progress)
|
||||
self.worker.status.connect(self.update_status)
|
||||
self.worker.finished.connect(self.download_finished)
|
||||
self.worker.error.connect(self.download_error)
|
||||
self.worker.start()
|
||||
|
||||
def update_status(self, status):
|
||||
self.status_label.setText(status)
|
||||
|
||||
def update_progress(self, value):
|
||||
self.progress_bar.setValue(value)
|
||||
|
||||
def download_finished(self, message):
|
||||
self.progress_bar.hide()
|
||||
self.status_label.setText(message)
|
||||
self.open_button.show()
|
||||
self.download_button.setText("Clear")
|
||||
self.download_button.show()
|
||||
self.cancel_button.hide()
|
||||
self.download_button.setEnabled(True)
|
||||
|
||||
def download_error(self, error_message):
|
||||
self.progress_bar.hide()
|
||||
self.status_label.setText(error_message)
|
||||
self.download_button.setText("Retry")
|
||||
self.download_button.show()
|
||||
self.cancel_button.show()
|
||||
self.download_button.setEnabled(True)
|
||||
self.cancel_button.setEnabled(True)
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
window = SpotifyFlacGUI()
|
||||
window.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
from time import sleep
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
import requests
|
||||
import json
|
||||
import hmac
|
||||
import time
|
||||
import hashlib
|
||||
from typing import Tuple, Callable
|
||||
|
||||
_TOTP_SECRET = bytearray([53,53,48,55,49,52,53,56,53,51,52,56,55,52,57,57,53,57,50,50,52,56,54,51,48,51,50,57,51,52,55])
|
||||
|
||||
def generate_totp(
|
||||
secret: bytes = _TOTP_SECRET,
|
||||
algorithm: Callable[[], object] = hashlib.sha1,
|
||||
digits: int = 6,
|
||||
counter_factory: Callable[[], int] = lambda: int(time.time()) // 30,
|
||||
) -> Tuple[str, int]:
|
||||
counter = counter_factory()
|
||||
hmac_result = hmac.new(
|
||||
secret, counter.to_bytes(8, byteorder="big"), algorithm
|
||||
).digest()
|
||||
|
||||
offset = hmac_result[-1] & 15
|
||||
truncated_value = (
|
||||
(hmac_result[offset] & 127) << 24
|
||||
| (hmac_result[offset + 1] & 255) << 16
|
||||
| (hmac_result[offset + 2] & 255) << 8
|
||||
| (hmac_result[offset + 3] & 255)
|
||||
)
|
||||
return (
|
||||
str(truncated_value % (10**digits)).zfill(digits),
|
||||
counter * 30_000,
|
||||
)
|
||||
|
||||
token_url = 'https://open.spotify.com/get_access_token'
|
||||
playlist_base_url = 'https://api.spotify.com/v1/playlists/{}'
|
||||
album_base_url = 'https://api.spotify.com/v1/albums/{}'
|
||||
track_base_url = 'https://api.spotify.com/v1/tracks/{}'
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
||||
'Accept': 'application/json',
|
||||
'Accept-Language': 'en-US,en;q=0.9',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'sec-ch-ua-platform': '"Windows"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-origin',
|
||||
'Referer': 'https://open.spotify.com/',
|
||||
'Origin': 'https://open.spotify.com'
|
||||
}
|
||||
|
||||
class SpotifyInvalidUrlException(Exception):
|
||||
pass
|
||||
|
||||
class SpotifyWebsiteParserException(Exception):
|
||||
pass
|
||||
|
||||
def parse_uri(uri):
|
||||
u = urlparse(uri)
|
||||
if u.netloc == "embed.spotify.com":
|
||||
if not u.query:
|
||||
raise SpotifyInvalidUrlException("ERROR: url {} is not supported".format(uri))
|
||||
qs = parse_qs(u.query)
|
||||
return parse_uri(qs['uri'][0])
|
||||
|
||||
if not u.scheme and not u.netloc:
|
||||
return {"type": "playlist", "id": u.path}
|
||||
|
||||
if u.scheme == "spotify":
|
||||
parts = uri.split(":")
|
||||
else:
|
||||
if u.netloc != "open.spotify.com" and u.netloc != "play.spotify.com":
|
||||
raise SpotifyInvalidUrlException("ERROR: url {} is not supported".format(uri))
|
||||
parts = u.path.split("/")
|
||||
|
||||
if parts[1] == "embed":
|
||||
parts = parts[1:]
|
||||
|
||||
l = len(parts)
|
||||
if l == 3 and parts[1] in ["album", "track", "playlist"]:
|
||||
return {"type": parts[1], "id": parts[2]}
|
||||
if l == 5 and parts[3] == "playlist":
|
||||
return {"type": parts[3], "id": parts[4]}
|
||||
|
||||
raise SpotifyInvalidUrlException("ERROR: unable to determine Spotify URL type or type is unsupported.")
|
||||
|
||||
def get_json_from_api(api_url, access_token):
|
||||
headers.update({'Authorization': 'Bearer {}'.format(access_token)})
|
||||
|
||||
req = requests.get(api_url, headers=headers, timeout=10)
|
||||
|
||||
if req.status_code == 429:
|
||||
seconds = int(req.headers.get("Retry-After", "5")) + 1
|
||||
print(f"INFO: rate limited! Sleeping for {seconds} seconds")
|
||||
sleep(seconds)
|
||||
return None
|
||||
|
||||
if req.status_code != 200:
|
||||
raise SpotifyWebsiteParserException(f"ERROR: {api_url} gave us not a 200. Instead: {req.status_code}")
|
||||
|
||||
return req.json()
|
||||
|
||||
def get_raw_spotify_data(spotify_url):
|
||||
url_info = parse_uri(spotify_url)
|
||||
|
||||
try:
|
||||
totp, timestamp = generate_totp()
|
||||
|
||||
params = {
|
||||
"reason": "init",
|
||||
"productType": "web-player",
|
||||
"totp": totp,
|
||||
"totpVer": 5,
|
||||
"ts": timestamp,
|
||||
}
|
||||
|
||||
req = requests.get(token_url, headers=headers, params=params, timeout=10)
|
||||
if req.status_code != 200:
|
||||
return {"error": f"Failed to get access token. Status code: {req.status_code}"}
|
||||
token = req.json()
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to get access token: {str(e)}"}
|
||||
|
||||
raw_data = {}
|
||||
|
||||
if url_info['type'] == "playlist":
|
||||
try:
|
||||
playlist_data = get_json_from_api(
|
||||
playlist_base_url.format(url_info["id"]),
|
||||
token["accessToken"]
|
||||
)
|
||||
if not playlist_data:
|
||||
return {"error": "Failed to get playlist data"}
|
||||
|
||||
raw_data = playlist_data
|
||||
|
||||
tracks = []
|
||||
tracks_url = f'https://api.spotify.com/v1/playlists/{url_info["id"]}/tracks?limit=100'
|
||||
while tracks_url:
|
||||
track_data = get_json_from_api(tracks_url, token["accessToken"])
|
||||
if not track_data:
|
||||
break
|
||||
|
||||
tracks.extend(track_data['items'])
|
||||
tracks_url = track_data.get('next')
|
||||
|
||||
raw_data['tracks']['items'] = tracks
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to get playlist data: {str(e)}"}
|
||||
|
||||
elif url_info["type"] == "album":
|
||||
try:
|
||||
album_data = get_json_from_api(
|
||||
album_base_url.format(url_info["id"]),
|
||||
token["accessToken"]
|
||||
)
|
||||
if not album_data:
|
||||
return {"error": "Failed to get album data"}
|
||||
|
||||
album_data['_token'] = token["accessToken"]
|
||||
raw_data = album_data
|
||||
|
||||
tracks = []
|
||||
tracks_url = f'{album_base_url.format(url_info["id"])}/tracks?limit=50'
|
||||
while tracks_url:
|
||||
track_data = get_json_from_api(tracks_url, token["accessToken"])
|
||||
if not track_data:
|
||||
break
|
||||
|
||||
tracks.extend(track_data['items'])
|
||||
tracks_url = track_data.get('next')
|
||||
|
||||
raw_data['tracks']['items'] = tracks
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to get album data: {str(e)}"}
|
||||
|
||||
elif url_info["type"] == "track":
|
||||
try:
|
||||
track_data = get_json_from_api(
|
||||
track_base_url.format(url_info["id"]),
|
||||
token["accessToken"]
|
||||
)
|
||||
if not track_data:
|
||||
return {"error": "Failed to get track data"}
|
||||
|
||||
raw_data = track_data
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to get track data: {str(e)}"}
|
||||
|
||||
return raw_data
|
||||
|
||||
def format_track_data(track_data):
|
||||
artists = []
|
||||
for artist in track_data['artists']:
|
||||
artists.append(artist['name'])
|
||||
|
||||
image_url = track_data.get('album', {}).get('images', [{}])[0].get('url', '')
|
||||
|
||||
return {
|
||||
"track": {
|
||||
"artists": ", ".join(artists),
|
||||
"name": track_data.get('name', ''),
|
||||
"album_name": track_data.get('album', {}).get('name', ''),
|
||||
"duration_ms": track_data.get('duration_ms', 0),
|
||||
"images": image_url,
|
||||
"release_date": track_data.get('album', {}).get('release_date', ''),
|
||||
"track_number": track_data.get('track_number', 0),
|
||||
"external_urls": track_data.get('external_urls', {}).get('spotify', '')
|
||||
}
|
||||
}
|
||||
|
||||
def format_album_data(album_data):
|
||||
artists = []
|
||||
for artist in album_data['artists']:
|
||||
artists.append(artist['name'])
|
||||
|
||||
image_url = album_data.get('images', [{}])[0].get('url', '')
|
||||
|
||||
track_list = []
|
||||
for track in album_data.get('tracks', {}).get('items', []):
|
||||
track_artists = []
|
||||
for artist in track.get('artists', []):
|
||||
track_artists.append(artist['name'])
|
||||
|
||||
track_list.append({
|
||||
"artists": ", ".join(track_artists),
|
||||
"name": track.get('name', ''),
|
||||
"album_name": album_data.get('name', ''),
|
||||
"duration_ms": track.get('duration_ms', 0),
|
||||
"images": image_url,
|
||||
"release_date": album_data.get('release_date', ''),
|
||||
"track_number": track.get('track_number', 0),
|
||||
"external_urls": track.get('external_urls', {}).get('spotify', '')
|
||||
})
|
||||
|
||||
return {
|
||||
"album_info": {
|
||||
"total_tracks": album_data.get('total_tracks', 0),
|
||||
"name": album_data.get('name', ''),
|
||||
"release_date": album_data.get('release_date', ''),
|
||||
"artists": ", ".join(artists),
|
||||
"images": image_url
|
||||
},
|
||||
"track_list": track_list
|
||||
}
|
||||
|
||||
def format_playlist_data(playlist_data):
|
||||
image_url = playlist_data.get('images', [{}])[0].get('url', '')
|
||||
|
||||
track_list = []
|
||||
for item in playlist_data.get('tracks', {}).get('items', []):
|
||||
track = item.get('track', {})
|
||||
artists = []
|
||||
for artist in track.get('artists', []):
|
||||
artists.append(artist['name'])
|
||||
|
||||
track_image = track.get('album', {}).get('images', [{}])[0].get('url', '')
|
||||
|
||||
track_list.append({
|
||||
"artists": ", ".join(artists),
|
||||
"name": track.get('name', ''),
|
||||
"album_name": track.get('album', {}).get('name', ''),
|
||||
"duration_ms": track.get('duration_ms', 0),
|
||||
"images": track_image,
|
||||
"release_date": track.get('album', {}).get('release_date', ''),
|
||||
"track_number": track.get('track_number', 0),
|
||||
"external_urls": track.get('external_urls', {}).get('spotify', '')
|
||||
})
|
||||
|
||||
return {
|
||||
"playlist_info": {
|
||||
"tracks": {"total": playlist_data.get('tracks', {}).get('total', 0)},
|
||||
"followers": {"total": playlist_data.get('followers', {}).get('total', 0)},
|
||||
"owner": {
|
||||
"display_name": playlist_data.get('owner', {}).get('display_name', ''),
|
||||
"name": playlist_data.get('name', ''),
|
||||
"images": image_url
|
||||
}
|
||||
},
|
||||
"track_list": track_list
|
||||
}
|
||||
|
||||
def process_spotify_data(raw_data, data_type):
|
||||
if not raw_data or "error" in raw_data:
|
||||
return {"error": "Invalid data provided"}
|
||||
|
||||
try:
|
||||
if data_type == "track":
|
||||
return format_track_data(raw_data)
|
||||
elif data_type == "album":
|
||||
return format_album_data(raw_data)
|
||||
elif data_type == "playlist":
|
||||
return format_playlist_data(raw_data)
|
||||
else:
|
||||
return {"error": "Invalid data type"}
|
||||
except Exception as e:
|
||||
return {"error": f"Error processing data: {str(e)}"}
|
||||
|
||||
def get_filtered_data(spotify_url):
|
||||
raw_data = get_raw_spotify_data(spotify_url)
|
||||
if raw_data and "error" not in raw_data:
|
||||
url_info = parse_uri(spotify_url)
|
||||
filtered_data = process_spotify_data(raw_data, url_info['type'])
|
||||
return filtered_data
|
||||
return {"error": "Failed to get raw data"}
|
||||
|
||||
if __name__ == '__main__':
|
||||
playlist = "https://open.spotify.com/playlist/37i9dQZEVXbNG2KDcFcKOF"
|
||||
album = "https://open.spotify.com/album/7kFyd5oyJdVX2pIi6P4iHE"
|
||||
song = "https://open.spotify.com/track/4wJ5Qq0jBN4ajy7ouZIV1c"
|
||||
|
||||
filtered_playlist = get_filtered_data(playlist)
|
||||
print(json.dumps(filtered_playlist, indent=2))
|
||||
|
||||
filtered_album = get_filtered_data(album)
|
||||
print(json.dumps(filtered_album, indent=2))
|
||||
|
||||
filtered_track = get_filtered_data(song)
|
||||
print(json.dumps(filtered_track, indent=2))
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
import requests
|
||||
import time
|
||||
import os
|
||||
import asyncio
|
||||
import re
|
||||
import base64
|
||||
|
||||
class TrackDownloader:
|
||||
def __init__(self, use_fallback=False):
|
||||
self.client = requests.Session()
|
||||
self.headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
self.progress_callback = None
|
||||
self.use_fallback = use_fallback
|
||||
self.base_domain = "lucida.su" if use_fallback else "lucida.to"
|
||||
|
||||
def set_progress_callback(self, callback):
|
||||
self.progress_callback = callback
|
||||
|
||||
def generate_filename(self, track_id, service):
|
||||
return f"{track_id}_{service}.flac"
|
||||
|
||||
async def get_track_info(self, track_id, service="amazon", use_fallback=None):
|
||||
if use_fallback is None:
|
||||
use_fallback = self.use_fallback
|
||||
|
||||
domain_type = "su" if use_fallback else "to"
|
||||
|
||||
spotify_url = f"https://open.spotify.com/track/{track_id}"
|
||||
|
||||
result = self.convert_spotify_link(spotify_url, service, domain_type)
|
||||
|
||||
if "error" in result:
|
||||
raise Exception(f"Failed to get track info: {result['error']}")
|
||||
|
||||
result["track_id"] = track_id
|
||||
|
||||
return result
|
||||
|
||||
def convert_spotify_link(self, spotify_url, target_service="amazon", domain_type="to"):
|
||||
track_id_match = re.search(r'track/([a-zA-Z0-9]+)', spotify_url)
|
||||
if not track_id_match:
|
||||
return {"error": "Invalid Spotify URL"}
|
||||
|
||||
domain = "lucida.to" if domain_type == "to" else "lucida.su"
|
||||
base_url = f"https://{domain}"
|
||||
|
||||
headers = {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
||||
"Accept-Language": "id-ID,id;q=0.9",
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"Host": domain,
|
||||
"Pragma": "no-cache",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
}
|
||||
|
||||
try:
|
||||
headers["Referer"] = f"{base_url}/?url={spotify_url}&country=auto"
|
||||
|
||||
request_params = {
|
||||
"url": spotify_url,
|
||||
"country": "auto",
|
||||
"to": target_service
|
||||
}
|
||||
|
||||
session = requests.Session()
|
||||
session.verify = True
|
||||
|
||||
response = session.get(
|
||||
base_url,
|
||||
params=request_params,
|
||||
headers=headers,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
html_content = response.text
|
||||
|
||||
token_match = re.search(r'token:"([^"]+)"', html_content)
|
||||
token_expiry_match = re.search(r'tokenExpiry:(\d+)', html_content)
|
||||
|
||||
token = token_match.group(1) if token_match else None
|
||||
token_expiry = int(token_expiry_match.group(1)) if token_expiry_match else None
|
||||
|
||||
url = None
|
||||
url_patterns = [
|
||||
r'"url":"([^"]+)"',
|
||||
r'href="(https?://[^"]*' + re.escape(target_service) + r'[^"]*track[^"]*)"',
|
||||
]
|
||||
|
||||
for pattern in url_patterns:
|
||||
url_match = re.search(pattern, html_content)
|
||||
if url_match:
|
||||
url = url_match.group(1).replace('\\/', '/')
|
||||
break
|
||||
|
||||
if not url:
|
||||
redirect_patterns = [
|
||||
r'url=([^&"]+)',
|
||||
r'href="([^"]+)"',
|
||||
r'window\.location\.href\s*=\s*[\'"]([^\'"]+)[\'"]',
|
||||
]
|
||||
|
||||
for pattern in redirect_patterns:
|
||||
matches = re.finditer(pattern, html_content)
|
||||
for match in matches:
|
||||
potential_url = match.group(1)
|
||||
if potential_url.startswith('http') and target_service.lower() in potential_url.lower():
|
||||
url = potential_url.replace('\\/', '/')
|
||||
break
|
||||
|
||||
if not url:
|
||||
service_urls = re.finditer(r'(https?://[^"\s]+' + re.escape(target_service) + r'[^"\s]+)', html_content)
|
||||
for match in service_urls:
|
||||
url = match.group(1).replace('\\/', '/')
|
||||
break
|
||||
|
||||
result = {
|
||||
"service": target_service,
|
||||
"url": url,
|
||||
"token": {
|
||||
"primary": None,
|
||||
"expiry": None
|
||||
}
|
||||
}
|
||||
|
||||
if token:
|
||||
try:
|
||||
decoded_once = base64.b64decode(token).decode('latin1')
|
||||
decoded_token = base64.b64decode(decoded_once).decode('latin1')
|
||||
result["token"]["primary"] = decoded_token
|
||||
except Exception:
|
||||
result["token"]["primary"] = token
|
||||
|
||||
result["token"]["expiry"] = token_expiry
|
||||
|
||||
return result
|
||||
|
||||
except Exception as error:
|
||||
return {"error": str(error)}
|
||||
|
||||
def download(self, metadata, output_dir, is_paused_callback=None, is_stopped_callback=None):
|
||||
track_url = metadata['url']
|
||||
primary_token = metadata['token']['primary']
|
||||
expiry = metadata['token']['expiry']
|
||||
track_id = metadata['track_id']
|
||||
service = metadata['service']
|
||||
|
||||
print(f"Starting download for: {track_url}")
|
||||
|
||||
if is_stopped_callback and is_stopped_callback():
|
||||
raise Exception("Download stopped by user")
|
||||
|
||||
initial_request = {
|
||||
"account": {"id": "auto", "type": "country"},
|
||||
"compat": "false",
|
||||
"downscale": "original",
|
||||
"handoff": True,
|
||||
"metadata": True,
|
||||
"private": True,
|
||||
"token": {
|
||||
"expiry": expiry,
|
||||
"primary": primary_token
|
||||
},
|
||||
"upload": {"enabled": False, "service": "pixeldrain"},
|
||||
"url": track_url
|
||||
}
|
||||
|
||||
response = self.client.post(f"https://{self.base_domain}/api/load?url=/api/fetch/stream/v2",
|
||||
json=initial_request,
|
||||
headers=self.headers)
|
||||
|
||||
csrf_token = response.cookies.get('csrf_token')
|
||||
if csrf_token:
|
||||
self.headers['X-CSRF-Token'] = csrf_token
|
||||
|
||||
initial_response = response.json()
|
||||
|
||||
if not initial_response.get("success", False):
|
||||
raise Exception(f"Initial request failed: {initial_response.get('error', 'Unknown error')}")
|
||||
|
||||
handoff = initial_response["handoff"]
|
||||
server = initial_response["server"]
|
||||
|
||||
file_name = self.generate_filename(track_id, service)
|
||||
|
||||
completion_url = f"https://{server}.{self.base_domain}/api/fetch/request/{handoff}"
|
||||
|
||||
print("Waiting for track processing to complete")
|
||||
while True:
|
||||
if is_stopped_callback and is_stopped_callback():
|
||||
raise Exception("Download stopped by user")
|
||||
|
||||
while is_paused_callback and is_paused_callback():
|
||||
time.sleep(0.1)
|
||||
if is_stopped_callback and is_stopped_callback():
|
||||
raise Exception("Download stopped by user")
|
||||
|
||||
completion_response = self.client.get(completion_url, headers=self.headers).json()
|
||||
|
||||
status = completion_response["status"]
|
||||
if status == "completed":
|
||||
print("Processing completed: 100%")
|
||||
break
|
||||
elif status == "error":
|
||||
raise Exception(f"API request failed: {completion_response.get('message', 'Unknown error')}")
|
||||
else:
|
||||
progress = completion_response.get("progress", {})
|
||||
if progress:
|
||||
current = progress.get("current", 0)
|
||||
total = progress.get("total", 100)
|
||||
percent = int((current / total) * 100) if total > 0 else 0
|
||||
action = progress.get("action", "Processing")
|
||||
print(f"Progress: {percent}% - {action} ({current}/{total})")
|
||||
|
||||
if action.lower() == "metadata":
|
||||
if self.progress_callback:
|
||||
self.progress_callback(0, 0)
|
||||
else:
|
||||
print(f"Status: {status} - Waiting for progress information...")
|
||||
if status.lower() == "metadata":
|
||||
if self.progress_callback:
|
||||
self.progress_callback(0, 0)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
download_url = f"https://{server}.{self.base_domain}/api/fetch/request/{handoff}/download"
|
||||
print(f"Starting download of: {file_name}")
|
||||
|
||||
response = self.client.get(download_url, stream=True, headers=self.headers)
|
||||
total_size = int(response.headers.get('content-length', 0))
|
||||
downloaded_size = 0
|
||||
|
||||
file_path = os.path.join(output_dir, file_name)
|
||||
|
||||
try:
|
||||
with open(file_path, 'wb') as file:
|
||||
start_time = time.time()
|
||||
last_update_time = start_time
|
||||
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
if is_stopped_callback and is_stopped_callback():
|
||||
file.close()
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
raise Exception("Download stopped by user")
|
||||
|
||||
while is_paused_callback and is_paused_callback():
|
||||
time.sleep(0.1)
|
||||
if is_stopped_callback and is_stopped_callback():
|
||||
file.close()
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
raise Exception("Download stopped by user")
|
||||
|
||||
if chunk:
|
||||
file.write(chunk)
|
||||
downloaded_size += len(chunk)
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - last_update_time >= 1:
|
||||
if total_size > 0:
|
||||
progress_percent = (downloaded_size / total_size) * 100
|
||||
elapsed_time = current_time - start_time
|
||||
speed = downloaded_size / (1024 * 1024 * elapsed_time) if elapsed_time > 0 else 0
|
||||
print(f"Download progress: {progress_percent:.2f}% ({downloaded_size}/{total_size}) - {speed:.2f} MB/s")
|
||||
else:
|
||||
print(f"Downloaded {downloaded_size / (1024 * 1024):.2f} MB")
|
||||
|
||||
last_update_time = current_time
|
||||
|
||||
if self.progress_callback:
|
||||
self.progress_callback(downloaded_size, total_size)
|
||||
|
||||
if downloaded_size == 0:
|
||||
raise Exception("No data received from server")
|
||||
|
||||
print(f"Download completed: {file_path}")
|
||||
return file_path
|
||||
|
||||
except Exception as e:
|
||||
if os.path.exists(file_path) and os.path.getsize(file_path) == 0:
|
||||
try:
|
||||
os.remove(file_path)
|
||||
except:
|
||||
pass
|
||||
raise e
|
||||
|
||||
async def main():
|
||||
use_fallback = False
|
||||
downloader = TrackDownloader(use_fallback)
|
||||
|
||||
output_dir = "."
|
||||
track_id = "2plbrEY59IikOBgBGLjaoe"
|
||||
service = "amazon"
|
||||
|
||||
def progress_update(current, total):
|
||||
if total > 0:
|
||||
percent = (current / total) * 100
|
||||
print(f"\rDownload progress: {percent:.2f}% ({current}/{total})", end="")
|
||||
|
||||
downloader.set_progress_callback(progress_update)
|
||||
|
||||
try:
|
||||
print(f"Getting track info for ID: {track_id} from {service}")
|
||||
metadata = await downloader.get_track_info(track_id, service)
|
||||
print(f"Track info received, starting download process")
|
||||
|
||||
downloaded_file = downloader.download(metadata, output_dir)
|
||||
print(f"\nFile downloaded successfully: {downloaded_file}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"version": "2.2"
|
||||
}
|
||||
Reference in New Issue
Block a user