alfred-nextcloud/bot/Commands/Youtubedl.php

74 lines
2.8 KiB
PHP
Raw Normal View History

2024-08-05 23:33:27 +02:00
<?php
namespace Shikiryu\Bot\Commands;
use Shikiryu\Bot\Bot;
2024-08-06 17:23:06 +02:00
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
2024-08-05 23:33:27 +02:00
class Youtubedl implements Icommands
{
2024-08-06 16:06:07 +02:00
public static function getMessage(Bot $bot, array $data): void
2024-08-05 23:33:27 +02:00
{
$action = strtolower($data[1]); // ex: tΓ©lΓ©charge
$url = filter_var($data[2], FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); // ex: https://www.youtube.com/watch?v=3BXDsVD6O10
$type = $data[3]; // ex: audio
if (empty($url)) {
$bot->replyPolitely('L\'url n\'est pas conforme');
}
2024-08-06 17:05:10 +02:00
if (!in_array($type, ['', 'audio', 'video', 'vidΓ©o'], true)) {
2024-08-05 23:33:27 +02:00
$bot->replyPolitely('Je n\'ai pas compris ce que je devais faire');
}
2024-08-06 16:06:07 +02:00
$youtubedl = self::getYoutubeDl($bot);
2024-08-05 23:33:27 +02:00
if (in_array($type, ['', 'audio'], true)) {
$type = 'l\'audio';
2024-08-06 17:23:06 +02:00
$process = new Process([$youtubedl, '--extract-audio', '--audio-format', 'mp3', sprintf('-o %s%s', static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s'), $url]);
// $cmd = sprintf('%s --extract-audio --audio-format mp3 -o %s%s %s', $youtubedl, static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s', $url);
// exec($cmd, $output);
// $bot->sendChatMessage('', sprintf('Je lance %s', $cmd));
2024-08-05 23:33:27 +02:00
} else {
$type = 'la vidΓ©o';
2024-08-06 17:23:06 +02:00
$process = new Process([$youtubedl, '-f bestvideo+bestaudio/best', sprintf('-o %s%s', static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s'), $url]);
// $cmd = sprintf('%s -f bestvideo+bestaudio/best -o %s%s %s', $youtubedl, static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s', $url);
// exec($cmd, $output);
// $bot->sendChatMessage('', sprintf('Je lance %s', $cmd));
2024-08-05 23:33:27 +02:00
}
2024-08-06 17:23:06 +02:00
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
$ex = new ProcessFailedException($process);
$bot->reply($ex->getMessage());
}
$bot->reply($process->getOutput());
// $bot->replyPolitely(sprintf('Je %s %s', $action, $type));
2024-08-05 23:33:27 +02:00
}
2024-08-06 16:06:07 +02:00
public static function getDescription(): string
2024-08-05 23:33:27 +02:00
{
return 'Youtubedl wrapper';
}
2024-08-06 16:06:07 +02:00
private static function getYoutubeDl(Bot $bot): string
{
$youtubedl_bin = $bot->getConfig()['youtube-dl']['bin'];
if (empty($youtubedl_bin)) {
exec('which youtube-dl', $output);
$youtubedl_bin = current($output);
}
return $youtubedl_bin;
}
private static function getDownloadFolder(Bot $bot): string
{
2024-08-06 17:23:06 +02:00
return $bot->getConfig()['youtube-dl']['download_folder'] ?? __DIR__.'/../../';
2024-08-06 16:06:07 +02:00
}
2024-08-05 23:33:27 +02:00
}