format->selected = ($this->wz->options['ytFormat'] ?? 'mp3') === 'mp4' ? 1 : 0; $this->source->items = array_values(YouTube::SOURCES); $this->source->selected = 0; $this->query->text = ''; $this->status->text = ''; $yt = $this->wz->youtube; if ($yt === null) { $this->status->text = "YouTube marche seulement dans WinZik installé sur l'ordinateur (Linux, Windows) : installez-le depuis jumphp.net/winzik."; foreach ([$this->source, $this->query, $this->find, $this->get, $this->prev, $this->next, $this->format, $this->remove, $this->clearAll, $this->clear] as $widget) { $widget->enabled = false; } $this->setFocus($this->quit); return; } $this->where->text = 'MP3 : ' . $yt->musicDir . ' MP4 : ' . $yt->videoDir; $this->where->visible = true; $this->source->selected = max(0, (int)array_search($yt->source, array_keys(YouTube::SOURCES), true)); $this->showResults(); $this->setFocus($this->query); } /** @return array{id: string, title: string, url: string}|null the line chosen, or the address typed */ private function chosen(): ?array { $typed = trim($this->query->text); if (preg_match('#^https?://#i', $typed) && !preg_match('#(spotify\.com|deezer\.com|deezer\.page\.link)#i', $typed)) { return ['id' => $typed, 'title' => $typed, 'url' => $typed]; // any address yt-dlp knows (YouTube, SoundCloud, Dailymotion...) } $yt = $this->wz->youtube; return $yt?->results[(int)$this->results->selected] ?? null; } public function onQuerySubmit(TextBox $sender): void { $this->onFindClick($this->find); } public function onSourceChange(ComboBox $sender, int $index, string $item): void { if ($this->wz->youtube !== null) { $this->wz->youtube->source = (string)(array_keys(YouTube::SOURCES)[$index] ?? 'youtube'); } } public function onPrevClick(Button $sender): void { $this->wz->youtube?->previousPage() && $this->shownKey = ''; } public function onNextClick(Button $sender): void { $this->wz->youtube?->nextPage() && $this->shownKey = ''; } public function onFindClick(Button $sender): void { $yt = $this->wz->youtube; if ($yt === null) { return; } $typed = trim($this->query->text); if (preg_match('#^https?://.*(spotify\.com|deezer\.com)#i', $typed)) { // Spotify / Deezer (DRM): the same song, taken from YouTube $this->status->text = 'Lecture du lien...'; $yt->resolveLink($typed)->then(function (mixed $songs) use ($yt): void { if (!is_array($songs) || $songs === []) { $this->status->text = "Impossible de savoir quels morceaux sont derrière ce lien : vérifiez qu'il est complet et public, puis réessayez."; return; } $format = (int)$this->format->selected === 1 ? 'mp4' : 'mp3'; // the songs already here: not again (no double) $yt->forgetFiles(); $playlist = array_map(fn (string $f) => $this->wz->path($f), $this->wz->playlist->items); $added = $here = 0; foreach ($songs as $s) { if ($yt->alreadyHave($s['title'], $s['url'], $playlist) !== null) { $here++; continue; } $yt->download(['id' => $s['url'], 'title' => $s['title'], 'url' => $s['url']], $format, $s['title']); $added++; } $this->wz->saveQueue(); $this->status->text = $added . ' morceau(x) ajouté(s) à la file (pris sur YouTube)' . ($here > 0 ? ' ; ' . $here . ' déjà là' : '') . '.'; }); return; } if ($typed === '') { $this->status->text = 'Tapez un artiste, un titre ou un lien, puis Entrée.'; $this->setFocus($this->query); return; } if (!$yt->ready()) { $this->status->text = 'yt-dlp arrive (téléchargement en cours) : réessayez dans un instant.'; return; } $yt->spotifyId = (string)($this->wz->options['spotifyId'] ?? ''); $yt->spotifySecret = (string)($this->wz->options['spotifySecret'] ?? ''); if ($yt->search($this->query->text, 1, (string)(array_keys(YouTube::SOURCES)[(int)$this->source->selected] ?? 'youtube'))) { $this->status->text = 'Recherche de « ' . trim($this->query->text) . ' »...'; $this->shownKey = ''; } } public function onResultsActivate(ListBox $sender, int $index, string $item): void { $this->onGetClick($this->get); } public function onGetClick(Button $sender): void { $yt = $this->wz->youtube; $entry = $this->chosen(); if ($yt === null || $entry === null) { $this->status->text = "Choisissez d'abord une vidéo dans la liste (ou collez son lien), puis Télécharger."; return; } $cannot = $yt->cannotDownload(); if ($cannot !== '') { $this->status->text = $cannot; return; } $format = (int)$this->format->selected === 1 ? 'mp4' : 'mp3'; $this->wz->options['ytFormat'] = $format; $this->wz->touch(); $yt->download($entry, $format); $this->status->text = '« ' . $entry['title'] . ' » (' . strtoupper($format) . ') ajouté à la file : choisissez-en d\'autres, ils suivront.'; $this->showJobs(); } private float $spin = 0.0; protected function tick(float $dt): void { parent::tick($dt); if ($this->wz->youtube === null) { return; } // Entrée: in the list of results it downloads the line chosen, elsewhere it searches $inList = $this->focused() === $this->results && (int)$this->results->selected >= 0; $this->get->props['default'] = $inList; $this->find->props['default'] = !$inList; // the loader of the search: a bar that runs while yt-dlp looks (and Chercher waits) $searching = $this->wz->youtube->searching !== ''; $this->busy->props['visible'] = $searching; $this->find->enabled = !$searching; $this->get->enabled = $this->chosen() !== null; if ($searching) { $this->spin = fmod($this->spin + $dt * 90, 100); $this->busy->value = round($this->spin); $dots = str_repeat('.', 1 + (int)(fmod($this->spin, 30) / 10)); $this->status->text = 'Recherche de « ' . $this->wz->youtube->searching . ' » ' . $dots; } $this->refresh += $dt; if ($this->refresh < 0.25) { return; } $this->refresh = 0; $this->showResults(); $this->showJobs(); } private function showResults(): void { $yt = $this->wz->youtube; if ($yt === null || $yt->searching !== '') { return; } if ($yt->error !== '') { $this->status->text = $yt->error; } elseif ($yt->results === [] && str_starts_with($this->status->text, 'Recherche')) { $this->status->text = 'Aucune vidéo trouvée : essayez d\'autres mots.'; } $this->pageText->text = $yt->query !== '' ? 'Page ' . $yt->page . ' - ' . YouTube::SOURCES[$yt->source] : ''; $this->prev->enabled = $yt->page > 1; $this->next->enabled = count($yt->results) >= YouTube::RESULTS; $key = $yt->source . '|' . $yt->page . '|' . count($yt->results); if ($this->shownKey === $key && $this->results->items !== []) { return; } $this->shownKey = $key; $this->results->items = array_map(fn (array $r) => sprintf('%s (%s%s)', $r['title'], YouTube::clock($r['duration']), $r['channel'] !== '' ? ' - ' . $r['channel'] : ''), $yt->results); $this->results->selected = $yt->results === [] ? -1 : 0; if ($yt->results !== [] && str_starts_with($this->status->text, 'Recherche')) { $this->status->text = count($yt->results) . ' résultat(s) : choisissez (flèches), puis Entrée, double clic ou Télécharger.'; } } private function showJobs(): void { $yt = $this->wz->youtube; $tools = $this->wz->tools; if ($yt === null || $tools === null) { return; } // one line per download: its bar, its state; then the whole queue $lines = []; foreach ($yt->jobs as $job) { $p = $job['state'] === 'done' ? 1.0 : ($job['state'] === 'convert' ? 1.0 : $job['progress']); $bar = str_repeat('█', (int)round($p * 12)) . str_repeat('░', 12 - (int)round($p * 12)); $lines[] = sprintf('%s %s %s [%s]', match ($job['state']) { 'queued' => '…', 'download', 'convert' => '▶', 'done' => '✔', default => '✖', }, $bar, match ($job['state']) { 'queued' => 'en attente', 'download' => (int)round($job['progress'] * 100) . ' %', 'convert' => 'conversion...', 'done' => 'ajouté', default => 'erreur : ' . $job['error'], }, strtoupper($job['format'])) . ' ' . $job['title']; } if ($this->queue->items !== $lines) { $this->queue->items = $lines; } [$finished, $all, $fraction] = $yt->total(); $this->total->value = round($fraction * 100); $this->totalText->text = $all === 0 ? '' : sprintf('%d / %d - %d %%', $finished, $all, (int)round($fraction * 100)); $ready = ($tools->ready('yt-dlp') || $yt->native()) && $tools->ready('ffmpeg'); $this->jobs->text = $ready ? '' : $tools->describe(); $this->jobs->visible = $this->jobs->text !== ''; $over = array_filter($yt->jobs, fn (array $j) => $j['state'] === 'done' || $j['state'] === 'error'); $this->remove->enabled = $this->queue->selected >= 0 && $this->queue->selected < count($yt->jobs); $this->clearAll->enabled = $yt->jobs !== []; $this->clear->enabled = $over !== []; } /** The line chosen in the queue removed (stopped if it was downloading). */ public function onRemoveClick(Button $sender): void { $i = $this->queue->selected; if ($i >= 0) { $this->wz->youtube?->remove($i); $this->wz->saveQueue(); $this->showJobs(); } } /** The whole queue emptied (the download going on stopped). */ public function onClearAllClick(Button $sender): void { $this->wz->youtube?->clearQueue(); $this->wz->saveQueue(); $this->showJobs(); } public function onClearClick(Button $sender): void { $this->wz->youtube?->clearFinished(); $this->showJobs(); } public function onQuitClick(Button $sender): void { $this->wz->save(); $this->close(); } }