|
|
|
@@ -2,15 +2,17 @@ import sys
|
|
|
|
|
import asyncio
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import requests
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from packaging import version
|
|
|
|
|
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
|
|
|
|
|
QGroupBox, QComboBox, QDialog, QDialogButtonBox)
|
|
|
|
|
from PyQt6.QtCore import QThread, pyqtSignal, Qt, QSettings, QSize, QTimer, QUrl
|
|
|
|
|
from PyQt6.QtGui import QIcon, QPixmap, QCursor,QDesktopServices
|
|
|
|
|
from getMetadata import get_metadata
|
|
|
|
|
from getTracks import TrackDownloader
|
|
|
|
|
|
|
|
|
|
class ImageDownloader(QThread):
|
|
|
|
|
finished = pyqtSignal(bytes)
|
|
|
|
@@ -29,10 +31,11 @@ class MetadataFetcher(QThread):
|
|
|
|
|
finished = pyqtSignal(dict)
|
|
|
|
|
error = pyqtSignal(str)
|
|
|
|
|
|
|
|
|
|
def __init__(self, url, headless=True, use_fallback=False):
|
|
|
|
|
def __init__(self, url, headless=True, service="tidal", use_fallback=False):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.url = url
|
|
|
|
|
self.headless_mode = headless
|
|
|
|
|
self.service = service
|
|
|
|
|
self.use_fallback = use_fallback
|
|
|
|
|
self.max_retries = 3
|
|
|
|
|
|
|
|
|
@@ -45,10 +48,11 @@ class MetadataFetcher(QThread):
|
|
|
|
|
import zendriver as zd
|
|
|
|
|
from asyncio import sleep
|
|
|
|
|
|
|
|
|
|
domain = "lucida.su" if self.use_fallback else "lucida.to"
|
|
|
|
|
|
|
|
|
|
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}"
|
|
|
|
|
lucida_url = f"https://{domain}/?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F{track_id}&country=auto&to={self.service}"
|
|
|
|
|
browser = await zd.start(headless=self.headless_mode)
|
|
|
|
|
try:
|
|
|
|
|
page = await browser.get(lucida_url)
|
|
|
|
@@ -89,28 +93,40 @@ class DownloaderWorker(QThread):
|
|
|
|
|
finished = pyqtSignal(str)
|
|
|
|
|
error = pyqtSignal(str)
|
|
|
|
|
|
|
|
|
|
def __init__(self, metadata, output_dir, filename_format='title_artist'):
|
|
|
|
|
def __init__(self, metadata, output_dir, filename_format='title_artist', use_fallback=False):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.metadata = metadata
|
|
|
|
|
self.output_dir = output_dir
|
|
|
|
|
self.filename_format = filename_format
|
|
|
|
|
self.downloader = TrackDownloader()
|
|
|
|
|
self.use_fallback = use_fallback
|
|
|
|
|
self.downloader = TrackDownloader(use_fallback=use_fallback)
|
|
|
|
|
self.last_update_time = 0
|
|
|
|
|
self.last_downloaded_size = 0
|
|
|
|
|
|
|
|
|
|
def format_size(self, size_bytes):
|
|
|
|
|
return f"{size_bytes / (1024 * 1024):.2f}MB"
|
|
|
|
|
units = ['B', 'KB', 'MB', 'GB']
|
|
|
|
|
index = 0
|
|
|
|
|
while size_bytes >= 1024 and index < len(units) - 1:
|
|
|
|
|
size_bytes /= 1024
|
|
|
|
|
index += 1
|
|
|
|
|
return f"{size_bytes:.2f}{units[index]}"
|
|
|
|
|
|
|
|
|
|
def format_speed(self, speed_bytes):
|
|
|
|
|
return f"{speed_bytes * 8 / (1024 * 1024):.2f}Mbps"
|
|
|
|
|
speed_bits = speed_bytes * 8
|
|
|
|
|
|
|
|
|
|
if speed_bits >= 1024 * 1024:
|
|
|
|
|
speed_mbps = speed_bits / (1024 * 1024)
|
|
|
|
|
return f"{speed_mbps:.2f}Mbps"
|
|
|
|
|
else:
|
|
|
|
|
speed_kbps = speed_bits / 1024
|
|
|
|
|
return f"{speed_kbps:.2f}Kbps"
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
if current_time - self.last_update_time >= 0.5:
|
|
|
|
|
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
|
|
|
|
@@ -132,11 +148,80 @@ class DownloaderWorker(QThread):
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.error.emit(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
class ServiceComboBox(QComboBox):
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
self.setIconSize(QSize(16, 16))
|
|
|
|
|
self.setup_items()
|
|
|
|
|
|
|
|
|
|
def setup_items(self):
|
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
icons_dir = os.path.join(current_dir, 'icons')
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(icons_dir):
|
|
|
|
|
os.makedirs(icons_dir)
|
|
|
|
|
|
|
|
|
|
services = [
|
|
|
|
|
{'id': 'tidal', 'name': 'Tidal', 'icon': 'tidal.png'},
|
|
|
|
|
{'id': 'amazon', 'name': 'Amazon Music', 'icon': 'amazon.png'},
|
|
|
|
|
{'id': 'qobuz', 'name': 'Qobuz', 'icon': 'qobuz.png'}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for service in services:
|
|
|
|
|
icon_path = os.path.join(icons_dir, service['icon'])
|
|
|
|
|
if not os.path.exists(icon_path):
|
|
|
|
|
self.create_placeholder_icon(icon_path)
|
|
|
|
|
|
|
|
|
|
icon = QIcon(icon_path)
|
|
|
|
|
self.addItem(icon, service['name'], service['id'])
|
|
|
|
|
|
|
|
|
|
def create_placeholder_icon(self, path):
|
|
|
|
|
pixmap = QPixmap(16, 16)
|
|
|
|
|
pixmap.fill(Qt.GlobalColor.transparent)
|
|
|
|
|
pixmap.save(path)
|
|
|
|
|
|
|
|
|
|
class UpdateDialog(QDialog):
|
|
|
|
|
def __init__(self, current_version, new_version, parent=None):
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
self.setWindowTitle("Update Available")
|
|
|
|
|
self.setFixedWidth(400)
|
|
|
|
|
self.setModal(True)
|
|
|
|
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
|
|
|
|
|
|
message = QLabel(f"A new version of SpotiFLAC is available!\n\n"
|
|
|
|
|
f"Current version: v{current_version}\n"
|
|
|
|
|
f"New version: v{new_version}")
|
|
|
|
|
message.setWordWrap(True)
|
|
|
|
|
layout.addWidget(message)
|
|
|
|
|
|
|
|
|
|
self.disable_check = QCheckBox("Turn off update checking")
|
|
|
|
|
self.disable_check.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
|
|
|
layout.addWidget(self.disable_check)
|
|
|
|
|
|
|
|
|
|
button_box = QDialogButtonBox()
|
|
|
|
|
self.update_button = QPushButton("Update")
|
|
|
|
|
self.update_button.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
|
|
|
self.cancel_button = QPushButton("Cancel")
|
|
|
|
|
self.cancel_button.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
|
|
|
|
|
|
|
|
button_box.addButton(self.update_button, QDialogButtonBox.ButtonRole.AcceptRole)
|
|
|
|
|
button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole)
|
|
|
|
|
|
|
|
|
|
layout.addWidget(button_box)
|
|
|
|
|
|
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
|
|
self.update_button.clicked.connect(self.accept)
|
|
|
|
|
self.cancel_button.clicked.connect(self.reject)
|
|
|
|
|
|
|
|
|
|
class SpotiFlacGUI(QMainWindow):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.settings = QSettings('SpotifyFlac', 'Settings')
|
|
|
|
|
self.setWindowTitle("Spotify FLAC")
|
|
|
|
|
self.current_version = "1.7"
|
|
|
|
|
self.settings = QSettings('SpotiFlac', 'Settings')
|
|
|
|
|
self.setWindowTitle("SpotiFLAC")
|
|
|
|
|
self.check_for_updates = self.settings.value('check_for_updates', True, type=bool)
|
|
|
|
|
|
|
|
|
|
icon_path = os.path.join(os.path.dirname(__file__), "icon.svg")
|
|
|
|
|
if os.path.exists(icon_path):
|
|
|
|
@@ -155,13 +240,49 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.load_settings()
|
|
|
|
|
self.setup_settings_persistence()
|
|
|
|
|
|
|
|
|
|
last_url = self.settings.value('last_url', '')
|
|
|
|
|
self.url_input.setText(last_url)
|
|
|
|
|
self.url_input.textChanged.connect(self.save_url)
|
|
|
|
|
|
|
|
|
|
if self.check_for_updates:
|
|
|
|
|
QTimer.singleShot(0, self.check_updates)
|
|
|
|
|
|
|
|
|
|
def check_updates(self):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.get("https://raw.githubusercontent.com/afkarxyz/SpotiFLAC/refs/heads/main/version.json")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
data = response.json()
|
|
|
|
|
new_version = data.get("version")
|
|
|
|
|
|
|
|
|
|
if new_version and version.parse(new_version) > version.parse(self.current_version):
|
|
|
|
|
dialog = UpdateDialog(self.current_version, new_version, self)
|
|
|
|
|
result = dialog.exec()
|
|
|
|
|
|
|
|
|
|
if dialog.disable_check.isChecked():
|
|
|
|
|
self.settings.setValue('check_for_updates', False)
|
|
|
|
|
self.check_for_updates = False
|
|
|
|
|
|
|
|
|
|
if result == QDialog.DialogCode.Accepted:
|
|
|
|
|
QDesktopServices.openUrl(QUrl("https://github.com/afkarxyz/SpotiFLAC/releases"))
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error checking for updates: {e}")
|
|
|
|
|
|
|
|
|
|
def load_settings(self):
|
|
|
|
|
headless = self.settings.value('headless', True, type=bool)
|
|
|
|
|
fallback = self.settings.value('fallback', False, type=bool)
|
|
|
|
|
service = self.settings.value('service', 'tidal')
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
for i in range(self.service_combo.count()):
|
|
|
|
|
if self.service_combo.itemData(i) == service:
|
|
|
|
|
self.service_combo.setCurrentIndex(i)
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
self.format_title_artist.setChecked(format_type == 'title_artist')
|
|
|
|
|
self.format_artist_title.setChecked(format_type == 'artist_title')
|
|
|
|
|
self.dir_input.setText(output_dir)
|
|
|
|
@@ -171,6 +292,8 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
lambda x: self.settings.setValue('headless', bool(x)))
|
|
|
|
|
self.fallback_checkbox.stateChanged.connect(
|
|
|
|
|
lambda x: self.settings.setValue('fallback', bool(x)))
|
|
|
|
|
self.service_combo.currentIndexChanged.connect(
|
|
|
|
|
lambda i: self.settings.setValue('service', self.service_combo.itemData(i)))
|
|
|
|
|
self.format_title_artist.toggled.connect(
|
|
|
|
|
lambda x: self.settings.setValue('format', 'title_artist' if x else 'artist_title'))
|
|
|
|
|
self.dir_input.textChanged.connect(
|
|
|
|
@@ -218,12 +341,12 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
settings_group = QGroupBox("Settings")
|
|
|
|
|
settings_layout = QHBoxLayout(settings_group)
|
|
|
|
|
settings_layout.setContentsMargins(10, 0, 10, 10)
|
|
|
|
|
settings_layout.setSpacing(20)
|
|
|
|
|
settings_layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
settings_container = QWidget()
|
|
|
|
|
settings_container_layout = QHBoxLayout(settings_container)
|
|
|
|
|
settings_container_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
settings_container_layout.setSpacing(20)
|
|
|
|
|
settings_container_layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
self.headless_checkbox = QCheckBox("Headless")
|
|
|
|
|
self.headless_checkbox.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
@@ -235,14 +358,28 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.fallback_checkbox.setChecked(False)
|
|
|
|
|
settings_container_layout.addWidget(self.fallback_checkbox)
|
|
|
|
|
|
|
|
|
|
service_widget = QWidget()
|
|
|
|
|
service_layout = QHBoxLayout(service_widget)
|
|
|
|
|
service_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
service_layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
service_label = QLabel("Service:")
|
|
|
|
|
self.service_combo = ServiceComboBox()
|
|
|
|
|
self.service_combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
|
|
|
|
|
|
service_layout.addWidget(service_label)
|
|
|
|
|
service_layout.addWidget(self.service_combo)
|
|
|
|
|
|
|
|
|
|
settings_container_layout.addWidget(service_widget)
|
|
|
|
|
|
|
|
|
|
format_widget = QWidget()
|
|
|
|
|
format_layout = QHBoxLayout(format_widget)
|
|
|
|
|
format_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
format_layout.setSpacing(15)
|
|
|
|
|
format_layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
format_label = QLabel("Filename Format:")
|
|
|
|
|
self.format_title_artist = QRadioButton("Title - Artist")
|
|
|
|
|
self.format_artist_title = QRadioButton("Artist - Title")
|
|
|
|
|
format_label = QLabel("Filename:")
|
|
|
|
|
self.format_title_artist = QRadioButton("Title")
|
|
|
|
|
self.format_artist_title = QRadioButton("Artist")
|
|
|
|
|
self.format_title_artist.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
|
self.format_artist_title.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
|
self.format_title_artist.setChecked(True)
|
|
|
|
@@ -329,8 +466,40 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.progress_bar.hide()
|
|
|
|
|
self.main_layout.addWidget(self.progress_bar)
|
|
|
|
|
|
|
|
|
|
bottom_layout = QHBoxLayout()
|
|
|
|
|
|
|
|
|
|
self.status_label = QLabel("")
|
|
|
|
|
self.main_layout.addWidget(self.status_label)
|
|
|
|
|
bottom_layout.addWidget(self.status_label, stretch=1)
|
|
|
|
|
|
|
|
|
|
self.update_button = QPushButton()
|
|
|
|
|
icon_path = os.path.join(os.path.dirname(__file__), "update.svg")
|
|
|
|
|
if os.path.exists(icon_path):
|
|
|
|
|
self.update_button.setIcon(QIcon(icon_path))
|
|
|
|
|
self.update_button.setFixedSize(16, 16)
|
|
|
|
|
self.update_button.setStyleSheet("""
|
|
|
|
|
QPushButton {
|
|
|
|
|
border: none;
|
|
|
|
|
background: transparent;
|
|
|
|
|
}
|
|
|
|
|
QPushButton:hover {
|
|
|
|
|
background: transparent;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
self.update_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
|
self.update_button.setToolTip("Check for Updates")
|
|
|
|
|
self.update_button.clicked.connect(self.open_update_page)
|
|
|
|
|
|
|
|
|
|
bottom_layout.addWidget(self.update_button)
|
|
|
|
|
|
|
|
|
|
self.main_layout.addLayout(bottom_layout)
|
|
|
|
|
|
|
|
|
|
def save_url(self, url):
|
|
|
|
|
self.settings.setValue('last_url', url)
|
|
|
|
|
self.validate_url(url)
|
|
|
|
|
|
|
|
|
|
def open_update_page(self):
|
|
|
|
|
import webbrowser
|
|
|
|
|
webbrowser.open('https://github.com/afkarxyz/SpotiFLAC/releases')
|
|
|
|
|
|
|
|
|
|
def validate_url(self, url):
|
|
|
|
|
url = url.strip()
|
|
|
|
@@ -361,8 +530,9 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
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)
|
|
|
|
|
fallback = self.fallback_checkbox.isChecked()
|
|
|
|
|
service = self.service_combo.currentData()
|
|
|
|
|
self.fetcher = MetadataFetcher(url, headless=headless, service=service, use_fallback=fallback)
|
|
|
|
|
self.fetcher.finished.connect(self.handle_track_info)
|
|
|
|
|
self.fetcher.error.connect(self.handle_fetch_error)
|
|
|
|
|
self.fetcher.start()
|
|
|
|
@@ -379,6 +549,7 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.track_widget.show()
|
|
|
|
|
self.download_button.show()
|
|
|
|
|
self.cancel_button.show()
|
|
|
|
|
self.update_button.hide()
|
|
|
|
|
self.status_label.clear()
|
|
|
|
|
self.adjustWindowHeight()
|
|
|
|
|
|
|
|
|
@@ -419,6 +590,7 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.status_label.clear()
|
|
|
|
|
self.metadata = None
|
|
|
|
|
self.fetch_button.setEnabled(True)
|
|
|
|
|
self.update_button.show()
|
|
|
|
|
self.setFixedHeight(180)
|
|
|
|
|
|
|
|
|
|
def button_clicked(self):
|
|
|
|
@@ -428,6 +600,7 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.start_download()
|
|
|
|
|
|
|
|
|
|
def clear_form(self):
|
|
|
|
|
self.settings.setValue('last_url', '')
|
|
|
|
|
self.url_input.clear()
|
|
|
|
|
self.progress_bar.hide()
|
|
|
|
|
self.progress_bar.setValue(0)
|
|
|
|
@@ -439,6 +612,7 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.track_widget.hide()
|
|
|
|
|
self.input_widget.show()
|
|
|
|
|
self.metadata = None
|
|
|
|
|
self.update_button.show()
|
|
|
|
|
self.setFixedHeight(180)
|
|
|
|
|
|
|
|
|
|
def start_download(self):
|
|
|
|
@@ -457,10 +631,13 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
self.status_label.setText("Preparing...")
|
|
|
|
|
|
|
|
|
|
format_type = 'artist_title' if self.format_artist_title.isChecked() else 'title_artist'
|
|
|
|
|
fallback = self.fallback_checkbox.isChecked()
|
|
|
|
|
|
|
|
|
|
self.worker = DownloaderWorker(
|
|
|
|
|
metadata=self.metadata,
|
|
|
|
|
output_dir=output_dir,
|
|
|
|
|
filename_format=format_type
|
|
|
|
|
filename_format=format_type,
|
|
|
|
|
use_fallback=fallback
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.worker.progress.connect(self.update_progress)
|
|
|
|
@@ -495,7 +672,7 @@ class SpotifyFlacGUI(QMainWindow):
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
window = SpotifyFlacGUI()
|
|
|
|
|
window = SpotiFlacGUI()
|
|
|
|
|
window.show()
|
|
|
|
|
sys.exit(app.exec())
|
|
|
|
|
|