DE
apps/winzik / src / Media / LyricsSearch.php · commit 6576d6f
<?phpnamespace App\Media;use App\Core\LyricsDownload;use App\Core\Market;use App\Model\Lyrics;use App\Model\Text;use JumPHP\Async\Task;use JumPHP\Http\Http;use JumPHP\Http\HttpResponse;/** * A search of the lyrics of a song on the Internet, every source at once, never blocking: the candidates come in as * the sources answer (`candidates`, best first), `sources` says where each one is, `task` settles with the whole list * once every source answered. * * Sources: LRCLIB (its search: synchronised lyrics, else the plain text), the marketplace of WinZik (.LYR shared by the * users), the old site of 2001 (winster.free.fr through the Internet Archive), and Claude Code when it is on the * computer (`claude -p` with WebSearch / WebFetch only, no MCP server: it tells WHERE the lyrics are, LRCLIB ids or * addresses of .lrc / .txt files, WinZik takes them there: an answer that comes from pages of the web is never run, and * never names this computer or its network: Transcriber::publicUrl()). * * A candidate (array): id (unique in the search), source (lrclib | market | archive | claude), sourceName (French), * artist, title, album, duration (s, 0 = unknown), synced (bool), lines (int: lines with words), preview (the first * lines, " / " between them), text (the words, a line per row), lyrics (App\Model\Lyrics: what importLyrics() saves). */final class LyricsSearch{    public const SOURCES = ['lrclib' => 'LRCLIB', 'market' => 'Marketplace WinZik', 'archive' => 'Ancien site WinZik (2001)', 'claude' => 'Claude (Internet)'];    private const HEADERS = ['Accept' => 'application/json', 'User-Agent' => 'WinZik (https://jumphp.net/winzik)'];    private const MAX_TEXT = 65536;    /** @var list<array<string, mixed>> best first */    public array $candidates = [];    /** @var array<string, string> source => searching | found | none | error | off */    public array $sources = [];    /** settles with $candidates when every source answered */    public Task $task;    public bool $cancelled = false;    private ?Process $job = null;    /** @var array<string, bool> */    private array $seen = [];    private int $next = 1;    /**     * @param (\Closure(list<string>): Process)|null $runner how `claude -p` is started (tests: a fake)     * @param ?string $claude Claude Code's program, null = not used     */    public function __construct(private Http $http, public string $artist, public string $title, public float $duration = 0.0,        private ?Market $market = null, private ?\Closure $runner = null, private ?string $claude = null, private string $logDir = '')    {        $this->task = new Task();        $this->artist = trim($artist);        $this->title = trim($title);    }    /** Asks every source (once). */    public function start(): self    {        $this->sources = array_fill_keys(array_keys(self::SOURCES), 'off');        if ($this->title === '' && $this->artist === '') {            $this->task->settle([]);            return $this;        }        $this->sources['lrclib'] = 'searching';        if ($this->market?->enabled()) {            $this->sources['market'] = 'searching';        }        if ($this->artist !== '' && $this->title !== '') {            $this->sources['archive'] = 'searching';        }        if ($this->claude !== null) {            $this->sources['claude'] = 'searching';        }        $this->lrclib();        if ($this->sources['market'] === 'searching') {            $this->searchMarket();        }        if ($this->sources['archive'] === 'searching') {            $this->archive(LyricsDownload::candidates($this->artist, $this->title));        }        if ($this->sources['claude'] === 'searching') {            $this->askClaude();        }        $this->checkDone();        return $this;    }    public function done(): bool    {        return $this->task->isDone();    }    /** One line for the screens ("Recherche... 3 résultats"). */    public function describe(): string    {        $n = count($this->candidates);        $found = $n === 0 ? 'aucunes paroles' : ($n === 1 ? '1 résultat' : "$n résultats");        if ($this->done()) {            return $n === 0 ? 'Aucunes paroles trouvées pour « ' . trim($this->artist . ' - ' . $this->title, ' -') . ' »' : 'Recherche terminée : ' . $found;        }        $waiting = array_keys(array_filter($this->sources, fn (string $s) => $s === 'searching'));        return 'Recherche (' . implode(', ', array_map(fn (string $s) => self::SOURCES[$s], $waiting)) . ')... ' . $found;    }    /** Stops: what came stays, nothing more comes (Claude ended). */    public function cancel(): void    {        $this->cancelled = true;        $this->stop();        foreach ($this->sources as $k => $s) {            if ($s === 'searching') {                $this->sources[$k] = 'none';            }        }        $this->task->settle($this->candidates);    }    /** Quitting: Claude ended now. */    public function stop(): void    {        if ($this->job !== null) {            $this->job->stop();            $this->job->cleanup();            $this->job = null;        }    }    /** Each frame: Claude's answer. */    public function poll(): void    {        if ($this->job === null) {            return;        }        $this->job->poll();        if (!$this->job->done()) {            return;        }        $job = $this->job;        $this->job = null;        $ok = $job->ok();        $out = $job->output();        $job->cleanup();        $sources = $ok ? array_slice(Transcriber::sources($out), 0, 4) : [];        if ($sources === []) {            $this->finish('claude', $ok ? 'none' : 'error');            return;        }        $this->claudeSources($sources, false);    }    // --- the sources ------------------------------------------------------------------------------------------------    private function lrclib(): void    {        $query = $this->artist !== '' && $this->title !== '' ? ['track_name' => $this->title, 'artist_name' => $this->artist] : ['q' => trim("$this->artist $this->title")];        $ask = function (array $query, bool $again): void {            $this->http->request('GET', Lrclib::URL . '/search?' . http_build_query($query), null, self::HEADERS, 20)->then(function (HttpResponse $r) use ($again): void {                if ($this->cancelled) {                    return;                }                $hits = $r->ok() && is_array($r->json()) ? array_slice((array)$r->json(), 0, 8) : [];                $added = 0;                foreach ($hits as $hit) {                    $added += is_array($hit) && $this->add(self::fromLrclib($hit, 'lrclib')) ? 1 : 0;                }                if ($added === 0 && $again) {                    // "artiste - titre" spelt otherwise: the free search                    $this->lrclibFree();                    return;                }                $this->finish('lrclib', $added > 0 ? 'found' : ($r->ok() ? 'none' : 'error'));            }, fn () => $this->finish('lrclib', 'error'));        };        $ask($query, isset($query['track_name']));    }    private function lrclibFree(): void    {        $this->http->request('GET', Lrclib::URL . '/search?' . http_build_query(['q' => trim("$this->artist $this->title")]), null, self::HEADERS, 20)            ->then(function (HttpResponse $r): void {                $added = 0;                foreach ($r->ok() && is_array($r->json()) ? array_slice((array)$r->json(), 0, 8) : [] as $hit) {                    $added += is_array($hit) && $this->add(self::fromLrclib($hit, 'lrclib')) ? 1 : 0;                }                $this->finish('lrclib', $added > 0 ? 'found' : 'none');            }, fn () => $this->finish('lrclib', 'error'));    }    /**     * A hit of LRCLIB (search or get) as a candidate: its synchronised lyrics, else its plain text; null for an     * instrumental or an empty one.     * @param array<string, mixed> $hit     * @return array<string, mixed>|null     */    public static function fromLrclib(array $hit, string $source): ?array    {        if (!empty($hit['instrumental'])) {            return null;        }        $lyrics = Lrclib::fromLrc((string)($hit['syncedLyrics'] ?? ''));        if ($lyrics === null) {            $plain = (string)($hit['plainLyrics'] ?? '');            $lyrics = trim($plain) === '' ? null : Lyrics::fromText($plain);            if ($lyrics !== null && $lyrics->synced) {                return null;        // a "plain" text with times: not what it says it is            }        }        if ($lyrics === null) {            return null;        }        return self::candidate($source, $lyrics, (string)($hit['artistName'] ?? ''), (string)($hit['trackName'] ?? $hit['name'] ?? ''),            (string)($hit['albumName'] ?? ''), (float)($hit['duration'] ?? 0));    }    private function searchMarket(): void    {        $market = $this->market;        if ($market === null) {            return;        }        $market->list('lyrics', trim("$this->artist $this->title"))->then(function (?array $items) use ($market): void {            $items = array_slice(array_values(array_filter((array)$items, 'is_array')), 0, 4);            if ($this->cancelled || $items === []) {                $this->finish('market', 'none');                return;            }            $left = count($items);            $one = function () use (&$left): void {                if (--$left === 0) {                    $found = array_filter($this->candidates, fn (array $c) => $c['source'] === 'market') !== [];                    $this->finish('market', $found ? 'found' : 'none');                }            };            foreach ($items as $item) {                $market->download('lyrics', (string)($item['id'] ?? ''))->then(function (?string $bytes) use ($item, $one): void {                    $lyrics = $bytes !== null && strlen($bytes) <= self::MAX_TEXT ? Lyrics::parse($bytes) : null;                    if (!$this->cancelled && $lyrics !== null && !$lyrics->isEmpty()) {                        $this->add(self::candidate('market', $lyrics, (string)($item['artist'] ?? ''),                            (string)(($item['title'] ?? '') ?: ($item['name'] ?? '')), '', 0.0));                    }                    $one();                }, fn () => $one());            }        }, fn () => $this->finish('market', 'error'));    }    /** @param list<string> $urls the two addresses of 2001 */    private function archive(array $urls): void    {        $url = array_shift($urls);        if ($url === null || $this->cancelled) {            $this->finish('archive', 'none');            return;        }        $this->http->request('GET', $url, null, ['User-Agent' => 'WinZik'], 20)->then(function (HttpResponse $r) use ($urls): void {            $lyrics = $r->ok() && !str_starts_with(ltrim($r->body), '<') && strlen($r->body) <= self::MAX_TEXT ? Lyrics::parse($r->body) : null;            if ($lyrics === null || $lyrics->isEmpty() || $this->cancelled) {                $this->archive($urls);                return;            }            $this->add(self::candidate('archive', $lyrics, $this->artist, $this->title, '', 0.0));            $this->finish('archive', 'found');        }, fn () => $this->archive($urls));    }    private function askClaude(): void    {        $who = trim($this->artist . ' - ' . $this->title, ' -');        $length = $this->duration > 0 ? sprintf(' (durée %d:%02d)', intdiv((int)$this->duration, 60), (int)$this->duration % 60) : '';        $prompt = "Trouve sur Internet où sont publiées les paroles de la chanson « $who »$length : synchronisées (format LRC, avec les temps) "            . "de préférence, sinon le texte seul. Cherche d'abord sur lrclib.net (son API publique : https://lrclib.net/api/search?q=... ; essaie "            . "plusieurs orthographes de l'artiste et du titre), puis ailleurs (fichiers .lrc ou .txt). Ne recopie pas les paroles : réponds "            . "UNIQUEMENT avec où les prendre, une source par ligne, la meilleure d'abord, au format LRCLIB:<identifiant numérique> ou "            . "URL:<adresse https directe d'un fichier texte .lrc ou .txt>. Si tu ne trouves rien, réponds seulement : INTROUVABLE";        $command = [(string)$this->claude, '-p', $prompt, '--tools', 'WebSearch,WebFetch', '--allowedTools', 'WebSearch,WebFetch', '--strict-mcp-config'];        try {            $this->job = $this->runner !== null ? ($this->runner)($command) : Process::start($command, $this->logDir !== '' ? $this->logDir : sys_get_temp_dir() . '/winzik-logs');        } catch (\Throwable) {            $this->finish('claude', 'error');        }    }    /** @param list<string> $sources "lrclib:123", "url:https://..." */    private function claudeSources(array $sources, bool $found): void    {        $source = array_shift($sources);        if ($source === null || $this->cancelled) {            $this->finish('claude', $found ? 'found' : 'none');            return;        }        $lrclib = str_starts_with($source, 'lrclib:');        $url = $lrclib ? Lrclib::URL . '/get/' . substr($source, 7) : substr($source, 4);        if (!$lrclib && !Transcriber::publicUrl($url)) {            $this->claudeSources($sources, $found);            return;        }        $this->http->request('GET', $url, null, $lrclib ? self::HEADERS : ['User-Agent' => 'WinZik'], 20)->then(function (HttpResponse $r) use ($sources, $found, $lrclib): void {            $candidate = null;            if ($r->ok() && strlen($r->body) <= self::MAX_TEXT) {                if ($lrclib) {                    $data = $r->json();                    $candidate = is_array($data) ? self::fromLrclib($data, 'claude') : null;                } elseif (!str_starts_with(ltrim($r->body), '<') && !str_contains(strtolower((string)$r->header('content-type', '')), 'html')) {                    $lyrics = Lyrics::fromText($r->body);                    $candidate = $lyrics !== null ? self::candidate('claude', $lyrics, $this->artist, $this->title, '', 0.0) : null;                }            }            $this->claudeSources($sources, ($candidate !== null && $this->add($candidate)) || $found);        }, fn () => $this->claudeSources($sources, $found));    }    // --- the list ---------------------------------------------------------------------------------------------------    /** @return array<string, mixed> */    public static function candidate(string $source, Lyrics $lyrics, string $artist, string $title, string $album, float $duration): array    {        $texts = array_values(array_filter($lyrics->texts(), fn (string $t) => trim($t) !== ''));        return [            'id' => '', 'source' => $source, 'sourceName' => self::SOURCES[$source] ?? $source,            'artist' => trim($artist), 'title' => trim($title), 'album' => trim($album), 'duration' => $duration,            'synced' => $lyrics->synced, 'lines' => count($texts),            'preview' => mb_substr(implode(' / ', array_slice($texts, 0, 3)), 0, 160),            'text' => $lyrics->plainText(), 'lyrics' => $lyrics,        ];    }    /** @param array<string, mixed>|null $c true when it was new */    private function add(?array $c): bool    {        if ($c === null || ($c['lines'] ?? 0) === 0) {            return false;        }        $key = ($c['synced'] ? 's' : 'p') . mb_substr(preg_replace('/\W+/u', '', Text::fold((string)$c['text'])) ?? '', 0, 120);        if (isset($this->seen[$key])) {            return false;        }        $this->seen[$key] = true;        $c['id'] = $c['source'] . ':' . $this->next++;        $c['score'] = $this->score($c);        $this->candidates[] = $c;        // best first: the same song (title, artist, length), synchronised before plain; then in the order they came        usort($this->candidates, fn (array $a, array $b) => $b['score'] <=> $a['score'] ?: (int)substr(strrchr($a['id'], ':') ?: ':0', 1) <=> (int)substr(strrchr($b['id'], ':') ?: ':0', 1));        return true;    }    /** @param array<string, mixed> $c */    private function score(array $c): float    {        $same = fn (string $a, string $b): bool => $a !== '' && $b !== '' && (str_contains(Text::fold($a), Text::fold($b)) || str_contains(Text::fold($b), Text::fold($a)));        $score = ($c['synced'] ? 2.0 : 0.0) + ($same((string)$c['title'], $this->title) ? 3.0 : 0.0) + ($same((string)$c['artist'], $this->artist) ? 2.0 : 0.0);        if ($this->duration > 0 && $c['duration'] > 0) {            $score += abs($this->duration - $c['duration']) <= 3 ? 1.0 : (abs($this->duration - $c['duration']) > 20 ? -1.0 : 0.0);        }        return $score;    }    private function finish(string $source, string $state): void    {        if (($this->sources[$source] ?? '') === 'searching') {            $this->sources[$source] = $state;        }        $this->checkDone();    }    private function checkDone(): void    {        if (!in_array('searching', $this->sources, true)) {            $this->task->settle($this->candidates);        }    }}