16
16
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
17
17
* @link https://docs.madelineproto.xyz MadelineProto documentation
18
18
*/
19
+
20
+ use Amp \ByteStream \ReadableStream ;
21
+ use Amp \Process \Process ;
19
22
use danog \MadelineProto \API ;
20
23
use danog \MadelineProto \EventHandler \Attributes \Handler ;
21
24
use danog \MadelineProto \EventHandler \CommandType ;
36
39
use League \Uri \Contracts \UriException ;
37
40
use League \Uri \Uri ;
38
41
42
+ use function Amp \async ;
43
+ use function Amp \ByteStream \buffer ;
44
+ use function Amp \ByteStream \getStderr ;
45
+ use function Amp \ByteStream \pipe ;
46
+
39
47
// MadelineProto is already loaded
40
48
if (class_exists (API ::class)) {
41
49
// 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
110
118
}
111
119
112
120
/**
113
- * Upload an url.
121
+ * Upload a url or a youtube video .
114
122
*/
115
123
#[FilterCommand('upload ' , [CommandType::SLASH ])]
116
- public function cmdUrl (PrivateMessage &Incoming &IsNotEdited $ message ): void
124
+ public function cmdYt (PrivateMessage &Incoming &IsNotEdited $ message ): void
117
125
{
118
126
$ 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 ;
126
157
}
158
+
159
+ async (
160
+ $ this ->cmdUpload (...),
161
+ $ url ,
162
+ $ name ,
163
+ $ message
164
+ )->finally ($ finally );
127
165
}
128
166
129
167
/**
@@ -140,7 +178,7 @@ public function cmdNameFile(PrivateMessage&Incoming&IsNotEdited $message): void
140
178
}
141
179
}
142
180
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
144
182
{
145
183
try {
146
184
$ sent = $ message ->reply ('Preparing... ' );
0 commit comments