- YouTube: Fixes

- gImages: 403 hinzugefügt + Mimetype wird ausgelesen und das richtige Dokument gesendet + Perfoamcnce durch fields erhöht + Mehr Bilder sollten nun gesendet werden können (#2)
This commit is contained in:
Andreas Bielawski 2016-06-21 23:06:35 +02:00
parent ddbaca396f
commit 891a8f94c2
2 changed files with 19 additions and 9 deletions

View File

@ -50,29 +50,39 @@ function gImages:action(msg, config)
local apikey = cred_data.google_apikey local apikey = cred_data.google_apikey
local cseid = cred_data.google_cse_id local cseid = cred_data.google_cse_id
local BASE_URL = 'https://www.googleapis.com/customsearch/v1' 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) 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 if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection) utilities.send_reply(self, msg, config.errors.connection, true)
return return
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if jdat.searchInformation.totalResults == '0' then if jdat.searchInformation.totalResults == '0' then
utilities.send_reply(self, msg, config.errors.results) utilities.send_reply(self, msg, config.errors.results, true)
return return
end end
local i = math.random(jdat.queries.request[1].count) local i = math.random(jdat.queries.request[1].count)
local img_url = jdat.items[i].link local img_url = jdat.items[i].link
local file = download_to_file(img_url) if jdat.items[i].mime == 'image/gif' then
if string.ends(img_url, ".gif") then local file = download_to_file(img_url, 'img.gif')
utilities.send_document(self, msg.chat.id, file, img_url) result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id)
return
else 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 return
end end
end end

View File

@ -87,7 +87,7 @@ local function convertISO8601Time(duration)
end end
function send_youtube_data(data, msg, self, link, sendpic) 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 description = data.snippet.localized.description
local uploader = data.snippet.channelTitle local uploader = data.snippet.channelTitle
local upload_date = makeOurDate(data.snippet.publishedAt) local upload_date = makeOurDate(data.snippet.publishedAt)