From 6b469fdb37ed9f46f9c209b5f97b8cb316aad338 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 11 Aug 2016 02:34:37 +0200 Subject: [PATCH] =?UTF-8?q?-=20YouTube-DL:=20Downloadprozess=20f=C3=BCr=20?= =?UTF-8?q?MP4=20umgeschrieben=20+=20nur=20noch=20YouTube=20wird=20unterst?= =?UTF-8?q?=C3=BCtzt=20-=20YouTube:=20Bugfix,=20wenn=20Video-ID=20invalide?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/youtube.lua | 4 ++ otouto/plugins/youtube_dl.lua | 72 ++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/otouto/plugins/youtube.lua b/otouto/plugins/youtube.lua index d40524a..1f1c8dd 100644 --- a/otouto/plugins/youtube.lua +++ b/otouto/plugins/youtube.lua @@ -209,6 +209,10 @@ end function youtube:action(msg, config, matches) local yt_code = matches[1] local data = get_yt_data(yt_code) + if not data then + utilities.send_reply(self, msg, config.errors.results) + return + end send_youtube_data(data, msg, self) return end diff --git a/otouto/plugins/youtube_dl.lua b/otouto/plugins/youtube_dl.lua index b11b3d5..46d5dc1 100644 --- a/otouto/plugins/youtube_dl.lua +++ b/otouto/plugins/youtube_dl.lua @@ -2,34 +2,31 @@ local youtube_dl = {} function youtube_dl:init(config) youtube_dl.triggers = { - "^/(mp4) (https?://[%w-_%.%?%.:/%+=&]+)$", - "^/(mp3) (https?://[%w-_%.%?%.:/%+=&]+)$" + "^/(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-_-]+)" } youtube_dl.doc = [[* -]]..config.cmd_pat..[[mp3* __: Lädt Audio von [untersützten Seiten](https://rg3.github.io/youtube-dl/supportedsites.html) -*]]..config.cmd_pat..[[mp4* __: Lädt Video von [untersützten Seiten](https://rg3.github.io/youtube-dl/supportedsites.html) +]]..config.cmd_pat..[[mp3* __: Lädt Audio von YouTube +*]]..config.cmd_pat..[[mp4* __: Lädt Video von YouTube ]] end youtube_dl.command = 'mp3 , /mp4 ' -function youtube_dl:convert_video(link) - local output = io.popen('youtube-dl -f mp4 --max-filesize 49m -o "/tmp/%(title)s.%(ext)s" '..link):read('*all') - print(output) - if string.match(output, '.* File is larger .*') then - return 'TOOBIG' - end - local video = string.match(output, '%[download%] Destination: /tmp/(.*).mp4') - if not video then - video = string.match(output, '%[download%] /tmp/(.*).mp4 has already been downloaded') - end - return '/tmp/'..video..'.mp4' +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 end -function youtube_dl:convert_audio(link) - local output = io.popen('youtube-dl --max-filesize 49m -o "/tmp/%(title)s.%(ext)s" --extract-audio --audio-format mp3 '..link):read('*all') - print(output) +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') if string.match(output, '.* File is larger .*') then return 'TOOBIG' end @@ -37,25 +34,48 @@ function youtube_dl:convert_audio(link) return '/tmp/'..audio..'.mp3' end -function youtube_dl:action(msg, config) - local link = matches[2] +function youtube_dl:action(msg, config, matches) + local id = matches[2] if matches[1] == 'mp4' then + local first_msg = utilities.send_reply(self, msg, 'Video wird heruntergeladen...', 'HTML') utilities.send_typing(self, msg.chat.id, 'upload_video') - local file = youtube_dl:convert_video(link) - if file == 'TOOBIG' then - utilities.send_reply(self, msg, 'Das Video überschreitet die Grenze von 50 MB!') + 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) return end - utilities.send_video(self, msg.chat.id, file, nil, msg.message_id) + + 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, 'Das Video überschreitet die Grenze von 50 MB!\nDirektlink zum Video ('..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, 'Direktlink zum Video ('..resolution..')', nil, 'HTML') + utilities.send_video(self, msg.chat.id, file, nil, msg.message_id, duration, width, height) return end if matches[1] == 'mp3' then + local first_msg = utilities.send_reply(self, msg, 'Audio wird heruntergeladen...', 'HTML') utilities.send_typing(self, msg.chat.id, 'upload_audio') - local file = youtube_dl:convert_audio(link) + local file = youtube_dl:convert_audio(id) if file == 'TOOBIG' then - utilities.send_reply(self, msg, 'Die MP3 überschreitet die Grenze von 50 MB!') + utilities.edit_message(self, msg.chat.id, first_msg.result.message_id, 'Die MP3 überschreitet die Grenze von 50 MB!', nil, 'HTML') return end utilities.send_audio(self, msg.chat.id, file, msg.message_id)