2016-06-20 22:17:56 +02:00
local youtube_dl = { }
function youtube_dl : init ( config )
youtube_dl.triggers = {
2016-08-11 02:34:37 +02:00
" ^/(mp4) https?://w?w?w?%.?youtu.be/([A-Za-z0-9-_-]+) " ,
" ^/(mp4) https?://w?w?w?%.?youtube.com/embed/([A-Za-z0-9-_-]+) " ,
" ^/(mp4) https?://w?w?w?%.?youtube.com/watch%?v=([A-Za-z0-9-_-]+) " ,
" ^/(mp3) https?://w?w?w?%.?youtu.be/([A-Za-z0-9-_-]+) " ,
" ^/(mp3) https?://w?w?w?%.?youtube.com/embed/([A-Za-z0-9-_-]+) " ,
" ^/(mp3) https?://w?w?w?%.?youtube.com/watch%?v=([A-Za-z0-9-_-]+) "
2016-06-20 22:17:56 +02:00
}
youtube_dl.doc = [ [ *
2016-08-11 02:34:37 +02:00
] ] .. config.cmd_pat .. [ [ mp3 * _ < URL > _ : Lädt Audio von YouTube
* ] ] .. config.cmd_pat .. [ [ mp4 * _ < URL > _ : Lädt Video von YouTube
2016-06-20 22:17:56 +02:00
] ]
end
youtube_dl.command = ' mp3 <URL>, /mp4 <URL> '
2016-08-11 02:34:37 +02:00
function youtube_dl : convert_video ( id )
local ytdl_json = io.popen ( ' youtube-dl -f 22/43/18/36/17 --max-filesize 49m -j https://www.youtube.com/watch/?v= ' .. id ) : read ( ' *all ' )
if not ytdl_json then return end
local data = json.decode ( ytdl_json )
return data
2016-06-20 22:17:56 +02:00
end
2016-08-11 02:34:37 +02:00
function youtube_dl : convert_audio ( id )
local output = io.popen ( ' youtube-dl --max-filesize 49m -o "/tmp/%(title)s.%(ext)s" --extract-audio --audio-format mp3 https://www.youtube.com/watch/?v= ' .. id ) : read ( ' *all ' )
2016-06-27 21:24:09 +02:00
if string.match ( output , ' .* File is larger .* ' ) then
2016-06-20 22:17:56 +02:00
return ' TOOBIG '
end
local audio = string.match ( output , ' %[ffmpeg%] Destination: /tmp/(.*).mp3 ' )
return ' /tmp/ ' .. audio .. ' .mp3 '
end
2016-08-11 02:34:37 +02:00
function youtube_dl : action ( msg , config , matches )
local id = matches [ 2 ]
2016-06-20 22:17:56 +02:00
if matches [ 1 ] == ' mp4 ' then
2016-08-11 02:34:37 +02:00
local first_msg = utilities.send_reply ( self , msg , ' <b>Video wird heruntergeladen...</b> ' , ' HTML ' )
2016-06-20 22:17:56 +02:00
utilities.send_typing ( self , msg.chat . id , ' upload_video ' )
2016-08-11 02:34:37 +02:00
local data = youtube_dl : convert_video ( id )
if not data then
utilities.edit_message ( self , msg.chat . id , first_msg.result . message_id , config.errors . results )
2016-06-20 22:17:56 +02:00
return
end
2016-08-11 02:34:37 +02:00
local ext = data.ext
local resolution = data.resolution
local url = data.url
local headers = get_http_header ( url ) -- need to get full url, because first url is actually a 302
local full_url = headers.location
if not full_url then
utilities.edit_message ( self , msg.chat . id , first_msg.result . message_id , config.errors . connection )
return
end
local headers = get_http_header ( full_url ) -- YES TWO FCKING HEAD REQUESTS
if tonumber ( headers [ " content-length " ] ) > 52420000 then
utilities.edit_message ( self , msg.chat . id , first_msg.result . message_id , ' <b>Das Video überschreitet die Grenze von 50 MB!</b> \n <a href=" ' .. full_url .. ' ">Direktlink zum Video</a> ( ' .. resolution .. ' ) ' , nil , ' HTML ' )
return
end
local file = download_to_file ( full_url , id .. ' . ' .. ext )
local width = data.width
local height = data.width
local duration = data.duration
utilities.edit_message ( self , msg.chat . id , first_msg.result . message_id , ' <a href=" ' .. full_url .. ' ">Direktlink zum Video</a> ( ' .. resolution .. ' ) ' , nil , ' HTML ' )
utilities.send_video ( self , msg.chat . id , file , nil , msg.message_id , duration , width , height )
2016-06-20 22:17:56 +02:00
return
end
if matches [ 1 ] == ' mp3 ' then
2016-08-11 02:34:37 +02:00
local first_msg = utilities.send_reply ( self , msg , ' <b>Audio wird heruntergeladen...</b> ' , ' HTML ' )
2016-06-20 22:17:56 +02:00
utilities.send_typing ( self , msg.chat . id , ' upload_audio ' )
2016-08-11 02:34:37 +02:00
local file = youtube_dl : convert_audio ( id )
2016-06-20 22:17:56 +02:00
if file == ' TOOBIG ' then
2016-08-11 02:34:37 +02:00
utilities.edit_message ( self , msg.chat . id , first_msg.result . message_id , ' <b>Die MP3 überschreitet die Grenze von 50 MB!</b> ' , nil , ' HTML ' )
2016-06-20 22:17:56 +02:00
return
end
utilities.send_audio ( self , msg.chat . id , file , msg.message_id )
return
end
end
return youtube_dl