Add better ytdl

This commit is contained in:
2024-08-06 16:06:07 +02:00
parent a63538c768
commit 1dfcb4a4d5
8 changed files with 895 additions and 63 deletions

View File

@@ -4,61 +4,40 @@ namespace Shikiryu\Bot;
class Bot
{
/**
* @var Request
*/
protected $request;
protected $events = [];
protected $config = [];
/**
* @var string[]
*/
protected static $masters = [];
protected Request $request;
protected array $events = [];
protected array $config = [];
protected static array $masters = [];
/**
* @return string
*/
protected function getMaster()
protected function getMaster(): string
{
$masters = array_merge(self::$masters, $this->getConfig()['masters']);
return $masters[array_rand($masters, 1)];
return $masters[array_rand($masters)];
}
/**
* Bot constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}
/**
* @param Request $request
* @return bool
*/
public function isValid(Request $request)
public function isValid(Request $request): bool
{
$this->request = $request;
return $this->config['token'] === $request->getToken();
}
/**
* @param string $pattern the pattern to listen for
* @param string $pattern the pattern to listen for
* @param \Closure|string $callback the callback to execute. Either a closure or a Class@method notation
*
* @return $this
*/
public function hears($pattern, $callback)
public function hears(string $pattern, \Closure|string $callback): static
{
if (!array_key_exists($pattern, $this->events)) {
$this->events[$pattern] = $callback;
} else {
error_log(sprintf('Event %s déjà en place', $pattern));
}
// $this->events[$pattern][] = $callback;
return $this;
}
@@ -66,33 +45,30 @@ class Bot
* Try to match messages with the ones we should
* listen to.
*/
public function listen($sentence)
public function listen($sentence): void
{
foreach ($this->events as $pattern => $command) {
$preg_pattern = sprintf('#%s#i', $pattern);
if (preg_match($preg_pattern, $sentence, $matches)) {
if (is_callable($command)) {
call_user_func($command, $this);
$command($this);
} else {
call_user_func([$command, 'getMessage'], $this, $matches);
$command->getMessage($this, $matches);
}
return;
}
}
$this->replyPolitely('Je n\'ai pas compris');
}
/**
* @param string $message
*/
public function replyPolitely($message)
public function replyPolitely(string $message): void
{
$message .= ', '.$this->getMaster().'.';
$this->reply($message);
}
public function sendChatMessage(string $referenceId, string $message): void {
public function sendChatMessage(string $referenceId, string $message): void {
$body = [
'message' => $message,
'referenceId' => $referenceId,
@@ -109,7 +85,7 @@ class Bot
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'nextcloud-talk-hackerone-adapter/1.0');
curl_setopt($ch, CURLOPT_USERAGENT, 'alfred/1.0');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'OCS-APIRequest: true',
@@ -117,8 +93,7 @@ class Bot
'X-Nextcloud-Talk-Bot-Random: ' . $random,
'X-Nextcloud-Talk-Bot-Signature: ' . $hash,
]);
$var = curl_exec($ch);
$error = curl_error($ch);
curl_exec($ch);
curl_close($ch);
}
@@ -126,13 +101,8 @@ class Bot
return str_replace(['@', 'http://', 'https://'], ['👤', '🔗', '🔗🔒'], $text);
}
/**
* @param string $message
*/
public function reply($message)
public function reply(string $message): void
{
// header('Content-type: application/json');
// die('{"text": "'.$message.'"}');
$this->sendChatMessage('', $message);
}

View File

@@ -6,8 +6,7 @@ use Shikiryu\Bot\Bot;
class Youtubedl implements Icommands
{
public static function getMessage(Bot $bot, array $data)
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
@@ -21,18 +20,37 @@ class Youtubedl implements Icommands
$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';
exec("youtube-dl --extract-audio --audio-format mp3 -o '/volume1/music/Podcast/Youtube/%(upload_date)s-%(uploader)s-%(title)s.%(ext)s' $url &> /dev/null &");
exec(sprintf('%s --extract-audio --audio-format mp3 -o %s%s %s &> /dev/null &', $youtubedl, static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s', $url));
} else {
$type = 'la vidéo';
exec("youtube-dl -f bestvideo+bestaudio/best -o '/volume1/music/Podcast/Youtube/%(upload_date)s-%(uploader)s-%(title)s.%(ext)s' $url &> /dev/null &");
exec(sprintf('%s -f bestvideo+bestaudio/best -o %s%s %s &> /dev/null &', $youtubedl, static::getDownloadFolder($bot), '%(upload_date)s-%(uploader)s-%(title)s.%(ext)s', $url));
}
$bot->replyPolitely(sprintf('Je %s %s', $action, $type));
}
public static function getDescription()
public static function getDescription(): string
{
return 'Youtubedl wrapper';
}
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']['folder'] ?? __DIR__.'/../../';
}
}

8
bot/Request.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace Shikiryu\Bot;
class Request
{
}