From 891a8f94c23d323f4a75c956b0786fc3f4693d66 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 21 Jun 2016 23:06:35 +0200 Subject: [PATCH] =?UTF-8?q?-=20YouTube:=20Fixes=20-=20gImages:=20403=20hin?= =?UTF-8?q?zugef=C3=BCgt=20+=20Mimetype=20wird=20ausgelesen=20und=20das=20?= =?UTF-8?q?richtige=20Dokument=20gesendet=20+=20Perfoamcnce=20durch=20fiel?= =?UTF-8?q?ds=20erh=C3=B6ht=20+=20Mehr=20Bilder=20sollten=20nun=20gesendet?= =?UTF-8?q?=20werden=20k=C3=B6nnen=20(#2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/gImages.lua | 26 ++++++++++++++++++-------- otouto/plugins/youtube.lua | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index 484ed02..cd1f13d 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -50,29 +50,39 @@ function gImages:action(msg, config) local apikey = cred_data.google_apikey local cseid = cred_data.google_cse_id local BASE_URL = 'https://www.googleapis.com/customsearch/v1' - local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. URL.escape(input) + local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. URL.escape(input) .. '&fields=searchInformation(totalResults),queries(request(count)),items(link,mime,image(contextLink))' local jstr, res = HTTPS.request(url) + if res == 403 then + local jdat = JSON.decode(jstr) + utilities.send_reply(self, msg, 'Fehler '..jdat.error.code..': '..jdat.error.message..' ('..jdat.error.errors[1].reason..')', nil, msg.message_id) + return + end + if res ~= 200 then - utilities.send_reply(self, msg, config.errors.connection) + utilities.send_reply(self, msg, config.errors.connection, true) return end local jdat = JSON.decode(jstr) if jdat.searchInformation.totalResults == '0' then - utilities.send_reply(self, msg, config.errors.results) + utilities.send_reply(self, msg, config.errors.results, true) return end local i = math.random(jdat.queries.request[1].count) local img_url = jdat.items[i].link - local file = download_to_file(img_url) - if string.ends(img_url, ".gif") then - utilities.send_document(self, msg.chat.id, file, img_url) - return + if jdat.items[i].mime == 'image/gif' then + local file = download_to_file(img_url, 'img.gif') + result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id) else - utilities.send_photo(self, msg.chat.id, file, img_url) + local file = download_to_file(img_url, 'img.png') + result = utilities.send_photo(self, msg.chat.id, file, img_url, msg.message_id) + end + + if not result then + utilities.send_reply(self, msg, config.errors.connection, true) return end end diff --git a/otouto/plugins/youtube.lua b/otouto/plugins/youtube.lua index cc48638..c5c98a7 100644 --- a/otouto/plugins/youtube.lua +++ b/otouto/plugins/youtube.lua @@ -87,7 +87,7 @@ local function convertISO8601Time(duration) end function send_youtube_data(data, msg, self, link, sendpic) - local title = data.snippet.localized.title + local title = utilities.md_escape(data.snippet.localized.title) -- local description = data.snippet.localized.description local uploader = data.snippet.channelTitle local upload_date = makeOurDate(data.snippet.publishedAt)