Compare commits

...
19 Commits
Author SHA1 Message Date
luciano e7fde842f2 feat: upgrade archivio con auto-pick, audio preview, cancel-all (v1.9.4)
Tab Upgrade:
- Checkbox "Scelta automatica" per skippare il modal quando ci sono
  multi-match (usa bitrate DESC)
- Anteprima audio nel picker: play button sul source + ogni candidato
  via bridge read_audio_data_url (WKWebView blocca file://)
- Bottone "Annulla tutto" nel picker → interrompe l'intera coda
- Fix mtime: shutil.copy (non copy2) così il file upgradato ha
  data odierna nel Finder
- Fix stop audio → non triggera più toast di errore
- Modal picker più largo (620-780px) con footer wrap

Extra: campo release_date estratto anche in Beatport (publish_date)
e YouTube (upload_date) — completato scaffold sort colonna Data
2026-08-02 12:56:19 +02:00
luciano e10466d93b ui: UpgradeUI archive_dir + candidates picker + bump v1.9.3
Frontend Upgrade:
- state.upArchiveDir + handler Sfoglia/Rimuovi per la seconda path-display
- start_upgrade payload include archive_dir
- bridge handler 'upgrade:candidates_needed' apre openUpgradePicker() con
  lista candidati (basename + bitrate + size + similarity + path)
- 3 azioni utente: Usa selezionato / Cerca YouTube / Salta; ESC e click
  su backdrop = Salta. Chiusura implicita quando upgrade:done.
- CSS minimale (.upgrade-picker-*) accodato dopo le sezioni esistenti.

Version bump v1.9.2 -> v1.9.3.
2026-08-02 11:59:59 +02:00
luciano f0a29f7ca7 ui: campo Cartella archivio + modal picker per Upgrade
Nella tab Upgrade aggiunta seconda coppia path-display + Sfoglia/Rimuovi
per la cartella archivio opzionale (con hint esplicativo).

In fondo al body: nuovo modal upgradePickerModal con lista candidati e
3 bottoni (Usa selezionato / Cerca YouTube / Salta). Riusa le classi
esistenti .modal-backdrop / .modal / .modal-head / .modal-body / .modal-foot.
2026-08-02 11:56:48 +02:00
luciano 4e1af7b8f6 bridge: upgrade_resolve_candidates + evento upgrade:candidates_needed
start_upgrade accetta 'archive_dir' nel payload e lo inoltra a
upgrade_folder. Il worker passa un resolve_callback che emette
'upgrade:candidates_needed' e blocca su una queue.Queue(1) attendendo
la scelta utente dal modal (timeout 10 min → skip).

Il metodo upgrade_resolve_candidates(choice) è chiamato dalla UI e
deposita la scelta nella queue. stop_upgrade sblocca eventuale attesa
inviando un 'skip' automatico. Log dedicati per gli status nuovi
(local_copy, local_upgraded, skipped_by_user, resolve_wait, scan_archive).
2026-08-02 11:56:10 +02:00
luciano d8723f9408 upgrader: _scan_archive + _find_candidates + resolve_callback per match locale
Nuovo flusso: se archive_dir è passato, upgrade_folder cerca versioni HQ
del brano nell'archivio locale prima di scaricare da YouTube. Match via
Jaccard sui token del filename (soglia default 0.5). Se 1 candidato:
copia/converte diretto in mp3 320k. Se >=2 e resolve_callback presente:
chiama la callback bloccante (per la UI) con lista candidati + bitrate
+ similarity, poi rispetta la scelta (use_local / use_youtube / skip).
Fallback su YouTube se nessun match o archive_dir=None.

Test unitari sui 3 helper (_normalize_stem, _scan_archive, _find_candidates).
2026-08-02 11:54:58 +02:00
luciano eb20a034e6 fix: stop button e hang su ffmpeg orfano in Upgrade (bump v1.9.2)
Il worker Python era bloccato in `for line in proc.stdout` che non
ritornava mai quando il subprocess yt-dlp veniva killed ma lasciava
figli ffmpeg orfani con pipe stdout ancora aperto. Effetti:
1) Il bottone Stop non fermava l'upgrade
2) Anche il watchdog kill non liberava il thread

Fix:
- Lettura stdout in thread separato + queue.Queue
- Main loop polla ogni 500ms con timeout: controlla is_stopped() e
  proc.poll() -> Stop risponde entro 1s
- Watchdog usa kill() (SIGKILL) invece di terminate() (SIGTERM) per
  garantire cleanup child orfani
2026-08-02 11:37:40 +02:00
luciano cdd380d62f fix: watchdog su download upgrader (bump v1.9.1)
Prima: se yt-dlp si bloccava nel download di un video (geo-restricted,
YouTube throttle, rete lenta), il for loop su stdout attendeva
indefinitamente e la tab Upgrade si fermava dopo 2-3 brani.

Ora: threading.Timer da 5 min uccide il subprocess se in scaduta.
Al kill il for esce -> codice trova temp_dir vuota -> marca il brano
"download_error" e prosegue col successivo.
2026-08-02 10:55:47 +02:00
luciano 2689f686ec Merge feat/traxsource-charts: MusicTools v1.9.0
Nuova tab Traxsource per caricare la Top 100 mensile per genere.
23 generi, bypass Cloudflare via curl_cffi session, HTML scraping con
BeautifulSoup, cache 15min. Riusa pipeline download + tag ID3.

29 nuovi test unitari, 90/90 test verdi totali.
2026-08-01 12:15:36 +02:00
luciano f8e2ee66bd ui: TraxsourceUI (init, loadChart, render, download) 2026-08-01 12:10:28 +02:00
luciano a07f6c34ca ui: markup tab Traxsource 2026-08-01 12:08:56 +02:00
luciano 6c473eaec9 bridge: 4 metodi Api Traxsource (parallelo Beatport, metadata album=label) 2026-08-01 12:07:56 +02:00
luciano 16c8b8ee67 config: v1.9.0 + traxsource_last_genre nei DEFAULTS 2026-08-01 12:05:11 +02:00
luciano 88e59ad37f traxsource: fetch_top100 con session curl_cffi + retry + cache 15min 2026-08-01 12:01:00 +02:00
luciano 784b56682f traxsource: _discover_top100_url + _parse_tracks con BeautifulSoup 2026-08-01 11:59:55 +02:00
luciano e1e629f8a2 traxsource: helper _split_title_mix + _format_artists + _large_cover + eccezioni 2026-08-01 11:57:42 +02:00
luciano a95912f78c traxsource: GENRES + TraxsourceTrack + list_genres 2026-08-01 11:55:30 +02:00
luciano 7feda99720 traxsource: fixture HTML + bs4 dep + script refresh generi 2026-08-01 11:54:55 +02:00
luciano 191c245fc1 plan: implementation Traxsource charts v1.9.0 2026-08-01 11:50:44 +02:00
luciano 98235625ba spec: feature Traxsource charts (v1.9.0 target) 2026-08-01 11:47:32 +02:00
17 changed files with 12709 additions and 48 deletions

No files matched your search

+215 -4
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import json
import os
import queue
import re
import threading
import time
@@ -15,7 +16,7 @@ from typing import Any, Callable, Optional
import requests
from core.config import load_config, save_config, VERSION, LICENSE_API_URL
from core import beatport, spotify_client
from core import beatport, spotify_client, traxsource
from core import license as license_mod
from core.downloader import (
download_playlist,
@@ -110,6 +111,9 @@ class Api:
self._download_thread: Optional[threading.Thread] = None
self._upgrade_thread: Optional[threading.Thread] = None
self._video_thread: Optional[threading.Thread] = None
# Coda usata dal resolve_callback per attendere la scelta utente
# sul modal "match locali multipli" della tab Upgrade.
self._upgrade_resolve_q: "queue.Queue[dict]" = queue.Queue(1)
# ------------------------------------------------------------------
# Helpers
@@ -882,6 +886,8 @@ class Api:
return {"ok": False, "error": "Upgrade gia in corso"}
directory = (payload.get("directory") or "").strip()
archive_dir = (payload.get("archive_dir") or "").strip() or None
archive_auto_pick = bool(payload.get("archive_auto_pick", False))
recursive = bool(payload.get("recursive", False))
try:
threshold = int(payload.get("threshold", 310))
@@ -898,9 +904,16 @@ class Api:
cfg = load_config()
cookies_path = cfg.get("cookies_path", "")
# Drain la queue di risoluzione da eventuali run precedenti (safety)
try:
while True:
self._upgrade_resolve_q.get_nowait()
except queue.Empty:
pass
self._upgrade_thread = threading.Thread(
target=self._upgrade_worker,
args=(directory, threshold, cookies_path, recursive),
args=(directory, threshold, cookies_path, recursive, archive_dir, archive_auto_pick),
daemon=True,
)
self._upgrade_thread.start()
@@ -908,10 +921,63 @@ class Api:
def stop_upgrade(self) -> dict:
request_upgrade_stop()
# Sblocca eventuale resolve_callback in attesa: inviamo scelta 'skip'
# cosi il worker esce pulito invece di restare bloccato in q.get().
try:
self._upgrade_resolve_q.put_nowait({"action": "skip"})
except queue.Full:
pass
self._log("upgrade", "[INFO] Interruzione richiesta...")
return {"ok": True}
def _upgrade_worker(self, directory, threshold, cookies_path, recursive):
def read_audio_data_url(self, path: str) -> dict:
"""Legge un file audio locale e lo ritorna come data URL base64
per il preview HTML5 nel modal upgrade. WKWebView (macOS pywebview)
blocca `file://` da HTML servito via file://, quindi passiamo dal
bridge. Limite: 60MB per non esplodere la memoria JS."""
import base64 as _b64
MAX = 60 * 1024 * 1024
try:
p = Path(path or "")
if not p.exists() or not p.is_file():
return {"ok": False, "error": "File non trovato"}
size = p.stat().st_size
if size > MAX:
return {"ok": False, "error": f"File troppo grande ({size // 1024 // 1024}MB, max 60MB)"}
ext = p.suffix.lower().lstrip(".")
mime_map = {
"mp3": "audio/mpeg", "m4a": "audio/mp4", "aac": "audio/aac",
"wav": "audio/wav", "flac": "audio/flac", "ogg": "audio/ogg",
"opus": "audio/opus", "webm": "audio/webm",
}
mime = mime_map.get(ext, "audio/mpeg")
b64 = _b64.b64encode(p.read_bytes()).decode("ascii")
return {"ok": True, "data_url": f"data:{mime};base64,{b64}"}
except Exception as e:
return {"ok": False, "error": str(e)}
def upgrade_resolve_candidates(self, choice: dict) -> dict:
"""Riceve la scelta utente dal modal 'match locali multipli' e la
deposita nella queue attesa dal resolve_callback del worker.
`choice`: {'action': 'use_local'|'use_youtube'|'skip', 'path': str?}
"""
if not isinstance(choice, dict):
choice = {"action": "skip"}
try:
# Drain (nel caso rarissimo di doppio put) e poi metti la nuova
try:
self._upgrade_resolve_q.get_nowait()
except queue.Empty:
pass
self._upgrade_resolve_q.put_nowait(choice)
return {"ok": True}
except Exception as e:
return {"ok": False, "error": str(e)}
def _upgrade_worker(self, directory, threshold, cookies_path, recursive,
archive_dir: Optional[str] = None,
archive_auto_pick: bool = False):
view = "upgrade"
def progress_cb(idx, total, filename, status, old_kbps, new_kbps):
@@ -938,6 +1004,19 @@ class Api:
diff = new_kbps - old_kbps
diff_str = f" (+{diff}kbps)" if diff > 0 else ""
self._log(view, f"[UPGRADE] {filename}: {old_kbps} -> {new_kbps}kbps{diff_str}")
elif status == "local_copy":
self._log(view, f"[LOCALE] {filename}: uso match dall'archivio")
elif status == "local_upgraded":
diff = new_kbps - old_kbps
diff_str = f" (+{diff}kbps)" if diff > 0 else ""
self._log(view, f"[LOCALE OK] {filename}: {old_kbps} -> {new_kbps}kbps{diff_str}")
elif status == "skipped_by_user":
self._log(view, f"[SKIP] {filename} (saltato manualmente)")
elif status == "resolve_wait":
self._log(view, f"[SCELTA] {filename}: match multipli, in attesa scelta utente")
elif status == "scan_archive":
# `new_kbps` in questo caso trasporta il count delle chiavi
self._log(view, f"[ARCHIVIO] Indicizzati {new_kbps} gruppi audio")
elif status == "download_error":
self._log(view, f"[ERRORE] {filename}: download fallito")
elif status == "stopped":
@@ -948,7 +1027,35 @@ class Api:
self._emit("upgrade:progress", payload_evt)
upgrade_folder(directory, threshold, cookies_path, recursive, progress_cb)
def resolve_cb(source_path: str, candidates: list) -> dict:
"""Bloccante: emette evento e attende scelta utente via queue.
Se archive_auto_pick=True, salta il modal e sceglie il primo
candidato (già ordinato per bitrate DESC nel core).
`source_path` è il path assoluto del file sorgente (per il preview
audio nel modal)."""
src_name = Path(source_path).name if source_path else ""
if archive_auto_pick and candidates:
top = candidates[0]
path = top.get("path") if isinstance(top, dict) else str(top)
self._log(view, f"[AUTO] {src_name}: match multipli, uso {Path(path).name}")
return {"action": "use_local", "path": path}
self._emit("upgrade:candidates_needed", {
"file": src_name,
"source_path": source_path,
"candidates": candidates,
})
try:
# Timeout 10 min — se l'utente sparisce, salta il brano
choice = self._upgrade_resolve_q.get(timeout=600)
except queue.Empty:
choice = {"action": "skip"}
return choice
upgrade_folder(
directory, threshold, cookies_path, recursive, progress_cb,
archive_dir=archive_dir,
resolve_callback=resolve_cb,
)
self._emit("upgrade:done", {"ok": True})
# ------------------------------------------------------------------
@@ -1165,6 +1272,110 @@ class Api:
"subfolder": subfolder,
})
# ================================================================
# Traxsource charts
# ================================================================
def _traxsource_output_dir(self, out_root: str, genre_name: str) -> Path:
"""Cartella dove finiscono i file Traxsource. Coerente con la
sanitizzazione di `start_tracks_download` (slash -> underscore)."""
safe_genre = genre_name.replace("/", "_").replace("\\", "_").strip()
subfolder = f"Traxsource_{safe_genre}" if safe_genre else "Traxsource"
return Path(out_root) / subfolder
def traxsource_genres(self) -> list:
"""Lista dei generi Traxsource per il dropdown UI."""
return traxsource.list_genres()
def traxsource_fetch_chart(self, slug: str, force_refresh: bool = False) -> dict:
"""Fetches la Top 100 corrente per il genere. Salva l'ultimo genere in config."""
try:
cfg = load_config()
cfg["traxsource_last_genre"] = slug
save_config(cfg)
except Exception:
pass
try:
tracks = traxsource.fetch_top100(slug, force_refresh=force_refresh)
except ValueError as e:
return {"ok": False, "error": "invalid_genre", "message": str(e)}
except traxsource.TraxsourceUnreachableError as e:
return {"ok": False, "error": "unreachable", "message": str(e)}
except traxsource.TraxsourceParseError as e:
return {"ok": False, "error": "parse", "message": str(e)}
return {"ok": True, "tracks": [asdict(t) for t in tracks]}
def traxsource_check_existing(self, tracks: list, genre_name: str) -> list:
"""True per ogni track già presente nella cartella output Traxsource.
Match euristico: filename (senza extension) deve contenere sia il titolo
che il primo artista (case-insensitive)."""
cfg = load_config()
out_root = (cfg.get("output_dir") or "").strip()
if not out_root:
return [False] * len(tracks)
out_dir = self._traxsource_output_dir(out_root, genre_name)
if not out_dir.exists():
return [False] * len(tracks)
existing_stems = [p.stem.lower() for p in out_dir.glob("*.mp3")]
result = []
for t in tracks:
title = (t.get("title") or "").lower().strip()
artists = (t.get("artists") or "")
first_artist = artists.split(",")[0].split("&")[0].strip().lower()
if not title or not first_artist:
result.append(False)
continue
result.append(any(
(title in stem and first_artist in stem)
for stem in existing_stems
))
return result
def traxsource_download_selected(self, tracks: list, genre_name: str) -> dict:
"""Converte i TraxsourceTrack in tracklist compatibile con
start_tracks_download + costruisce metadata paralleli per il tagger ID3
(album=label Traxsource, date=YYYY-MM corrente, genre=nome genere)."""
cfg = load_config()
out_root = (cfg.get("output_dir") or "").strip()
if not out_root:
return {"ok": False, "error": "Cartella output non impostata"}
target = self._traxsource_output_dir(out_root, genre_name)
subfolder = target.name # es. "Traxsource_Tech House"
import datetime as _dt
current_month = _dt.date.today().strftime("%Y-%m")
converted: list = []
metadata: list = []
for t in tracks:
title = (t.get("title") or "").strip()
artists = (t.get("artists") or "").strip()
if not title:
continue
converted.append({"name": title, "artist": artists})
metadata.append({
"title": title,
"artist": artists,
"album": (t.get("label") or "").strip() or f"Traxsource Top 100 {current_month}",
"date": current_month,
"genre": genre_name,
"cover_url": (t.get("cover_url_large") or t.get("image_url") or "").strip(),
})
if not converted:
return {"ok": False, "error": "Nessun brano valido"}
return self.start_tracks_download({
"tracks": converted,
"output_dir": out_root,
"subfolder": subfolder,
"metadata": metadata,
})
# ================================================================
# Music Search — Spotify + YouTube
# ================================================================
+2
View File
@@ -24,6 +24,7 @@ class BeatportTrack:
duration_sec: int
beatport_id: int
image_url: str = "" # URL cover art (default vuoto per retrocompatibilità test)
release_date: str = "" # ISO YYYY-MM-DD, vuoto se mancante
@property
def display(self) -> str:
@@ -177,6 +178,7 @@ def _parse_tracks(data: dict) -> list:
duration_sec=length_ms // 1000,
beatport_id=int(item.get("id") or 0),
image_url=_extract_image_url(item.get("image")),
release_date=str(item.get("publish_date") or item.get("new_release_date") or "").strip(),
)
except (TypeError, ValueError) as e:
raise BeatportParseError(f"track[{i}] shape inattesa: {e}") from e
+3 -1
View File
@@ -5,7 +5,7 @@ import os
import sys
from pathlib import Path
VERSION = "v1.8.6"
VERSION = "v1.9.4"
APP_NAME = "MusicTools"
@@ -72,6 +72,8 @@ DEFAULTS = {
"theme": "dark",
# ---- Beatport ----
"beatport_last_genre": "melodic-house-techno", # ultimo genere Top 100 caricato
# ---- Traxsource ----
"traxsource_last_genre": "tech-house",
# ---- Music Search (Spotify + YouTube) ----
"spotify_search_last_query": "",
"spotify_search_artist_mode": False,
+277
View File
@@ -0,0 +1,277 @@
"""Fetch Top 100 Traxsource per genere.
Approccio: session curl_cffi (bypass CF via cookie) + BeautifulSoup HTML scraping.
Spec: docs/superpowers/specs/2026-08-01-traxsource-charts-design.md.
"""
from __future__ import annotations
import re
import time
from dataclasses import dataclass
# Generi musicali Traxsource (slug -> (id, display_name)).
# Aggiornato via scripts/refresh_traxsource_genres.py. Esclusi "sounds/samples/loops",
# "acapella", "beats", "efx-dj-tools", "stems" (non generi ma tipi di prodotto).
GENRES: dict = {
"afro-house": (27, "Afro House"),
"afro-latin-brazilian": (23, "Afro / Latin / Brazilian"),
"broken-beat-nu-jazz": (2, "Broken Beat / Nu Jazz"),
"classic-house": (12, "Classic House"),
"deep-house": (13, "Deep House"),
"drum-and-bass": (31, "Drum & Bass"),
"electro-house": (11, "Electro House"),
"electronica": (5, "Electronica"),
"garage": (29, "Garage"),
"house": (4, "House"),
"jackin-house": (15, "Jackin House"),
"leftfield": (14, "Leftfield"),
"lounge-chill-out": (1, "Lounge / Chill Out"),
"melodic-progressive-house": (19, "Melodic / Progressive House"),
"minimal-deep-tech": (16, "Minimal / Deep Tech"),
"nu-disco-indie-dance": (17, "Nu Disco / Indie Dance"),
"pop-dance": (32, "Pop Dance"),
"r-and-b-hip-hop": (6, "R&B / Hip Hop"),
"soul-funk-disco": (3, "Soul / Funk / Disco"),
"soulful-house": (24, "Soulful House"),
"tech-house": (18, "Tech House"),
"techno": (20, "Techno"),
"world": (30, "World"),
}
@dataclass(frozen=True)
class TraxsourceTrack:
position: int
title: str
mix: str
artists: str
label: str
traxsource_id: int
slug: str
image_url: str = ""
cover_url_large: str = ""
def list_genres() -> list:
result = [
{"slug": slug, "id": gid, "name": name}
for slug, (gid, name) in GENRES.items()
]
result.sort(key=lambda g: g["name"].casefold())
return result
class TraxsourceError(Exception):
"""Base per errori Traxsource."""
class TraxsourceUnreachableError(TraxsourceError):
"""Rete / 5xx dopo retry."""
class TraxsourceParseError(TraxsourceError):
"""HTML ricevuto ma non conforme allo schema atteso."""
_MIX_PAREN_RE = re.compile(r"^(.*)\s*\(([^()]+)\)\s*$")
_SIZE_RE = re.compile(r"/\d+x\d+/")
def _split_title_mix(full_title: str) -> tuple:
"""Estrae mix dalle parentesi finali. 'Foo (Extended Mix)' -> ('Foo', 'Extended Mix').
Se non ci sono parentesi finali, mix = ''."""
if not full_title:
return ("", "")
m = _MIX_PAREN_RE.match(full_title.strip())
if m:
return (m.group(1).strip(), m.group(2).strip())
return (full_title.strip(), "")
def _format_artists(names: list) -> str:
"""['A', 'B', 'C'] -> 'A, B & C'. Strips whitespace."""
clean = [n.strip() for n in names if n and n.strip()]
if not clean:
return ""
if len(clean) == 1:
return clean[0]
return ", ".join(clean[:-1]) + " & " + clean[-1]
def _large_cover(url: str) -> str:
"""Sostituisce /NxN/ nel path con /500x500/. Se pattern assente, ritorna invariato."""
if not url:
return ""
return _SIZE_RE.sub("/500x500/", url)
_TOP100_LINK_RE = re.compile(r'href="(/title/\d+/top-100-[a-z0-9-]+)"')
def _discover_top100_url(genre_html: str) -> str:
"""Estrae il path relativo della playlist Top 100 corrente dalla pagina di un genere."""
m = _TOP100_LINK_RE.search(genre_html)
if not m:
raise TraxsourceParseError("link Top 100 non trovato nella pagina genere")
return m.group(1)
def _parse_tracks(html: str) -> list:
"""Parsa la pagina Top 100 (title playlist) e ritorna list[TraxsourceTrack].
Selettori (verificati su fixture tech-house 2026-07):
row = div.trk-row.play-trk (data-trid=<int>)
position = div.tnum (inside div.tnum-pos)
title <a> = div.trk-cell.title a[href^="/track/"]
version = span.version (contiene child span.duration da rimuovere)
artists = a.com-artists (uno o piu)
label <a> = div.trk-cell.label a
cover img = div.trk-cell.thumb img (src /scripts/image.php/52x52/...)
"""
# Lazy import — bs4 non e' hard-dep del modulo (importato solo quando serve).
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
rows = soup.select("div.trk-row.play-trk")
if not rows:
raise TraxsourceParseError("nessuna track (div.trk-row.play-trk) trovata")
out: list = []
for i, row in enumerate(rows, 1):
try:
trid = int(row.get("data-trid") or 0)
pos_el = row.select_one("div.tnum")
position = i # fallback su enumerate se pos manca / non e' un numero
if pos_el:
pos_txt = pos_el.get_text(strip=True)
if pos_txt.isdigit():
position = int(pos_txt)
title_a = row.select_one('div.trk-cell.title a[href^="/track/"]')
if not title_a:
continue
title = title_a.get_text(strip=True)
href = title_a.get("href") or ""
slug = href.rsplit("/", 1)[-1]
# Mix version: contenuto di span.version, escluso span.duration
mix = ""
version_el = row.select_one("span.version")
if version_el:
dur_el = version_el.select_one("span.duration")
if dur_el:
dur_el.extract()
mix = version_el.get_text(strip=True)
artist_names = [a.get_text(strip=True) for a in row.select("a.com-artists")]
artists = _format_artists(artist_names)
label_a = row.select_one("div.trk-cell.label a")
label = label_a.get_text(strip=True) if label_a else ""
img = row.select_one('div.trk-cell.thumb img[src*="/scripts/image.php/"]')
image_url = (img.get("src") or "") if img else ""
cover_large = _large_cover(image_url)
out.append(TraxsourceTrack(
position=position,
title=title,
mix=mix,
artists=artists,
label=label,
traxsource_id=trid,
slug=slug,
image_url=image_url,
cover_url_large=cover_large,
))
except Exception as e:
raise TraxsourceParseError(f"errore parse track[{i}]: {e}") from e
return out
# --- fetch_top100 con session curl_cffi + retry + cache ------------------
_IMPERSONATE = "chrome131"
_REQUEST_TIMEOUT = 15
_MAX_ATTEMPTS = 3
_BACKOFF_SEC = [1, 3]
_CACHE_TTL_SEC = 15 * 60
_cache: dict = {}
_session_singleton = None
def _session():
"""Ritorna la Session curl_cffi singleton, preriscaldata con GET a /."""
global _session_singleton
if _session_singleton is None:
from curl_cffi import requests as _cffi
_session_singleton = _cffi.Session(impersonate=_IMPERSONATE)
try:
_session_singleton.get("https://www.traxsource.com/", timeout=_REQUEST_TIMEOUT)
except Exception:
pass # cookie CF possono arrivare comunque
return _session_singleton
def _do_get(session, url: str) -> str:
"""GET con retry + backoff. Include Referer per pagine interne."""
last_exc = None
for attempt in range(_MAX_ATTEMPTS):
try:
resp = session.get(
url,
timeout=_REQUEST_TIMEOUT,
headers={"Referer": "https://www.traxsource.com/"},
)
if resp.status_code >= 500 or resp.status_code == 403:
raise Exception(f"HTTP {resp.status_code}")
resp.raise_for_status()
return resp.text
except Exception as e:
last_exc = e
if attempt < _MAX_ATTEMPTS - 1:
time.sleep(_BACKOFF_SEC[attempt])
raise TraxsourceUnreachableError(
f"Traxsource irraggiungibile dopo {_MAX_ATTEMPTS} tentativi: {last_exc}"
)
def fetch_top100(slug: str, force_refresh: bool = False) -> list:
"""Fetches Top 100 corrente per il genere.
1. Verifica slug in GENRES
2. Fetch pagina genre -> _discover_top100_url
3. Fetch pagina Top 100 -> _parse_tracks
4. Cache 15 min
Raises:
ValueError: slug non in GENRES
TraxsourceUnreachableError: rete/5xx dopo retry
TraxsourceParseError: HTML non conforme
"""
if slug not in GENRES:
raise ValueError(f"slug genere non valido: {slug!r}")
now = time.time()
if not force_refresh:
cached = _cache.get(slug)
if cached and (now - cached[0]) < _CACHE_TTL_SEC:
return cached[1]
gid, _name = GENRES[slug]
sess = _session()
genre_url = f"https://www.traxsource.com/genre/{gid}/{slug}"
genre_html = _do_get(sess, genre_url)
top100_path = _discover_top100_url(genre_html)
top100_url = "https://www.traxsource.com" + top100_path
top100_html = _do_get(sess, top100_url)
tracks = _parse_tracks(top100_html)
_cache[slug] = (now, tracks)
return tracks
+335 -12
View File
@@ -3,16 +3,28 @@
from __future__ import annotations
import json
import queue
import re
import shutil
import subprocess
import threading
from pathlib import Path
from typing import Callable, Optional
from core.paths import find_ytdlp, find_ffmpeg_dir, find_ffprobe, subprocess_flags
from core.paths import find_ytdlp, find_ffmpeg_dir, find_ffmpeg, find_ffprobe, subprocess_flags
AUDIO_EXTENSIONS = {".mp3", ".m4a", ".wav", ".flac"}
# Soglia minima Jaccard per considerare un file dell'archivio come candidato.
_ARCHIVE_MIN_SIMILARITY = 0.5
# Token di lunghezza inferiore a questa vengono scartati (troppo generici).
_MIN_TOKEN_LEN = 3
# Timeout massimo per singolo download yt-dlp (secondi). Watchdog kill.
# Serve a evitare hang su video geo-restricted, YouTube throttle o rete lenta.
_DOWNLOAD_TIMEOUT_SEC = 300
# Flag globale per interruzione
_stop_event = threading.Event()
_current_process: Optional[subprocess.Popen] = None
@@ -70,6 +82,143 @@ def get_bitrate(filepath: str | Path) -> int:
return 0
def _normalize_stem(stem: str) -> set:
"""Ritorna il set di token normalizzati per un nome file (senza extension).
Pipeline:
1. Lowercase
2. Sostituisce caratteri non alfanumerici con spazi (mantiene split naturale)
3. Split su whitespace
4. Filtra token di lunghezza < _MIN_TOKEN_LEN (troppo generici)
"""
if not stem:
return set()
lowered = stem.lower()
# Manteniamo spazi ma sostituiamo tutto il resto (che non è alfanumerico) con spazi
cleaned = re.sub(r"[^a-z0-9\s]+", " ", lowered)
tokens = cleaned.split()
return {t for t in tokens if len(t) >= _MIN_TOKEN_LEN}
def _tokens_to_key(tokens: set) -> str:
"""Chiave stabile per un set di token (usata come chiave del dict d'indice)."""
return "|".join(sorted(tokens))
def _key_to_tokens(key: str) -> set:
"""Inverso di _tokens_to_key."""
if not key:
return set()
return set(key.split("|"))
def _scan_archive(archive_dir: str) -> dict:
"""Indicizza recursivamente un archivio di file audio.
Ritorna dict {token_key: [Path, ...]}. La scansione è case-insensitive
sulle estensioni: `.MP3`, `.Mp3`, `.mp3` sono tutti riconosciuti.
"""
base = Path(archive_dir)
index: dict = {}
if not base.exists() or not base.is_dir():
return index
for f in base.rglob("*"):
if not f.is_file():
continue
if f.suffix.lower() not in AUDIO_EXTENSIONS:
continue
tokens = _normalize_stem(f.stem)
if not tokens:
continue
key = _tokens_to_key(tokens)
index.setdefault(key, []).append(f)
return index
def _find_candidates(
target_stem: str,
index: dict,
min_similarity: float = _ARCHIVE_MIN_SIMILARITY,
) -> list:
"""Cerca candidati nell'indice tramite Jaccard sui token del nome.
Ritorna lista di tuple `(path, similarity, bitrate)` ordinata per
bitrate DESC, poi similarity DESC.
"""
target_tokens = _normalize_stem(target_stem)
if not target_tokens:
return []
results: list = []
for key, paths in index.items():
entry_tokens = _key_to_tokens(key)
if not entry_tokens:
continue
common = len(target_tokens & entry_tokens)
if common == 0:
continue
total = len(target_tokens | entry_tokens)
if total == 0:
continue
sim = common / total
if sim < min_similarity:
continue
for p in paths:
try:
br = get_bitrate(p)
except Exception:
br = 0
results.append((p, sim, br))
# Ordina: bitrate DESC, poi similarity DESC (stabile)
results.sort(key=lambda t: (-t[2], -t[1]))
return results
def _copy_or_convert_to_mp3(
src: Path,
dst: Path,
temp_dir: Path,
) -> bool:
"""Copia (o converte se necessario) `src` in `dst` come MP3.
- Se src è già .mp3 → copia diretta con shutil.copy2
- Altrimenti → converti con ffmpeg a 320k CBR
Ritorna True in caso di successo.
"""
try:
if src.suffix.lower() == ".mp3":
# shutil.copy (non copy2): mtime del target = ora, non quello
# del sorgente. Così è chiaro nel Finder che il file è stato
# aggiornato dall'upgrade.
shutil.copy(str(src), str(dst))
return dst.exists()
# Convert non-mp3 -> mp3 320k
ffmpeg_bin = find_ffmpeg() or "ffmpeg"
temp_out = temp_dir / f"_archive_convert_{dst.stem}.mp3"
# -y per sovrascrivere se residuo di run precedente
subprocess.run(
[
ffmpeg_bin, "-y",
"-i", str(src),
"-vn", # scarta eventuali stream video/cover art (le riproviamo dopo)
"-c:a", "libmp3lame",
"-b:a", "320k",
"-id3v2_version", "3",
str(temp_out),
],
capture_output=True,
timeout=300,
**subprocess_flags(),
)
if not temp_out.exists():
return False
temp_out.replace(dst)
return True
except Exception:
return False
def _load_done_set(done_file: Path) -> set[str]:
if done_file.exists():
return set(done_file.read_text(encoding="utf-8").splitlines())
@@ -180,8 +329,21 @@ def upgrade_folder(
cookies_path: Optional[str] = None,
recursive: bool = False,
progress_callback: Optional[Callable] = None,
archive_dir: Optional[str] = None,
resolve_callback: Optional[Callable] = None,
) -> None:
"""Logica principale di upgrade qualita."""
"""Logica principale di upgrade qualita.
Args:
archive_dir: se presente, prima di scaricare da YouTube l'app cerca
una versione HQ del brano in questa cartella (recursive). Se ne
trova una la copia (o converte in mp3 320k) mantenendo il nome
originale.
resolve_callback: chiamato in caso di match multiplo nell'archivio.
Riceve una lista di dict {path, bitrate, size, similarity} e deve
ritornare bloccante un dict {'action': 'use_local'|'use_youtube'|
'skip', 'path': Optional[str]}.
"""
reset_stop()
ytdlp = find_ytdlp()
@@ -195,7 +357,7 @@ def upgrade_folder(
else:
folders = [Path(directory)]
all_items: list[tuple[Path, Path]] = []
all_items: list = []
for folder in folders:
for ext in AUDIO_EXTENSIONS:
for f in sorted(folder.glob(f"*{ext}")):
@@ -207,6 +369,16 @@ def upgrade_folder(
progress_callback(0, 0, "", "no_files", 0, 0)
return
# Pre-scan dell'archivio (una volta sola). Se archive_dir è None si salta.
archive_index: dict = {}
if archive_dir:
try:
archive_index = _scan_archive(archive_dir)
except Exception:
archive_index = {}
if progress_callback:
progress_callback(0, total, "", "scan_archive", 0, len(archive_index))
processed = 0
for filepath, folder in all_items:
@@ -230,6 +402,95 @@ def upgrade_folder(
current_kbps = get_bitrate(filepath)
# ------------------------------------------------------------------
# 1) ARCHIVE LOOKUP (se archive_dir presente)
# ------------------------------------------------------------------
if archive_index:
try:
candidates = _find_candidates(filename, archive_index)
except Exception:
candidates = []
selected_path: Optional[Path] = None
user_chose_youtube = False
user_chose_skip = False
if len(candidates) == 1:
selected_path = candidates[0][0]
elif len(candidates) >= 2 and resolve_callback is not None:
cand_payload = []
for cp, csim, cbr in candidates:
try:
csize = cp.stat().st_size
except Exception:
csize = 0
cand_payload.append({
"path": str(cp),
"bitrate": cbr,
"size": csize,
"similarity": csim,
})
if progress_callback:
progress_callback(processed, total, filepath.name,
"resolve_wait", current_kbps, 0)
try:
choice = resolve_callback(str(filepath), cand_payload) or {}
except Exception:
choice = {}
action = (choice.get("action") or "").strip()
if action == "use_local":
chosen = (choice.get("path") or "").strip()
if chosen:
cp = Path(chosen)
if cp.exists():
selected_path = cp
elif action == "use_youtube":
user_chose_youtube = True
elif action == "skip":
user_chose_skip = True
else:
# Risposta invalida: fallback su YouTube per non bloccare
user_chose_youtube = True
# len(candidates) >= 2 senza callback: fallback YouTube
# len(candidates) == 0: fallback YouTube
if user_chose_skip:
_mark_done(done_file, filename)
processed += 1
if progress_callback:
progress_callback(processed, total, filepath.name,
"skipped_by_user", current_kbps, 0)
continue
if selected_path is not None and not user_chose_youtube:
# Copia/converte il candidato locale come .mp3 con nome originale
dst = filepath.parent / f"{filename}.mp3"
if progress_callback:
progress_callback(processed, total, filepath.name,
"local_copy", current_kbps, 0)
# Rimuovi originale solo se ha estensione diversa (altrimenti
# verrà sovrascritto dalla copia)
try:
if filepath.exists() and filepath.resolve() != dst.resolve():
filepath.unlink()
except Exception:
pass
ok = _copy_or_convert_to_mp3(selected_path, dst, temp_dir)
if ok:
new_kbps = get_bitrate(dst)
_mark_done(done_file, filename)
_cleanup_temp(temp_dir)
processed += 1
if progress_callback:
progress_callback(processed, total, filepath.name,
"local_upgraded", current_kbps, new_kbps)
continue
# Copia fallita: fallback YouTube (non marchiamo done)
_cleanup_temp(temp_dir)
# ------------------------------------------------------------------
# 2) YOUTUBE FALLBACK (comportamento originale)
# ------------------------------------------------------------------
if progress_callback:
progress_callback(processed, total, filepath.name, "searching", current_kbps, 0)
@@ -288,17 +549,79 @@ def upgrade_folder(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
**subprocess_flags(),
)
proc = _current_process
for line in _current_process.stdout:
if is_stopped():
_current_process.terminate()
return
pct_match = re.search(r"(\d+(?:\.\d+)?)%", line)
if pct_match and progress_callback:
pct = int(float(pct_match.group(1)))
progress_callback(processed, total, filepath.name, "downloading", current_kbps, pct)
# Watchdog: kill del subprocess se supera _DOWNLOAD_TIMEOUT_SEC.
# Previene hang indefinito su video problematici (geo-block,
# YouTube throttle, rete lenta).
def _watchdog_kill():
try:
if proc.poll() is None:
proc.kill() # SIGKILL — SIGTERM può lasciare ffmpeg orfano che tiene aperto il pipe
except Exception:
pass
watchdog = threading.Timer(_DOWNLOAD_TIMEOUT_SEC, _watchdog_kill)
watchdog.daemon = True
watchdog.start()
# Lettore stdout in thread separato + queue: il main loop polla
# con timeout invece di bloccare su `for line in proc.stdout`.
# Cosi is_stopped() e proc.poll() vengono controllati periodicamente
# -> il bottone Stop risponde in <1s anche se il subprocess ha
# figli orfani (ffmpeg) che tengono aperto il pipe.
output_q: queue.Queue = queue.Queue()
def _reader():
try:
for line in proc.stdout:
output_q.put(line)
except Exception:
pass
finally:
output_q.put(None) # sentinel: pipe closed
reader = threading.Thread(target=_reader, daemon=True)
reader.start()
try:
while True:
if is_stopped():
try:
proc.kill()
except Exception:
pass
return
try:
line = output_q.get(timeout=0.5)
except queue.Empty:
# Se il subprocess e' morto e il pipe non produce piu' output
# (child orfano), esci comunque.
if proc.poll() is not None and output_q.empty():
# Aspetta ancora un attimo per drenare
try:
line = output_q.get(timeout=1.0)
except queue.Empty:
break
else:
continue
if line is None:
break
pct_match = re.search(r"(\d+(?:\.\d+)?)%", line)
if pct_match and progress_callback:
pct = int(float(pct_match.group(1)))
progress_callback(processed, total, filepath.name, "downloading", current_kbps, pct)
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
try:
proc.kill()
proc.wait(timeout=5)
except Exception:
pass
finally:
watchdog.cancel()
_current_process.wait()
with _process_lock:
_current_process = None
+9
View File
@@ -63,6 +63,14 @@ def search_youtube(query: str, limit: int = 50) -> list:
video_id = e.get("id") or ""
url = e.get("url") or (f"https://www.youtube.com/watch?v={video_id}" if video_id else "")
image_url = f"https://i.ytimg.com/vi/{video_id}/mqdefault.jpg" if video_id else ""
# yt-dlp `upload_date` è YYYYMMDD (stringa). Lo converto in ISO YYYY-MM-DD.
# Con --flat-playlist può essere assente; in tal caso resta stringa vuota.
raw_date = str(e.get("upload_date") or "").strip()
release_date = ""
if len(raw_date) == 8 and raw_date.isdigit():
release_date = f"{raw_date[0:4]}-{raw_date[4:6]}-{raw_date[6:8]}"
elif raw_date:
release_date = raw_date # forma sconosciuta, passa così
result.append({
"id": video_id,
"url": url,
@@ -70,5 +78,6 @@ def search_youtube(query: str, limit: int = 50) -> list:
"channel": e.get("uploader") or e.get("channel") or "",
"duration_sec": int(e.get("duration") or 0),
"image_url": image_url,
"release_date": release_date,
})
return result
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,256 @@
# Traxsource Charts — Design Spec
- **Data:** 2026-08-01
- **Autore:** LuZa + Claude
- **Stato:** Approvato, pronto per implementation plan
- **Target release:** MusicTools v1.9.0
## Obiettivo
Nuova tab "▲ Traxsource" nell'app che permette di:
1. Scegliere un genere musicale da un elenco di ~15 generi Traxsource
2. Caricare la **Top 100** ufficiale del mese corrente per quel genere
3. Anteprima checkbox + download riusando il pipeline esistente (Spotify search → yt-dlp → tag ID3 + cover)
Sostituisce l'attività manuale di copiare/incollare tracklist Traxsource.
## Non-goals
- Chart diverse dalla Top 100 (Must Have, Just Added, ecc.)
- Ricerca artista/label specifica su Traxsource
- Preview audio (Traxsource ha DRM)
- Duration tracks (non è nell'HTML della lista; sarebbe una richiesta extra per track)
- Login/account Traxsource
## Ricognizione tecnica (fatta 2026-08-01)
- **Cloudflare Managed Challenge attivo** su tutte le pagine tranne home e /top. Bypass: session-based con `curl_cffi.Session(impersonate='chrome131')`, prima GET a `/`, poi le pagine sensibili con `Referer: https://www.traxsource.com/`.
- **Non è Next.js** → no `__NEXT_DATA__`. HTML server-rendered.
- **Struttura URL:**
- Genre index: `/genre/<id>/<slug>` — es. `/genre/18/tech-house`. Contiene solo 10 top track più altri widget
- Top 100 corrente: `/title/<title_id>/top-100-<genre>-of-<month>-<year>` — es. `/title/2847986/top-100-tech-house-of-july-2026`. Contiene 100 track completi in un unico documento (~394KB)
- **Il title_id cambia ogni mese.** Non è hardcodabile. Va scoperto dinamicamente
- **Discovery Top 100 URL:** nella pagina `/genre/<id>/<slug>` c'è (almeno un) `<a href="/title/<id>/top-100-<slug>-of-<month>-<year>">`. Estrai col regex `href="(/title/\d+/top-100-[a-z0-9-]+)"`
- **Track markup (stabile):**
```html
<div data-trid="14842066" class="top-item play-trk ptk-14842066">
<div class="ttib position">1</div>
<div class="image"><img src="https://.../52x52/HASH.jpg" /></div>
<div class="ttib info">
<a href="/track/14842066/badman-sound-extended-mix" class="com-title">Badman Sound (Extended Mix)</a>
<a href="/artist/92248/hannah-wants" class="com-artists">Hannah Wants</a>,
<a href="/artist/321517/trace" class="com-artists">Trace</a>
<a href="/label/325/nervous" class="com-label">Nervous</a>
</div>
</div>
```
Class names sono stabili (semantici, non generati da bundler).
- **Cover art:** URL contiene la size (`/52x52/`). Sostituendo con `/500x500/` si ottiene la versione grande per tagging ID3.
## Approccio scelto
- Session `curl_cffi` con `impersonate='chrome131'`. Cookie CF conservati automaticamente. Preriscaldamento con GET a `/` al primo utilizzo.
- Parser: `BeautifulSoup4` con selettori CSS puliti — `div.top-item.play-trk` per righe, `a.com-title` per titolo, `a.com-artists` per artisti, `a.com-label` per label, `div.ttib.position` per rank, `img` per cover URL.
- Regex sul titolo per estrarre "mix name" tra parentesi: `Song Title (Extended Mix)` → title="Song Title", mix="Extended Mix".
- Cache in-memory 15 min per la lista, come Beatport.
- Genere → (numeric_id, slug, display_name) map hardcoded (estratti dalla home /top).
**Alternative scartate:**
- `requests` puro: bloccato da CF, verificato
- API interna Traxsource: non esistono API pubbliche gratuite documentate; reverse engineering rischio ban
- Playwright: overkill (~100MB dep) quando session + BeautifulSoup basta
## Architettura
### Nuovo modulo `core/traxsource.py`
```python
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
# Mappa slug → (numeric_id, display_name)
# Verificati 2026-08-01 dalla home /top di Traxsource
GENRES: dict = {
"afro-house": (33, "Afro House"),
"afro-latin-brazilian": (23, "Afro / Latin / Brazilian"),
"amapiano": (37, "Amapiano"),
"bass-club": (25, "Bass / Club"),
"breaks-uk-bass": (14, "Breaks / UK Bass"),
"classic-house": (12, "Classic House"),
"deep-house": (13, "Deep House"),
"dj-tools": (16, "DJ Tools"),
"downtempo-nu-disco-indie-dance": (22, "Downtempo / Nu Disco / Indie Dance"),
"electronica": (5, "Electronica"),
"electro-house": (11, "Electro House"),
"funky-jackin-groovy": (15, "Funky / Jackin / Groovy"),
"garage": (29, "Garage"),
"house": (1, "House"),
"lounge-chill-out": (10, "Lounge / Chill Out"),
"melodic-house-techno-progressive-house": (34, "Melodic House / Techno / Progressive House"),
"minimal-deep-tech": (27, "Minimal / Deep Tech"),
"organic-house-downtempo": (36, "Organic House / Downtempo"),
"progressive-house": (28, "Progressive House"),
"r-and-b-hip-hop": (6, "R&B / Hip Hop"),
"soulful-house": (24, "Soulful House"),
"tech-house": (18, "Tech House"),
"techno-peak-time-driving": (26, "Techno (Peak Time / Driving)"),
"techno-raw-deep-hypnotic": (32, "Techno (Raw / Deep / Hypnotic)"),
"trance": (30, "Trance"),
"world-reggae": (21, "World / Reggae"),
}
# NOTA: la mappa è enumerata in fase di implementazione fetchando /top con curl_cffi
# e cercando href="/genre/<id>/<slug>". Vedi Task 1 del plan.
@dataclass(frozen=True)
class TraxsourceTrack:
position: int
title: str
mix: str
artists: str # "A, B & C" formattato
label: str
traxsource_id: int
slug: str
image_url: str = "" # URL thumbnail (per UI)
cover_url_large: str = "" # URL cover 500x500 (per tagging)
class TraxsourceError(Exception): pass
class TraxsourceUnreachableError(TraxsourceError): pass
class TraxsourceParseError(TraxsourceError): pass
def list_genres() -> list:
"""Ritorna [{slug, id, name}, ...] ordinato alfabeticamente per name."""
def fetch_top100(slug: str, force_refresh: bool = False) -> list[TraxsourceTrack]:
"""Fetches Top 100 corrente per il genere.
1. Scopre l'URL della playlist Top 100 del mese via GET /genre/<id>/<slug>
2. Fetch della playlist /title/<id>/top-100-...
3. Parse HTML con BeautifulSoup
4. Cache in-memory 15 min
Raises:
ValueError se slug non in GENRES
TraxsourceUnreachableError su rete/5xx dopo retry
TraxsourceParseError su HTML non conforme (title link non trovato, meno di 50 track, ecc.)
"""
```
**Internals:**
- `_session()` singleton — inizializza `curl_cffi.Session(impersonate='chrome131')` e fa GET a `/` una volta per riscaldare CF cookies. Riusata per tutte le fetch successive.
- `_discover_top100_url(session, slug) → str` — fetcha `/genre/<id>/<slug>`, regex per il link Top 100
- `_parse_tracks(html) → list[TraxsourceTrack]` — BeautifulSoup, itera `div.top-item.play-trk`, estrae campi
- `_split_title_mix(full_title) → (title, mix)` — parsa `"Foo (Extended Mix)"` → `("Foo", "Extended Mix")`. Se no parentesi, mix = "".
- `_format_artists(anchors) → str` — join `[a.text for a in anchors]` con formattazione "A, B & C"
- `_large_cover(url) → str` — sostituisce `/52x52/` con `/500x500/` nel path
**Retry + timeout:** riusa gli stessi pattern di `core/beatport.py`: 3 tentativi con backoff [1, 3]s, timeout 15s.
### Nuovi metodi in `api/bridge.py`
Paralleli ai metodi Beatport (stesso schema):
- `traxsource_genres() → list[dict]`
- `traxsource_fetch_chart(slug, force_refresh) → {ok, tracks} | {ok:false, error, message}` — salva anche `traxsource_last_genre` in config
- `traxsource_check_existing(tracks, genre_name) → list[bool]` — check in `<output_dir>/Traxsource_<genre>/`
- `traxsource_download_selected(tracks, genre_name) → dict` — costruisce metadata (title/artist/album=label/date=YYYY-MM/genre=display_name/cover_url) e chiama `start_tracks_download` riusando il pipeline con tagging ID3
**Metadata per il tagger:**
- title, artist: dai campi track
- album: label Traxsource (best proxy — non c'è "album" per singole tracce)
- date: mese/anno corrente (dato che la Top 100 è mensile)
- genre: display_name del genere
- cover_url: `cover_url_large` (500x500)
### Frontend
Nuova tab in sidebar dopo YouTube (o Beatport):
```html
<button class="nav-item" data-view="traxsource" data-feature="audio">
<span class="nav-icon">▲</span><span>Traxsource</span>
</button>
```
Section `#view-traxsource` con struttura identica a Beatport (dropdown genere + Carica Top 100 + tabella + toolbar + log). Riusa tutte le classi `.beatport-*` per lo stile.
Modulo `TraxsourceUI` in `webui/js/app.js` — copia diretta di `BeatportUI` con selettori cambiati (`traxsource-*` invece di `beatport-*`) e chiamate API alle nuove funzioni.
**Ordinamento colonne** (sortable) e **cover art** già supportati riusando la stessa struttura tabella + modulo helper `_sortPairs`/`_bindSortableHeaders`/`_updateSortArrows` esistenti.
### Persistenza
Nuovo campo in `core/config.py::DEFAULTS`:
```python
"traxsource_last_genre": "tech-house",
```
### Dipendenze
- `beautifulsoup4` — nuova dep runtime. Aggiungere in `requirements.txt`. Serve `--collect-data bs4` in build_windows.py + build_macos.py? Verificare — bs4 di solito no.
## Testing
**Unit tests `tests/test_traxsource.py` (senza rete, con fixture HTML):**
Serve una fixture HTML reale della pagina Top 100 (~400KB). Task 1 la genera con `curl_cffi` come fatto per Beatport (`beatport_melodic_top100.html`).
| Test | Verifica |
|---|---|
| `test_list_genres_shape` | Lista dict con {slug, id, name}, ordinati |
| `test_parse_top100_extracts_100_tracks` | Fixture HTML → 100 track |
| `test_parse_positions_sequential` | 1..100 senza buchi |
| `test_parse_track_shape` | Ogni track ha title, artists, label, cover URLs non vuoti |
| `test_split_title_mix_with_parens` | `"Foo (Extended Mix)"` → `("Foo", "Extended Mix")` |
| `test_split_title_mix_no_parens` | `"Foo"` → `("Foo", "")` |
| `test_format_artists_multi` | `["A", "B", "C"]` → `"A, B & C"` |
| `test_large_cover_url_substitution` | `.../52x52/x.jpg` → `.../500x500/x.jpg` |
| `test_discover_top100_url_returns_playlist_link` | HTML genre stub → estrae `/title/N/top-100-...` |
| `test_fetch_top100_invalid_slug_raises_value_error` | slug non in GENRES → ValueError early |
Mock HTTP con `unittest.mock.patch("core.traxsource._session")` (patch della session singleton).
**Test manuale end-to-end:**
1. Avvia app → tab Traxsource
2. Selezione "Tech House" → Carica Top 100 → tabella 100 righe in <5s
3. Verifica cover art (thumbnail nella tabella)
4. Deseleziona 90, tieni 10 → Scarica selezionati
5. File in `MUSICA/Traxsource_Tech House/` con tag ID3 completi (title, artist, album=label, genre=Tech House, cover 500x500)
6. Ricarica stesso genere → cache hit
7. Riavvio → ultimo genere ricordato
## Rollout
1. Branch `feat/traxsource-charts`
2. Bump `core/config.py::VERSION` → `v1.9.0` (minor: nuova tab visibile)
3. Note release `/tmp/notes-v1.9.0.md`
4. Merge → tag → CI → server DB update (stesso flusso di sempre)
## Struttura file impattati
**Nuovi:**
- `core/traxsource.py` (~250 LOC)
- `tests/test_traxsource.py` (~200 LOC)
- `tests/fixtures/traxsource_tech_house_top100.html` (~400KB)
- `scripts/refresh_traxsource_genres.py` (~50 LOC, opzionale)
**Modificati:**
- `requirements.txt` — `beautifulsoup4>=4.12.0`
- `build_windows.py` + `build_macos.py` — verificare se serve `--collect-data bs4`
- `core/config.py` — VERSION bump + `traxsource_last_genre`
- `api/bridge.py` — 4 metodi Api `traxsource_*` (~150 LOC)
- `webui/index.html` — nav-item + section (~90 LOC)
- `webui/js/app.js` — modulo `TraxsourceUI` (~250 LOC, copia BeatportUI adattata)
## Considerazioni operative
- **Fragility scraping:** i class name di Traxsource (`com-title`, `com-artists`, `top-item`, ecc.) sono usati anche nel JS del sito → stabili nel tempo. Rischio: se Traxsource migra a un framework nuovo (React/Next), parser va rifatto.
- **Rate limiting:** un solo GET per la genre + un GET per la playlist Top 100 = 2 richieste per caricare la chart. Cache 15 min. Zero rischio ban in uso normale.
- **Legal:** stesso perimetro di Beatport — lettura di pagine pubbliche, no ToS violation esplicito.
- **Comparabilità con Beatport:** un utente power potrebbe usare entrambe le tab per triangolare le release del mese. UX consistente = curva apprendimento zero.
+1
View File
@@ -10,6 +10,7 @@ pyobjc-framework-AVFoundation>=10.0 ; sys_platform == "darwin"
pythonnet==3.0.5 ; sys_platform == "win32"
clr-loader==0.2.7.post0 ; sys_platform == "win32"
curl_cffi>=0.9.0
beautifulsoup4>=4.12.0
# --- dev only ---
pytest>=8.0.0
+34
View File
@@ -0,0 +1,34 @@
"""Estrae la lista dei generi Traxsource dalla pagina /top.
Uso: python3 scripts/refresh_traxsource_genres.py > /tmp/tx_genres.txt
Poi copia manualmente in core/traxsource.py::GENRES."""
from __future__ import annotations
import re
import sys
from curl_cffi import requests
def main() -> int:
s = requests.Session(impersonate="chrome131")
s.get("https://www.traxsource.com/", timeout=15)
r = s.get("https://www.traxsource.com/top", timeout=15,
headers={"Referer": "https://www.traxsource.com/"})
if r.status_code != 200:
print(f"HTTP {r.status_code}", file=sys.stderr)
return 1
# href="/genre/<id>/<slug>"
hits = re.findall(r'href="/genre/(\d+)/([a-z0-9-]+)"', r.text)
seen = set()
for gid, slug in hits:
if slug in seen:
continue
seen.add(slug)
# Display name = slug con capitalizzazione euristica (l'utente lo raffina a mano)
name = " ".join(w.capitalize() for w in slug.replace("-", " ").split())
print(f' "{slug}": ({gid}, "{name}"),')
return 0
if __name__ == "__main__":
sys.exit(main())
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+217
View File
@@ -0,0 +1,217 @@
"""Test per core.traxsource."""
from __future__ import annotations
import pytest
from core import traxsource
class TestListGenres:
def test_returns_list_of_dicts(self):
result = traxsource.list_genres()
assert isinstance(result, list)
assert len(result) >= 10
for g in result:
assert set(g.keys()) == {"slug", "id", "name"}
assert isinstance(g["slug"], str) and g["slug"]
assert isinstance(g["id"], int) and g["id"] > 0
assert isinstance(g["name"], str) and g["name"]
def test_sorted_alphabetically_by_name(self):
result = traxsource.list_genres()
names = [g["name"] for g in result]
assert names == sorted(names, key=str.casefold)
def test_tech_house_present(self):
result = traxsource.list_genres()
slugs = [g["slug"] for g in result]
assert "tech-house" in slugs
class TestTraxsourceTrack:
def test_frozen(self):
t = traxsource.TraxsourceTrack(
position=1, title="X", mix="Y", artists="A", label="L",
traxsource_id=1, slug="x",
)
with pytest.raises(Exception):
t.title = "Z"
def test_defaults(self):
t = traxsource.TraxsourceTrack(
position=1, title="X", mix="", artists="A", label="",
traxsource_id=1, slug="x",
)
assert t.image_url == ""
assert t.cover_url_large == ""
class TestSplitTitleMix:
def test_with_parens(self):
assert traxsource._split_title_mix("Foo (Extended Mix)") == ("Foo", "Extended Mix")
def test_without_parens(self):
assert traxsource._split_title_mix("Foo") == ("Foo", "")
def test_multiple_parens_takes_last(self):
# es. "Foo (feat. Bar) (Original Mix)" -> mix = "Original Mix"
assert traxsource._split_title_mix("Foo (feat. Bar) (Original Mix)") == ("Foo (feat. Bar)", "Original Mix")
def test_empty(self):
assert traxsource._split_title_mix("") == ("", "")
class TestFormatArtists:
def test_single(self):
assert traxsource._format_artists(["Kapuchon"]) == "Kapuchon"
def test_two(self):
assert traxsource._format_artists(["A", "B"]) == "A & B"
def test_three(self):
assert traxsource._format_artists(["A", "B", "C"]) == "A, B & C"
def test_empty(self):
assert traxsource._format_artists([]) == ""
def test_strips_whitespace(self):
assert traxsource._format_artists([" A ", " B "]) == "A & B"
class TestLargeCover:
def test_substitutes_52x52_with_500x500(self):
url = "https://www.traxsource.com/scripts/image.php/52x52/abc.jpg"
assert traxsource._large_cover(url) == "https://www.traxsource.com/scripts/image.php/500x500/abc.jpg"
def test_no_change_when_no_pattern(self):
assert traxsource._large_cover("https://example.com/x.jpg") == "https://example.com/x.jpg"
def test_empty(self):
assert traxsource._large_cover("") == ""
class TestExceptions:
def test_exceptions_are_subclasses(self):
assert issubclass(traxsource.TraxsourceUnreachableError, traxsource.TraxsourceError)
assert issubclass(traxsource.TraxsourceParseError, traxsource.TraxsourceError)
class TestDiscoverTop100Url:
def test_extracts_link_from_genre_page(self, fixtures_dir):
html = (fixtures_dir / "traxsource_tech_house_genre.html").read_text()
url = traxsource._discover_top100_url(html)
assert url.startswith("/title/")
assert "top-100" in url
def test_raises_when_no_link(self):
with pytest.raises(traxsource.TraxsourceParseError, match="Top 100"):
traxsource._discover_top100_url("<html>nulla</html>")
class TestParseTracks:
def test_parses_100_tracks(self, fixtures_dir):
html = (fixtures_dir / "traxsource_tech_house_top100.html").read_text()
tracks = traxsource._parse_tracks(html)
assert len(tracks) == 100
def test_positions_sequential_1_to_100(self, fixtures_dir):
html = (fixtures_dir / "traxsource_tech_house_top100.html").read_text()
tracks = traxsource._parse_tracks(html)
positions = [t.position for t in tracks]
assert positions == list(range(1, 101))
def test_track_shape(self, fixtures_dir):
html = (fixtures_dir / "traxsource_tech_house_top100.html").read_text()
tracks = traxsource._parse_tracks(html)
first = tracks[0]
assert first.title
assert first.artists
assert first.traxsource_id > 0
assert first.slug
assert first.image_url.startswith("https://")
assert first.cover_url_large.startswith("https://")
assert "500x500" in first.cover_url_large
def test_raises_when_no_tracks(self):
with pytest.raises(traxsource.TraxsourceParseError, match="track"):
traxsource._parse_tracks("<html>vuoto</html>")
from unittest.mock import patch, MagicMock
def _mock_response(text: str, status_code: int = 200) -> MagicMock:
resp = MagicMock()
resp.text = text
resp.status_code = status_code
def _raise():
if status_code >= 400:
raise Exception(f"HTTP {status_code}")
resp.raise_for_status = _raise
return resp
class TestFetchTop100:
@pytest.fixture
def genre_html(self, fixtures_dir):
return (fixtures_dir / "traxsource_tech_house_genre.html").read_text()
@pytest.fixture
def top100_html(self, fixtures_dir):
return (fixtures_dir / "traxsource_tech_house_top100.html").read_text()
def test_success_returns_100_tracks(self, genre_html, top100_html):
traxsource._cache.clear()
mock_sess = MagicMock()
mock_sess.get.side_effect = [
_mock_response(genre_html, 200),
_mock_response(top100_html, 200),
]
with patch("core.traxsource._session", return_value=mock_sess):
tracks = traxsource.fetch_top100("tech-house")
assert len(tracks) == 100
def test_invalid_slug_raises_value_error(self):
with pytest.raises(ValueError, match="slug"):
traxsource.fetch_top100("not-a-genre")
def test_5xx_retries_and_raises_unreachable(self):
traxsource._cache.clear()
mock_sess = MagicMock()
mock_sess.get.return_value = _mock_response("", 503)
with patch("core.traxsource._session", return_value=mock_sess), \
patch("core.traxsource.time.sleep"):
with pytest.raises(traxsource.TraxsourceUnreachableError):
traxsource.fetch_top100("tech-house")
# 3 tentativi
assert mock_sess.get.call_count == 3
def test_cache_hit_within_ttl(self, genre_html, top100_html):
traxsource._cache.clear()
mock_sess = MagicMock()
mock_sess.get.side_effect = [
_mock_response(genre_html, 200),
_mock_response(top100_html, 200),
]
with patch("core.traxsource._session", return_value=mock_sess):
traxsource.fetch_top100("tech-house")
traxsource.fetch_top100("tech-house")
# 2 chiamate al primo fetch (genre + top100), 0 al secondo
assert mock_sess.get.call_count == 2
def test_force_refresh_bypasses_cache(self, genre_html, top100_html):
traxsource._cache.clear()
mock_sess = MagicMock()
# 4 risposte (2 fetch x 2 richieste ciascuno)
mock_sess.get.side_effect = [
_mock_response(genre_html, 200),
_mock_response(top100_html, 200),
_mock_response(genre_html, 200),
_mock_response(top100_html, 200),
]
with patch("core.traxsource._session", return_value=mock_sess):
traxsource.fetch_top100("tech-house")
traxsource.fetch_top100("tech-house", force_refresh=True)
assert mock_sess.get.call_count == 4
+169
View File
@@ -0,0 +1,169 @@
"""Test unitari per le funzioni di archive-lookup in core.upgrader.
Focalizzati su _normalize_stem, _scan_archive, _find_candidates.
Non testiamo il flow end-to-end di upgrade_folder (richiederebbe mock
di subprocess/yt-dlp/ffmpeg — out of scope, coperto da smoke test manuale).
"""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
from core import upgrader
# ============================================================
# _normalize_stem
# ============================================================
class TestNormalizeStem:
def test_lowercase_and_dedup(self):
# "Hot" e "hot" → un solo token; token duplicati collassano nel set
got = upgrader._normalize_stem("Hot Sauce hot sauce")
assert got == {"hot", "sauce"}
def test_filters_short_tokens(self):
# Token di lunghezza < 3 vengono scartati (di, il, a, b, cd...)
got = upgrader._normalize_stem("A B cd Boombox")
assert "a" not in got
assert "b" not in got
assert "cd" not in got
assert "boombox" in got
def test_removes_punctuation(self):
# Trattini, virgole, parentesi, apostrofi → tutti sostituiti con spazi
got = upgrader._normalize_stem("Artist - Title (Extended Mix)")
assert got == {"artist", "title", "extended", "mix"}
def test_empty_input(self):
assert upgrader._normalize_stem("") == set()
assert upgrader._normalize_stem(" ") == set()
def test_only_short_tokens_returns_empty(self):
assert upgrader._normalize_stem("a b c d") == set()
# ============================================================
# _scan_archive
# ============================================================
class TestScanArchive:
def test_recursive_scan(self, tmp_path: Path):
# Crea albero: top-level + sub/ + sub/sub2/
(tmp_path / "Artist - Song.mp3").touch()
(tmp_path / "sub").mkdir()
(tmp_path / "sub" / "Second Track.mp3").touch()
(tmp_path / "sub" / "sub2").mkdir()
(tmp_path / "sub" / "sub2" / "Deep One.m4a").touch()
# File non-audio devono essere ignorati
(tmp_path / "readme.txt").touch()
index = upgrader._scan_archive(str(tmp_path))
# 3 entries totali (una per ogni file audio, ognuna con token distinti)
assert len(index) == 3
# Verifica che le path siano riferite ai file corretti
all_paths = [p for paths in index.values() for p in paths]
names = sorted(p.name for p in all_paths)
assert names == ["Artist - Song.mp3", "Deep One.m4a", "Second Track.mp3"]
def test_case_insensitive_extensions(self, tmp_path: Path):
(tmp_path / "one.MP3").touch()
(tmp_path / "two.Mp3").touch()
(tmp_path / "three.WAV").touch()
(tmp_path / "four.FLAC").touch()
(tmp_path / "five.txt").touch() # non-audio: ignorato
index = upgrader._scan_archive(str(tmp_path))
all_paths = [p for paths in index.values() for p in paths]
assert len(all_paths) == 4 # 4 file audio, 1 skippato
def test_duplicates_appended(self, tmp_path: Path):
# Due file con stesso token set → stessa key, due path
(tmp_path / "sub1").mkdir()
(tmp_path / "sub2").mkdir()
(tmp_path / "sub1" / "Hot Sauce.mp3").touch()
(tmp_path / "sub2" / "Hot Sauce.mp3").touch()
index = upgrader._scan_archive(str(tmp_path))
assert len(index) == 1
# Una sola chiave, con 2 path
paths = list(index.values())[0]
assert len(paths) == 2
def test_nonexistent_dir_returns_empty(self, tmp_path: Path):
assert upgrader._scan_archive(str(tmp_path / "nope")) == {}
def test_empty_stems_skipped(self, tmp_path: Path):
# File il cui stem produce zero token utili (solo caratteri corti) → skip
(tmp_path / "a.mp3").touch()
(tmp_path / "Real Track Name.mp3").touch()
index = upgrader._scan_archive(str(tmp_path))
# Solo "Real Track Name.mp3" ha token >=3 chars
all_paths = [p for paths in index.values() for p in paths]
assert len(all_paths) == 1
assert all_paths[0].name == "Real Track Name.mp3"
# ============================================================
# _find_candidates
# ============================================================
class TestFindCandidates:
def _fake_index(self, tmp_path: Path, entries: list) -> dict:
"""Helper: crea file (touch) e ritorna un index manuale."""
index: dict = {}
for name in entries:
p = tmp_path / name
p.touch()
tokens = upgrader._normalize_stem(p.stem)
key = upgrader._tokens_to_key(tokens)
index.setdefault(key, []).append(p)
return index
def test_ranking_bitrate_desc_then_similarity_desc(self, tmp_path: Path):
# 3 candidati: variamo bitrate + similarity per verificare l'ordine
# a: sim alta (0.75), bitrate basso (128)
# b: sim media (0.5), bitrate alto (320)
# c: sim alta (0.75), bitrate medio (192)
# Ordine atteso: b(320) > c(192, sim 0.75) > a(128)
idx = self._fake_index(tmp_path, [
"Artist - Hot Sauce.mp3", # a: 3 token comuni su 4 = 0.75
"Different Song Boombox.mp3", # b: 1 su 5 = 0.2 (troppo bassa, filtrato)
"Artist Hot Sauce Extended.mp3", # c: 3 su 4 = 0.75
])
# Target: "Artist Hot Sauce" → tokens = {artist, hot, sauce}
target = "Artist Hot Sauce"
# Mock bitrate: mappa nome → kbps
def fake_bitrate(p):
n = Path(p).name
if n == "Artist - Hot Sauce.mp3": return 128
if n == "Different Song Boombox.mp3": return 320
if n == "Artist Hot Sauce Extended.mp3": return 192
return 0
with patch.object(upgrader, "get_bitrate", side_effect=fake_bitrate):
results = upgrader._find_candidates(target, idx, min_similarity=0.3)
# "b" viene filtrato (sim 1/5 = 0.2 < 0.3 min); restano a e c
assert len(results) == 2
# c (bitrate 192) prima di a (bitrate 128)
assert results[0][0].name == "Artist Hot Sauce Extended.mp3"
assert results[1][0].name == "Artist - Hot Sauce.mp3"
def test_below_threshold_filtered(self, tmp_path: Path):
# Un solo candidato con similarity ~ 1/5 = 0.2 → sotto default 0.5 → escluso
idx = self._fake_index(tmp_path, [
"Foo Bar Baz Qux Extra.mp3",
])
with patch.object(upgrader, "get_bitrate", return_value=320):
results = upgrader._find_candidates("Hot Sauce", idx) # solo "hot"/"sauce"
assert results == []
def test_exact_match_returned(self, tmp_path: Path):
idx = self._fake_index(tmp_path, [
"Hot Sauce Extended.mp3",
])
with patch.object(upgrader, "get_bitrate", return_value=320):
results = upgrader._find_candidates("Hot Sauce Extended", idx)
assert len(results) == 1
assert results[0][1] == 1.0 # perfect Jaccard
assert results[0][2] == 320
+118
View File
@@ -1680,8 +1680,21 @@ input[type="number"]::-webkit-inner-spin-button {
}
.beatport-table .col-pos { width: 40px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.beatport-table .col-dur { width: 70px; color: var(--text-2); font-variant-numeric: tabular-nums; text-align: right; }
.beatport-table .col-date { width: 100px; color: var(--text-2); font-variant-numeric: tabular-nums; font-size: 12px; white-space: nowrap; }
.beatport-table .col-state { width: 130px; font-size: 12px; }
.beatport-table thead th.sortable {
cursor: pointer;
user-select: none;
transition: color 0.15s ease;
}
.beatport-table thead th.sortable:hover { color: var(--text); }
.beatport-table thead th .sort-arrow {
font-size: 10px;
margin-left: 4px;
color: var(--indigo-text);
}
.beatport-table input[type="checkbox"] {
width: 16px;
height: 16px;
@@ -1708,3 +1721,108 @@ input[type="number"]::-webkit-inner-spin-button {
.hint-inline { margin-left: 0; }
.beatport-table .col-state { width: 110px; }
}
/* ===============================
UPGRADE — picker modal (match locale)
=============================== */
.modal-body.upgrade-picker-body {
/* Override del default .modal-body (mono + pre-wrap): qui è UI, non log */
font-family: inherit;
font-size: 13px;
color: var(--text);
white-space: normal;
user-select: none;
}
/* Modal picker: più largo dei modal standard + footer con wrap
per far entrare i 4 bottoni senza troncare */
#upgradePickerModal .modal {
min-width: 620px;
max-width: 780px;
}
#upgradePickerModal .modal-foot {
flex-wrap: wrap;
justify-content: flex-end;
}
.modal-sub {
font-size: 12px;
color: var(--text-2);
margin-bottom: 12px;
word-break: break-word;
}
.upgrade-picker-list {
display: flex;
flex-direction: column;
gap: 6px;
max-height: 380px;
overflow-y: auto;
}
.upgrade-picker-row {
display: flex;
align-items: flex-start;
gap: 10px;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: var(--r-md, 10px);
cursor: pointer;
transition: border-color 0.15s ease, background 0.15s ease;
background: var(--bg-input, transparent);
}
.upgrade-picker-row:hover { border-color: var(--text-2); }
.upgrade-picker-row.selected {
border-color: var(--blue, #3b82f6);
background: rgba(59, 130, 246, 0.08);
}
.upgrade-picker-row input[type="radio"] {
margin-top: 3px;
cursor: pointer;
}
.upgrade-picker-info {
flex: 1;
min-width: 0;
}
.upgrade-picker-play {
display: flex;
align-items: center;
flex-shrink: 0;
}
.upgrade-preview-btn {
width: 34px;
height: 34px;
border-radius: 50%;
border: 1px solid var(--border);
background: var(--bg-input, transparent);
color: var(--text);
font-size: 13px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0;
margin-left: 6px;
transition: background 0.15s ease, transform 0.1s ease;
}
.upgrade-preview-btn:hover { background: var(--bg-input-hover, rgba(255,255,255,0.05)); }
.upgrade-preview-btn.playing {
background: var(--green, #1DB954);
color: #000;
border-color: var(--green, #1DB954);
}
.upgrade-picker-title {
font-weight: 600;
color: var(--text);
word-break: break-word;
}
.upgrade-picker-meta {
font-size: 12px;
color: var(--text-2);
margin-top: 3px;
font-variant-numeric: tabular-nums;
}
.upgrade-picker-path {
font-size: 11px;
color: var(--text-3);
margin-top: 2px;
word-break: break-all;
font-family: "SF Mono", Menlo, Consolas, monospace;
}
+115 -13
View File
@@ -33,6 +33,10 @@
<span class="nav-icon">🎧</span>
<span>Beatport</span>
</button>
<button class="nav-item" data-view="traxsource" data-feature="audio">
<span class="nav-icon">▲</span>
<span>Traxsource</span>
</button>
<button class="nav-item" data-view="spotify" data-feature="audio">
<span class="nav-icon">🟢</span>
<span>Spotify</span>
@@ -268,6 +272,23 @@
<label class="field-label">Soglia HQ (kbps)</label>
<input type="number" id="upThreshold" class="input pill input-sm" value="310" />
</div>
<div class="field">
<label class="field-label">Cartella archivio (opzionale)</label>
<div class="row">
<div class="path-display empty" id="upArchivePathDisplay">Nessuna cartella</div>
<button class="btn btn-ghost pill btn-sm" id="upArchiveBrowseBtn">📂 Sfoglia</button>
<button class="btn btn-ghost pill btn-sm" id="upArchiveClearBtn" hidden>Rimuovi</button>
</div>
<label style="display:flex;gap:6px;align-items:center;margin-top:8px;font-size:13px;color:var(--text-2);">
<input type="checkbox" id="upArchiveAutoPick"/>
<span>Scelta automatica se ci sono più match (usa quello a bitrate più alto)</span>
</label>
<div class="hint">
<span class="hint-ico">ⓘ</span>
<div>Se selezionata, l'app cerca versioni HQ del brano nella tua libreria locale prima di scaricare da YouTube. In caso di match multipli scegli tu quale usare — oppure attiva "Scelta automatica" per non fermarti.</div>
</div>
</div>
</div>
<div class="card actions">
@@ -896,10 +917,11 @@
<tr>
<th class="col-check"><input type="checkbox" id="beatport-select-all" /></th>
<th class="col-cover"></th>
<th class="col-pos">#</th>
<th>Artista</th>
<th>Titolo (Mix)</th>
<th class="col-dur">Durata</th>
<th class="col-pos sortable" data-sort="position">#</th>
<th class="sortable" data-sort="artists">Artista</th>
<th class="sortable" data-sort="title">Titolo (Mix)</th>
<th class="col-dur sortable" data-sort="duration_sec">Durata</th>
<th class="col-date sortable" data-sort="release_date">Data</th>
<th class="col-state">Stato</th>
</tr>
</thead>
@@ -923,6 +945,64 @@
</div>
</section>
<!-- ===== VIEW: TRAXSOURCE ===== -->
<section class="view" id="view-traxsource">
<header class="hero hero-purple">
<div class="hero-content">
<div class="hero-eyebrow purple">TRAXSOURCE CHARTS</div>
<h1 class="hero-title">Top 100 Traxsource per genere</h1>
<p class="hero-subtitle">La classifica mensile ufficiale. Scarica in un click con tag ID3 completi (label, cover art, genere).</p>
</div>
<div class="hero-deco">▲</div>
</header>
<h2 class="section-label">Genere</h2>
<div class="card">
<div class="beatport-header">
<select id="traxsource-genre" class="input pill"></select>
<button class="btn btn-primary pill" id="traxsource-load-btn">
<span class="ico">↓</span> Carica Top 100
</button>
<span class="hint-inline">Shift+click per bypassare la cache</span>
</div>
<div id="traxsource-status" class="beatport-status"></div>
<div id="traxsource-output-info" class="beatport-output-info"></div>
</div>
<div class="card beatport-table-card">
<div class="beatport-table-wrap">
<table id="traxsource-table" class="beatport-table" hidden>
<thead>
<tr>
<th class="col-check"><input type="checkbox" id="traxsource-select-all" /></th>
<th class="col-cover"></th>
<th class="col-pos sortable" data-sort="position">#</th>
<th class="sortable" data-sort="artists">Artista</th>
<th class="sortable" data-sort="title">Titolo (Mix)</th>
<th class="sortable" data-sort="label">Label</th>
<th class="col-state">Stato</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="beatport-toolbar" id="traxsource-toolbar" hidden>
<span class="counter" id="traxsource-selected-count">0/0 selezionati</span>
<button class="btn btn-primary pill" id="traxsource-download-btn" disabled>
<span class="ico">▶</span> Scarica selezionati
</button>
<button class="btn btn-danger pill" id="traxsource-stop-btn" hidden>
<span class="ico">◼</span> Interrompi
</button>
</div>
</div>
<h2 class="section-label">Log</h2>
<div class="card log-card">
<div class="log" id="traxsource-log"></div>
</div>
</section>
<!-- ===== VIEW: SPOTIFY SEARCH ===== -->
<section class="view" id="view-spotify">
<header class="hero hero-indigo">
@@ -957,11 +1037,12 @@
<tr>
<th class="col-check"><input type="checkbox" id="spotify-select-all" checked /></th>
<th class="col-cover"></th>
<th class="col-pos">#</th>
<th>Artista</th>
<th>Titolo</th>
<th>Album</th>
<th class="col-dur">Durata</th>
<th class="col-pos sortable" data-sort="position">#</th>
<th class="sortable" data-sort="artists">Artista</th>
<th class="sortable" data-sort="name">Titolo</th>
<th class="sortable" data-sort="album">Album</th>
<th class="col-dur sortable" data-sort="duration_sec">Durata</th>
<th class="col-date sortable" data-sort="release_date">Data</th>
<th class="col-state"></th>
</tr>
</thead>
@@ -1015,10 +1096,11 @@
<tr>
<th class="col-check"><input type="checkbox" id="youtube-select-all" checked /></th>
<th class="col-cover"></th>
<th class="col-pos">#</th>
<th>Titolo video</th>
<th>Canale</th>
<th class="col-dur">Durata</th>
<th class="col-pos sortable" data-sort="position">#</th>
<th class="sortable" data-sort="title">Titolo video</th>
<th class="sortable" data-sort="channel">Canale</th>
<th class="col-dur sortable" data-sort="duration_sec">Durata</th>
<th class="col-date sortable" data-sort="release_date">Data</th>
<th class="col-state"></th>
</tr>
</thead>
@@ -1197,6 +1279,26 @@
</div>
</div>
<!-- ============== MODAL: Match locale Upgrade ============== -->
<div class="modal-backdrop" id="upgradePickerModal" hidden>
<div class="modal">
<div class="modal-head">
<h2>Match locali trovati</h2>
<button class="modal-close" id="upgradePickerCloseBtn">×</button>
</div>
<div class="modal-body upgrade-picker-body">
<div class="modal-sub" id="upgradePickerFilename"></div>
<div id="upgradePickerList" class="upgrade-picker-list"></div>
</div>
<div class="modal-foot">
<button class="btn btn-primary pill" id="upgradePickerUseBtn" disabled>Usa selezionato</button>
<button class="btn btn-ghost pill" id="upgradePickerYoutubeBtn">Cerca su YouTube</button>
<button class="btn btn-ghost pill" id="upgradePickerSkipBtn">Salta</button>
<button class="btn btn-danger pill" id="upgradePickerCancelAllBtn">■ Annulla tutto</button>
</div>
</div>
</div>
<!-- ============== TOAST ============== -->
<div class="toast" id="toast"></div>
+701 -18
View File
@@ -12,6 +12,8 @@ const state = {
loaded: null, // { kind: "urls"|"tracks", urls?, tracks?, count }
dlOutputDir: "",
upDir: "",
upArchiveDir: "",
upPicker: { file: "", candidates: [], selectedIdx: -1 },
videoOutputDir: "",
showSecret: false,
downloading: false,
@@ -310,8 +312,13 @@ const bridgeHandlers = {
state.upgrading = false;
$("#upgradeBtn").disabled = false;
$("#stopUpgradeBtn").disabled = true;
// Chiudi eventuale modal picker rimasto aperto
const modal = $("#upgradePickerModal");
if (modal && !modal.hidden) modal.hidden = true;
},
"upgrade:candidates_needed": (p) => openUpgradePicker(p),
"video:progress": (p) => {
if (typeof p.overall === "number") {
const pct = Math.round(p.overall * 100);
@@ -400,6 +407,7 @@ const logEls = {
spotify: () => $("#spotify-log"),
youtube: () => $("#youtube-log"),
convert: () => $("#convert-log"),
traxsource: () => $("#traxsource-log"),
};
function classifyLog(msg) {
@@ -512,6 +520,9 @@ async function init() {
await SpotifyUI.init();
await YoutubeUI.init();
// Traxsource charts tab
await TraxsourceUI.init();
// WAV -> MP3 converter tab
await ConvertUI.init();
}
@@ -710,6 +721,26 @@ $("#upBrowseBtn").addEventListener("click", async () => {
$("#upRecursive").addEventListener("change", refreshUpgradeScan);
// --- Cartella archivio (opzionale) ---
$("#upArchiveBrowseBtn").addEventListener("click", async () => {
const path = await window.pywebview.api.browse_directory();
if (path) {
state.upArchiveDir = path;
const disp = $("#upArchivePathDisplay");
disp.textContent = path;
disp.classList.remove("empty");
$("#upArchiveClearBtn").hidden = false;
}
});
$("#upArchiveClearBtn").addEventListener("click", () => {
state.upArchiveDir = "";
const disp = $("#upArchivePathDisplay");
disp.textContent = "Nessuna cartella";
disp.classList.add("empty");
$("#upArchiveClearBtn").hidden = true;
});
async function refreshUpgradeScan() {
if (!state.upDir) return;
const res = await window.pywebview.api.scan_audio_folder(state.upDir, $("#upRecursive").checked);
@@ -744,6 +775,8 @@ $("#upgradeBtn").addEventListener("click", async () => {
directory: state.upDir,
recursive: $("#upRecursive").checked,
threshold: parseInt($("#upThreshold").value, 10) || 310,
archive_dir: state.upArchiveDir || "",
archive_auto_pick: $("#upArchiveAutoPick").checked,
});
if (!res.ok) {
if (!handleGateBlock(res)) toast(res.error || "Errore", "error");
@@ -756,6 +789,205 @@ $("#stopUpgradeBtn").addEventListener("click", async () => {
$("#stopUpgradeBtn").disabled = true;
});
// --- Modal picker "Match locali multipli" ---
function _fmtBytes(n) {
if (!n || n < 0) return "—";
if (n < 1024) return n + " B";
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + " KB";
if (n < 1024 * 1024 * 1024) return (n / (1024 * 1024)).toFixed(1) + " MB";
return (n / (1024 * 1024 * 1024)).toFixed(2) + " GB";
}
function _basename(p) {
if (!p) return "";
const parts = String(p).split(/[/\\]/);
return parts[parts.length - 1] || p;
}
function _escapeHtml(s) {
return String(s ?? "").replace(/[&<>"']/g, (c) => ({
"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"
}[c]));
}
// --- Audio preview per il picker (single audio element condiviso) ---
let _upgradePreviewAudio = null;
let _upgradePreviewBtn = null; // bottone corrente in "playing" state
function _fileUrl(absPath) {
if (!absPath) return "";
// Split percorso e encode ogni segment (spazi → %20, unicode → utf-8 encoded)
const parts = String(absPath).split("/").map(encodeURIComponent);
return "file://" + parts.join("/");
}
function _stopUpgradePreview() {
if (_upgradePreviewAudio) {
// Rimuovi handler error PRIMA di pause/clear così stop manuale
// non fa scattare il toast di errore
_upgradePreviewAudio.onerror = null;
_upgradePreviewAudio.onended = null;
try { _upgradePreviewAudio.pause(); } catch {}
try { _upgradePreviewAudio.removeAttribute("src"); } catch {}
_upgradePreviewAudio = null;
}
if (_upgradePreviewBtn) {
_upgradePreviewBtn.textContent = "▶";
_upgradePreviewBtn.classList.remove("playing");
_upgradePreviewBtn = null;
}
}
function _makePreviewBtn(absPath) {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "upgrade-preview-btn";
btn.textContent = "▶";
btn.title = "Anteprima audio";
btn.addEventListener("click", async (e) => {
e.preventDefault();
e.stopPropagation();
// Se già in play su questo bottone → stop
if (_upgradePreviewBtn === btn) {
_stopUpgradePreview();
return;
}
_stopUpgradePreview();
btn.textContent = "⏳";
btn.disabled = true;
let res;
try {
res = await window.pywebview.api.read_audio_data_url(absPath);
} catch (err) {
btn.textContent = "▶";
btn.disabled = false;
toast && toast("Errore lettura file: " + err, "error");
return;
}
btn.disabled = false;
if (!res || !res.ok) {
btn.textContent = "▶";
toast && toast(res && res.error || "Impossibile leggere il file", "error");
return;
}
_upgradePreviewAudio = new Audio(res.data_url);
_upgradePreviewBtn = btn;
btn.textContent = "■";
btn.classList.add("playing");
_upgradePreviewAudio.onended = _stopUpgradePreview;
_upgradePreviewAudio.onerror = () => {
// Solo se il listener è ancora agganciato (stop manuale lo azzera prima)
const wasPlaying = !!_upgradePreviewAudio;
_stopUpgradePreview();
if (wasPlaying) toast && toast("Errore riproduzione audio", "error");
};
_upgradePreviewAudio.play().catch(() => _stopUpgradePreview());
});
return btn;
}
function openUpgradePicker(payload) {
const modal = $("#upgradePickerModal");
if (!modal) return;
const file = (payload && payload.file) || "";
const sourcePath = (payload && payload.source_path) || "";
const candidates = (payload && payload.candidates) || [];
state.upPicker = { file, sourcePath, candidates, selectedIdx: -1 };
// Header: filename + play button per il source
const header = $("#upgradePickerFilename");
header.innerHTML = "";
const label = document.createElement("span");
label.textContent = "Brano da upgradare: " + file + " ";
header.appendChild(label);
if (sourcePath) header.appendChild(_makePreviewBtn(sourcePath));
const list = $("#upgradePickerList");
list.innerHTML = "";
candidates.forEach((c, i) => {
const row = document.createElement("label");
row.className = "upgrade-picker-row";
row.dataset.idx = String(i);
const simPct = Math.round((c.similarity || 0) * 100);
const kbps = c.bitrate ? `${c.bitrate}k` : "—";
row.innerHTML = `
<input type="radio" name="upgradePickerRadio" value="${i}" />
<div class="upgrade-picker-info">
<div class="upgrade-picker-title">${_escapeHtml(_basename(c.path))}</div>
<div class="upgrade-picker-meta">${_escapeHtml(kbps)} · ${_escapeHtml(_fmtBytes(c.size))} · similarity ${simPct}%</div>
<div class="upgrade-picker-path">${_escapeHtml(c.path || "")}</div>
</div>
<span class="upgrade-picker-play"></span>
`;
const playSlot = row.querySelector(".upgrade-picker-play");
if (playSlot && c.path) playSlot.appendChild(_makePreviewBtn(c.path));
row.addEventListener("click", (e) => {
// Se il click viene dal bottone play, non selezionare la riga
if (e.target.closest(".upgrade-preview-btn")) return;
state.upPicker.selectedIdx = i;
$("#upgradePickerUseBtn").disabled = false;
$$("#upgradePickerList .upgrade-picker-row").forEach((r) =>
r.classList.toggle("selected", r === row)
);
const radio = row.querySelector("input[type=radio]");
if (radio) radio.checked = true;
});
list.appendChild(row);
});
$("#upgradePickerUseBtn").disabled = true;
modal.hidden = false;
}
function closeUpgradePicker() {
_stopUpgradePreview();
const modal = $("#upgradePickerModal");
if (modal) modal.hidden = true;
}
async function _sendUpgradeChoice(choice) {
try {
await window.pywebview.api.upgrade_resolve_candidates(choice);
} catch (_e) { /* backend può essere già terminato: ignora */ }
closeUpgradePicker();
}
$("#upgradePickerUseBtn")?.addEventListener("click", () => {
const idx = state.upPicker.selectedIdx;
if (idx < 0 || idx >= state.upPicker.candidates.length) return;
const path = state.upPicker.candidates[idx].path;
_sendUpgradeChoice({ action: "use_local", path });
});
$("#upgradePickerYoutubeBtn")?.addEventListener("click", () => {
_sendUpgradeChoice({ action: "use_youtube" });
});
$("#upgradePickerSkipBtn")?.addEventListener("click", () => {
_sendUpgradeChoice({ action: "skip" });
});
$("#upgradePickerCancelAllBtn")?.addEventListener("click", async () => {
// Skip questo file per sbloccare il worker, poi ferma l'intera coda
_sendUpgradeChoice({ action: "skip" });
try { await window.pywebview.api.stop_upgrade(); } catch {}
});
$("#upgradePickerCloseBtn")?.addEventListener("click", () => {
_sendUpgradeChoice({ action: "skip" });
});
$("#upgradePickerModal")?.addEventListener("click", (e) => {
// Click sul backdrop (non sulla card) → skip
if (e.target === e.currentTarget) _sendUpgradeChoice({ action: "skip" });
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
const modal = $("#upgradePickerModal");
if (modal && !modal.hidden) _sendUpgradeChoice({ action: "skip" });
}
});
// ============================================================
// RECORDER tab
// ============================================================
@@ -1183,6 +1415,61 @@ $("#quotaUpgradeBtn")?.addEventListener("click", async () => {
try { await window.pywebview.api.open_purchase_page(); } catch (_e) {}
});
// ============================================================
// Helper di ordinamento condivisi tra BeatportUI, SpotifyUI, YoutubeUI
// ============================================================
function _sortPairs(tracks, existing, col, dir) {
const pairs = tracks.map((t, i) => ({ t, e: existing[i] || false, origIdx: i }));
const dirMul = dir === "desc" ? -1 : 1;
pairs.sort((a, b) => {
let av = a.t[col], bv = b.t[col];
if (col === "position") {
// Per Beatport t.position esiste (rank ufficiale); per Spotify/YouTube
// torniamo l'ordine originale della risposta API.
av = (typeof av === "number") ? av : a.origIdx;
bv = (typeof bv === "number") ? bv : b.origIdx;
}
if (typeof av === "number" && typeof bv === "number") {
return (av - bv) * dirMul;
}
av = String(av == null ? "" : av).toLowerCase();
bv = String(bv == null ? "" : bv).toLowerCase();
// Empty strings sempre in fondo indipendentemente da direzione
if (av === "" && bv !== "") return 1;
if (bv === "" && av !== "") return -1;
if (av < bv) return -1 * dirMul;
if (av > bv) return 1 * dirMul;
return 0;
});
return {
tracks: pairs.map(p => p.t),
existing: pairs.map(p => p.e),
origIndexes: pairs.map(p => p.origIdx),
};
}
function _bindSortableHeaders(tableSelector, onSort) {
document.querySelectorAll(`${tableSelector} th.sortable`).forEach(th => {
th.style.cursor = "pointer";
th.style.userSelect = "none";
th.addEventListener("click", () => onSort(th.dataset.sort));
});
}
function _updateSortArrows(tableSelector, sortCol, sortDir) {
document.querySelectorAll(`${tableSelector} th.sortable`).forEach(th => {
const key = th.dataset.sort;
// Usa un <span class="sort-arrow"> per non toccare il testo principale
let arrow = th.querySelector(".sort-arrow");
if (!arrow) {
arrow = document.createElement("span");
arrow.className = "sort-arrow";
th.appendChild(arrow);
}
arrow.textContent = key === sortCol ? (sortDir === "asc" ? " ▲" : " ▼") : "";
});
}
// ============================================================
// BEATPORT tab — carica Top 100 per genere, seleziona tracce, scarica
// (il download riusa start_tracks_download, quindi passa dagli stessi
@@ -1195,6 +1482,8 @@ const BeatportUI = (function () {
let currentGenreName = "";
let currentGenreSlug = "";
let downloading = false;
let sortCol = null;
let sortDir = "asc";
function fmtDur(sec) {
const n = Number(sec) || 0;
@@ -1287,16 +1576,28 @@ const BeatportUI = (function () {
const tbody = table.querySelector("tbody");
tbody.innerHTML = "";
currentTracks.forEach((t, i) => {
let displayTracks = currentTracks;
let displayExisting = existing;
let origIndexes = currentTracks.map((_, i) => i);
if (sortCol) {
const s = _sortPairs(currentTracks, existing, sortCol, sortDir);
displayTracks = s.tracks;
displayExisting = s.existing;
origIndexes = s.origIndexes;
}
displayTracks.forEach((t, i) => {
const origIdx = origIndexes[i];
const alreadyDl = displayExisting[i];
const tr = document.createElement("tr");
if (existing[i]) tr.classList.add("already-downloaded");
if (alreadyDl) tr.classList.add("already-downloaded");
const tdCheck = document.createElement("td");
tdCheck.className = "col-check";
const cb = document.createElement("input");
cb.type = "checkbox";
cb.dataset.idx = String(i);
cb.checked = !existing[i];
cb.dataset.idx = String(origIdx);
cb.checked = !alreadyDl;
cb.addEventListener("change", updateSelectionCount);
tdCheck.appendChild(cb);
tr.appendChild(tdCheck);
@@ -1316,7 +1617,7 @@ const BeatportUI = (function () {
const tdPos = document.createElement("td");
tdPos.className = "col-pos";
tdPos.textContent = String(t.position || (i + 1));
tdPos.textContent = String(t.position || (origIdx + 1));
tr.appendChild(tdPos);
const tdArtist = document.createElement("td");
@@ -1332,20 +1633,36 @@ const BeatportUI = (function () {
tdDur.textContent = fmtDur(t.duration_sec);
tr.appendChild(tdDur);
const tdDate = document.createElement("td");
tdDate.className = "col-date";
tdDate.textContent = t.release_date || "";
tr.appendChild(tdDate);
const tdState = document.createElement("td");
tdState.className = "col-state";
if (existing[i]) tdState.textContent = "✓ già scaricato";
if (alreadyDl) tdState.textContent = "✓ già scaricato";
tr.appendChild(tdState);
tbody.appendChild(tr);
});
_updateSortArrows("#beatport-table", sortCol, sortDir);
table.hidden = false;
$("#beatport-toolbar").hidden = false;
updateOutputInfo();
updateSelectionCount();
}
function handleSort(col) {
if (sortCol === col) {
sortDir = sortDir === "asc" ? "desc" : "asc";
} else {
sortCol = col;
sortDir = "asc";
}
renderTable();
}
function updateSelectionCount() {
const boxes = $$("#beatport-table tbody input[type=checkbox]");
const total = boxes.length;
@@ -1462,6 +1779,7 @@ const BeatportUI = (function () {
$("#beatport-download-btn").addEventListener("click", startDownload);
$("#beatport-stop-btn").addEventListener("click", stopDownload);
$("#beatport-select-all").addEventListener("change", (e) => toggleAll(e.target.checked));
_bindSortableHeaders("#beatport-table", handleSort);
// Aggancia agli event listener esistenti del canale "download" senza rompere
// il comportamento della tab Scarica (wrapping additivo).
@@ -1492,7 +1810,7 @@ const BeatportUI = (function () {
const SpotifyUI = (() => {
// NOTE: local module state — named `mstate` to NOT shadow the outer
// module-level `state` (which holds `state.config`).
const mstate = { tracks: [], existing: [], downloading: false };
const mstate = { tracks: [], existing: [], downloading: false, sortCol: null, sortDir: "asc" };
function _escape(s) {
const d = document.createElement("div");
@@ -1530,31 +1848,55 @@ const SpotifyUI = (() => {
const tbody = $("#spotify-tbody");
if (!tbody) return;
tbody.innerHTML = "";
mstate.tracks.forEach((t, i) => {
const already = !!mstate.existing[i];
let displayTracks = mstate.tracks;
let displayExisting = mstate.existing;
let origIndexes = mstate.tracks.map((_, i) => i);
if (mstate.sortCol) {
const s = _sortPairs(mstate.tracks, mstate.existing, mstate.sortCol, mstate.sortDir);
displayTracks = s.tracks;
displayExisting = s.existing;
origIndexes = s.origIndexes;
}
displayTracks.forEach((t, i) => {
const origIdx = origIndexes[i];
const already = !!displayExisting[i];
const tr = document.createElement("tr");
tr.dataset.idx = String(i);
tr.dataset.idx = String(origIdx);
if (already) tr.classList.add("already-downloaded");
const coverImg = t.image_url
? `<img src="${_escape(t.image_url)}" alt="" loading="lazy" referrerpolicy="no-referrer" onerror="this.style.visibility='hidden'"/>`
: "";
tr.innerHTML = `
<td class="col-check"><input type="checkbox" data-idx="${i}" ${already ? "" : "checked"} /></td>
<td class="col-check"><input type="checkbox" data-idx="${origIdx}" ${already ? "" : "checked"} /></td>
<td class="col-cover">${coverImg}</td>
<td class="col-pos">${i + 1}</td>
<td class="col-pos">${origIdx + 1}</td>
<td>${_escape(t.artists)}</td>
<td>${_escape(t.name)}</td>
<td>${_escape(t.album)}</td>
<td class="col-dur">${_fmtDuration(t.duration_sec)}</td>
<td class="col-date">${_escape(t.release_date || "")}</td>
<td class="col-state">${already ? "✓ già scaricato" : ""}</td>
`;
const cb = tr.querySelector("input[type=checkbox]");
if (cb) cb.addEventListener("change", updateSelectionCount);
tbody.appendChild(tr);
});
_updateSortArrows("#spotify-table", mstate.sortCol, mstate.sortDir);
updateSelectionCount();
}
function handleSort(col) {
if (mstate.sortCol === col) {
mstate.sortDir = mstate.sortDir === "asc" ? "desc" : "asc";
} else {
mstate.sortCol = col;
mstate.sortDir = "asc";
}
renderTable();
}
function setStatus(text, kind) {
const el = $("#spotify-status");
if (!el) return;
@@ -1708,6 +2050,7 @@ const SpotifyUI = (() => {
});
$("#spotify-download-btn").addEventListener("click", startDownload);
$("#spotify-stop-btn").addEventListener("click", stopDownload);
_bindSortableHeaders("#spotify-table", handleSort);
// Wrap bridge handlers additivamente
const origLog = bridgeHandlers["log"];
@@ -1731,7 +2074,7 @@ const SpotifyUI = (() => {
// YouTube search
// =====================================================================
const YoutubeUI = (() => {
const mstate = { tracks: [], existing: [], downloading: false };
const mstate = { tracks: [], existing: [], downloading: false, sortCol: null, sortDir: "asc" };
function _escape(s) {
const d = document.createElement("div");
@@ -1768,30 +2111,54 @@ const YoutubeUI = (() => {
const tbody = $("#youtube-tbody");
if (!tbody) return;
tbody.innerHTML = "";
mstate.tracks.forEach((t, i) => {
const already = !!mstate.existing[i];
let displayTracks = mstate.tracks;
let displayExisting = mstate.existing;
let origIndexes = mstate.tracks.map((_, i) => i);
if (mstate.sortCol) {
const s = _sortPairs(mstate.tracks, mstate.existing, mstate.sortCol, mstate.sortDir);
displayTracks = s.tracks;
displayExisting = s.existing;
origIndexes = s.origIndexes;
}
displayTracks.forEach((t, i) => {
const origIdx = origIndexes[i];
const already = !!displayExisting[i];
const tr = document.createElement("tr");
tr.dataset.idx = String(i);
tr.dataset.idx = String(origIdx);
if (already) tr.classList.add("already-downloaded");
const coverImg = t.image_url
? `<img src="${_escape(t.image_url)}" alt="" loading="lazy" referrerpolicy="no-referrer" onerror="this.style.visibility='hidden'"/>`
: "";
tr.innerHTML = `
<td class="col-check"><input type="checkbox" data-idx="${i}" ${already ? "" : "checked"} /></td>
<td class="col-check"><input type="checkbox" data-idx="${origIdx}" ${already ? "" : "checked"} /></td>
<td class="col-cover">${coverImg}</td>
<td class="col-pos">${i + 1}</td>
<td class="col-pos">${origIdx + 1}</td>
<td>${_escape(t.title)}</td>
<td>${_escape(t.channel)}</td>
<td class="col-dur">${_fmtDuration(t.duration_sec)}</td>
<td class="col-date">${_escape(t.release_date || "")}</td>
<td class="col-state">${already ? "✓ già scaricato" : ""}</td>
`;
const cb = tr.querySelector("input[type=checkbox]");
if (cb) cb.addEventListener("change", updateSelectionCount);
tbody.appendChild(tr);
});
_updateSortArrows("#youtube-table", mstate.sortCol, mstate.sortDir);
updateSelectionCount();
}
function handleSort(col) {
if (mstate.sortCol === col) {
mstate.sortDir = mstate.sortDir === "asc" ? "desc" : "asc";
} else {
mstate.sortCol = col;
mstate.sortDir = "asc";
}
renderTable();
}
function setStatus(text, kind) {
const el = $("#youtube-status");
if (!el) return;
@@ -1934,6 +2301,7 @@ const YoutubeUI = (() => {
});
$("#youtube-download-btn").addEventListener("click", startDownload);
$("#youtube-stop-btn").addEventListener("click", stopDownload);
_bindSortableHeaders("#youtube-table", handleSort);
const origLog = bridgeHandlers["log"];
bridgeHandlers["log"] = (payload) => {
@@ -1952,6 +2320,321 @@ const YoutubeUI = (() => {
return { init };
})();
// ============================================================
// TRAXSOURCE tab — carica Top 100 per genere, seleziona tracce, scarica
// Parallelo a BeatportUI (stessi canali "download:progress" /
// "download:done" / log view="download"). Colonne: cover, #, artista,
// titolo (mix), label, stato. No Durata / Data (non nel markup Traxsource).
// ============================================================
const TraxsourceUI = (function () {
let genres = [];
let currentTracks = [];
let existing = [];
let currentGenreName = "";
let currentGenreSlug = "";
let downloading = false;
let sortCol = null;
let sortDir = "asc";
function safeGenreFolder(name) {
return String(name || "").replace(/[\/\\]/g, "_").trim();
}
function populateSelect() {
const sel = $("#traxsource-genre");
if (!sel) return;
sel.innerHTML = "";
for (const g of genres) {
const opt = document.createElement("option");
opt.value = g.slug;
opt.textContent = g.name;
sel.appendChild(opt);
}
}
function updateOutputInfo() {
const info = $("#traxsource-output-info");
if (!info) return;
const root = (state.config && state.config.output_dir) || "";
if (!root) {
info.textContent = "⚠ Imposta la cartella output in Impostazioni prima di scaricare";
info.className = "beatport-output-info warn";
return;
}
const folder = safeGenreFolder(currentGenreName);
info.textContent = folder
? `Destinazione: ${root}/Traxsource_${folder}`
: `Destinazione: ${root}/Traxsource`;
info.className = "beatport-output-info";
}
function setStatus(text, kind) {
const el = $("#traxsource-status");
if (!el) return;
el.textContent = text || "";
el.className = "beatport-status" + (kind ? " " + kind : "");
}
async function loadChart(forceRefresh) {
const sel = $("#traxsource-genre");
const slug = sel && sel.value;
if (!slug) return;
const genreName = (sel.selectedOptions[0] && sel.selectedOptions[0].textContent) || slug;
currentGenreSlug = slug;
currentGenreName = genreName;
setStatus(forceRefresh ? "Ricarico Top 100 (bypass cache)…" : "Caricamento Top 100…", "loading");
$("#traxsource-table").hidden = true;
$("#traxsource-toolbar").hidden = true;
$("#traxsource-table").querySelector("tbody").innerHTML = "";
let res;
try {
res = await window.pywebview.api.traxsource_fetch_chart(slug, !!forceRefresh);
} catch (e) {
setStatus("Errore: " + ((e && e.message) || e), "error");
return;
}
if (!res || !res.ok) {
const code = res && res.error;
let msg = (res && res.message) || "Errore sconosciuto";
if (code === "invalid_genre") msg = "Genere non valido";
else if (code === "unreachable") msg = "Traxsource non raggiungibile — riprova più tardi";
else if (code === "parse") msg = "Impossibile leggere i dati (schema pagina cambiato?)";
setStatus(msg, "error");
return;
}
currentTracks = res.tracks || [];
try {
existing = await window.pywebview.api.traxsource_check_existing(currentTracks, currentGenreName);
} catch (_e) {
existing = currentTracks.map(() => false);
}
setStatus(`${currentTracks.length} brani caricati`, "ok");
renderTable();
}
function renderTable() {
const table = $("#traxsource-table");
const tbody = table.querySelector("tbody");
tbody.innerHTML = "";
let displayTracks = currentTracks;
let displayExisting = existing;
let origIndexes = currentTracks.map((_, i) => i);
if (sortCol) {
const s = _sortPairs(currentTracks, existing, sortCol, sortDir);
displayTracks = s.tracks;
displayExisting = s.existing;
origIndexes = s.origIndexes;
}
displayTracks.forEach((t, i) => {
const origIdx = origIndexes[i];
const alreadyDl = displayExisting[i];
const tr = document.createElement("tr");
if (alreadyDl) tr.classList.add("already-downloaded");
const tdCheck = document.createElement("td");
tdCheck.className = "col-check";
const cb = document.createElement("input");
cb.type = "checkbox";
cb.dataset.idx = String(origIdx);
cb.checked = !alreadyDl;
cb.addEventListener("change", updateSelectionCount);
tdCheck.appendChild(cb);
tr.appendChild(tdCheck);
const tdCover = document.createElement("td");
tdCover.className = "col-cover";
if (t.image_url) {
const img = document.createElement("img");
img.src = t.image_url;
img.alt = "";
img.loading = "lazy";
img.referrerPolicy = "no-referrer";
img.onerror = () => { img.style.visibility = "hidden"; };
tdCover.appendChild(img);
}
tr.appendChild(tdCover);
const tdPos = document.createElement("td");
tdPos.className = "col-pos";
tdPos.textContent = String(t.position || (origIdx + 1));
tr.appendChild(tdPos);
const tdArtist = document.createElement("td");
tdArtist.textContent = t.artists || "";
tr.appendChild(tdArtist);
const tdTitle = document.createElement("td");
tdTitle.textContent = t.mix ? `${t.title} (${t.mix})` : (t.title || "");
tr.appendChild(tdTitle);
const tdLabel = document.createElement("td");
tdLabel.textContent = t.label || "";
tr.appendChild(tdLabel);
const tdState = document.createElement("td");
tdState.className = "col-state";
if (alreadyDl) tdState.textContent = "✓ già scaricato";
tr.appendChild(tdState);
tbody.appendChild(tr);
});
_updateSortArrows("#traxsource-table", sortCol, sortDir);
table.hidden = false;
$("#traxsource-toolbar").hidden = false;
updateOutputInfo();
updateSelectionCount();
}
function handleSort(col) {
if (sortCol === col) {
sortDir = sortDir === "asc" ? "desc" : "asc";
} else {
sortCol = col;
sortDir = "asc";
}
renderTable();
}
function updateSelectionCount() {
const boxes = $$("#traxsource-table tbody input[type=checkbox]");
const total = boxes.length;
const checked = boxes.filter((b) => b.checked).length;
$("#traxsource-selected-count").textContent = `${checked}/${total} selezionati`;
const dlBtn = $("#traxsource-download-btn");
if (dlBtn) dlBtn.disabled = downloading || checked === 0;
const master = $("#traxsource-select-all");
if (master) {
if (total === 0) { master.checked = false; master.indeterminate = false; }
else if (checked === 0) { master.checked = false; master.indeterminate = false; }
else if (checked === total) { master.checked = true; master.indeterminate = false; }
else { master.checked = false; master.indeterminate = true; }
}
}
function toggleAll(checked) {
$$("#traxsource-table tbody input[type=checkbox]").forEach((b) => { b.checked = checked; });
updateSelectionCount();
}
function appendTraxsourceLog(msg) {
const el = $("#traxsource-log");
if (!el) return;
const cls = classifyLog(msg);
const line = document.createElement("div");
if (cls) line.className = cls;
line.textContent = msg;
el.appendChild(line);
el.scrollTop = el.scrollHeight;
}
async function startDownload() {
const boxes = $$("#traxsource-table tbody input[type=checkbox]");
const selected = [];
boxes.forEach((b) => {
if (b.checked) {
const idx = parseInt(b.dataset.idx, 10);
if (!isNaN(idx) && currentTracks[idx]) selected.push(currentTracks[idx]);
}
});
if (!selected.length) {
toast("Seleziona almeno un brano", "error");
return;
}
if (!state.config || !state.config.output_dir) {
toast("Imposta la cartella output in Impostazioni", "error");
return;
}
downloading = true;
$("#traxsource-download-btn").disabled = true;
$("#traxsource-stop-btn").hidden = false;
$("#traxsource-stop-btn").disabled = false;
$("#traxsource-log").innerHTML = "";
appendTraxsourceLog(`[INFO] Avvio download di ${selected.length} brani da ${currentGenreName}…`);
let res;
try {
res = await window.pywebview.api.traxsource_download_selected(selected, currentGenreName);
} catch (e) {
appendTraxsourceLog("[ERRORE] " + ((e && e.message) || e));
finishDownload();
return;
}
if (!res || !res.ok) {
const errMsg = (res && (res.error || res.message)) || "Impossibile avviare il download";
appendTraxsourceLog("[ERRORE] " + errMsg);
if (!handleGateBlock(res || {})) toast(errMsg, "error");
finishDownload();
}
}
async function stopDownload() {
try {
await window.pywebview.api.stop_download();
} catch (e) { console.error(e); }
}
function finishDownload() {
downloading = false;
$("#traxsource-stop-btn").hidden = true;
$("#traxsource-stop-btn").disabled = true;
updateSelectionCount();
if (currentTracks.length) {
window.pywebview.api.traxsource_check_existing(currentTracks, currentGenreName)
.then((res) => { existing = res || existing; renderTable(); })
.catch(() => {});
}
}
async function init() {
try {
genres = await window.pywebview.api.traxsource_genres();
} catch (e) {
console.error("Traxsource: impossibile caricare i generi", e);
genres = [];
}
populateSelect();
const last = (state.config && state.config.traxsource_last_genre) || "";
const sel = $("#traxsource-genre");
if (sel && last && genres.some((g) => g.slug === last)) {
sel.value = last;
}
updateOutputInfo();
$("#traxsource-load-btn").addEventListener("click", (e) => loadChart(e.shiftKey));
$("#traxsource-download-btn").addEventListener("click", startDownload);
$("#traxsource-stop-btn").addEventListener("click", stopDownload);
$("#traxsource-select-all").addEventListener("change", (e) => toggleAll(e.target.checked));
_bindSortableHeaders("#traxsource-table", handleSort);
// Wrapping additivo sui bridgeHandlers (canale "download" condiviso)
const origProgress = bridgeHandlers["download:progress"];
bridgeHandlers["download:progress"] = (p) => {
if (origProgress) origProgress(p);
};
const origDone = bridgeHandlers["download:done"];
bridgeHandlers["download:done"] = (p) => {
if (origDone) origDone(p);
if (downloading) finishDownload();
};
const origLog = bridgeHandlers["log"];
bridgeHandlers["log"] = ({ view, msg }) => {
if (origLog) origLog({ view, msg });
if (downloading && view === "download") appendTraxsourceLog(msg);
};
}
return { init };
})();
// =====================================================================
// Converti (WAV -> MP3)
// =====================================================================