73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Shikiryu\Bot\Commands;
|
|
|
|
use Shikiryu\Bot\Bot;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class Youtubedl implements Icommands
|
|
{
|
|
public static function getMessage(Bot $bot, array $data): void
|
|
{
|
|
$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');
|
|
}
|
|
|
|
if (!in_array($type, ['', 'audio', 'video', 'vidéo'], true)) {
|
|
$bot->replyPolitely('Je n\'ai pas compris ce que je devais faire');
|
|
}
|
|
|
|
$youtubedl = self::getYoutubeDl($bot);
|
|
|
|
if (in_array($type, ['', 'audio'], true)) {
|
|
$type = 'l\'audio';
|
|
$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]);
|
|
} else {
|
|
$type = 'la vidéo';
|
|
$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]);
|
|
}
|
|
$bot->replyPolitely(sprintf('Je %s %s', $action, $type));
|
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
|
$ex = new ProcessFailedException($process);
|
|
$bot->reply($ex->getMessage());
|
|
return;
|
|
}
|
|
|
|
$bot->reply($process->getOutput());
|
|
}
|
|
|
|
public static function getDescription(): string
|
|
{
|
|
return 'Youtubedl wrapper';
|
|
}
|
|
|
|
public static function getPattern(): string
|
|
{
|
|
return '(convertis|télécharges?) (https?:\/\/[^\s]+) en (audio|video|vidéo)';
|
|
}
|
|
|
|
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
|
|
{
|
|
return $bot->getConfig()['youtube-dl']['download_folder'] ?? __DIR__.'/../../';
|
|
}
|
|
}
|