Skip to content

Commit be02cbb

Browse files
committed
Add support for yt-dlp in downloadRenameBot.php
1 parent fc655e1 commit be02cbb

File tree

6 files changed

+54
-65291
lines changed

6 files changed

+54
-65291
lines changed

‎.php-cs-fixer.dist.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function getRules(): array
2323
->in(__DIR__ . '/src')
2424
->in(__DIR__ . '/tests')
2525
->in(__DIR__ . '/examples')
26-
->in(__DIR__ . '/tools');
26+
->in(__DIR__ . '/tools')
27+
->exclude('TLParser.php');
2728

2829
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
2930

‎examples/downloadRenameBot.php

+48-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
1717
* @link https://docs.madelineproto.xyz MadelineProto documentation
1818
*/
19+
20+
use Amp\ByteStream\ReadableStream;
21+
use Amp\Process\Process;
1922
use danog\MadelineProto\API;
2023
use danog\MadelineProto\EventHandler\Attributes\Handler;
2124
use danog\MadelineProto\EventHandler\CommandType;
@@ -36,6 +39,11 @@
3639
use League\Uri\Contracts\UriException;
3740
use League\Uri\Uri;
3841

42+
use function Amp\async;
43+
use function Amp\ByteStream\buffer;
44+
use function Amp\ByteStream\getStderr;
45+
use function Amp\ByteStream\pipe;
46+
3947
// MadelineProto is already loaded
4048
if (class_exists(API::class)) {
4149
// Otherwise, if a stable version of MadelineProto was installed via composer, load composer autoloader
@@ -110,20 +118,50 @@ public function cmdSaveState(PrivateMessage&Incoming&HasMedia&IsNotEdited $messa
110118
}
111119

112120
/**
113-
* Upload an url.
121+
* Upload a url or a youtube video.
114122
*/
115123
#[FilterCommand('upload', [CommandType::SLASH])]
116-
public function cmdUrl(PrivateMessage&Incoming&IsNotEdited $message): void
124+
public function cmdYt(PrivateMessage&Incoming&IsNotEdited $message): void
117125
{
118126
$url = $message->commandArgs[0];
119-
$name = $message->commandArgs[1] ?? basename($url);
120-
try {
121-
$url = Uri::new($message->commandArgs[0]);
122-
$url->getScheme() || $url = "http://$url";
123-
$this->cmdUpload(new RemoteUrl("$url"), $name, $message);
124-
} catch (UriException $e) {
125-
$message->reply('Error: ' . $e->getMessage());
127+
$name = $message->commandArgs[1] ?? null;
128+
129+
$process = Process::start([
130+
'yt-dlp',
131+
$url,
132+
'-J',
133+
]);
134+
$info = json_decode(
135+
buffer($process->getStdout()),
136+
true,
137+
);
138+
$process->join();
139+
if (isset($info['title'])) {
140+
$name ??= $info['title'].".mp4";
141+
$url = Process::start([
142+
'yt-dlp',
143+
$url,
144+
'-o',
145+
'-',
146+
]);
147+
async(pipe(...), $url->getStderr(), getStderr())->ignore();
148+
$finally = $url->join(...);
149+
$url = $url->getStdout();
150+
} else {
151+
$name ??= $url;
152+
if (Uri::new($url)->getScheme() === null) {
153+
$url = "http://$url";
154+
}
155+
$url = new RemoteUrl($url);
156+
$finally = static fn () => null;
126157
}
158+
159+
async(
160+
$this->cmdUpload(...),
161+
$url,
162+
$name,
163+
$message
164+
)->finally($finally);
127165
}
128166

129167
/**
@@ -140,7 +178,7 @@ public function cmdNameFile(PrivateMessage&Incoming&IsNotEdited $message): void
140178
}
141179
}
142180

143-
private function cmdUpload(Media|RemoteUrl $file, string $name, PrivateMessage $message): void
181+
private function cmdUpload(Media|RemoteUrl|ReadableStream $file, string $name, PrivateMessage $message): void
144182
{
145183
try {
146184
$sent = $message->reply('Preparing...');

‎src/MTProtoTools/FilesLogic.php

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ public function upload($file, string $fileName = '', ?callable $cb = null, bool
316316
}
317317
if ($file instanceof RemoteUrl) {
318318
$file = $file->url;
319+
return $this->uploadFromUrl($file, 0, $fileName, $cb, $encrypted, $cancellation);
319320
}
320321
if ($file instanceof BotApiFileId) {
321322
$info = $this->getDownloadInfo($file->fileId);

0 commit comments

Comments
 (0)