Flickr-Plugins verbessert

This commit is contained in:
Andreas Bielawski 2016-10-06 15:50:22 +02:00
parent b428d59b1f
commit b17b070ed5
2 changed files with 39 additions and 13 deletions

View File

@ -43,11 +43,12 @@ function flickr:send_flickr_photo_data(data)
return text, image_url return text, image_url
end end
else else
return '"'..title..'", aufgenommen '..taken..' von '..username..' ('..data.views..' Aufrufe)\nBild konnte nicht gedownloadet werden (Keine Berechtigung)' return '<b>'..title..'</b>, aufgenommen '..taken..' von <b>'..username..'</b> <i>('..data.views..' Aufrufe)</i>\nBild konnte nicht gedownloadet werden (Keine Berechtigung)'
end end
end end
function flickr:action(msg, config, matches) function flickr:action(msg, config, matches)
utilities.send_typing(msg.chat.id, 'upload_photo')
local data = flickr:get_flickr_photo_data(matches[2]) local data = flickr:get_flickr_photo_data(matches[2])
if not data then utilities.send_reply(msg, config.errors.connection) return end if not data then utilities.send_reply(msg, config.errors.connection) return end
local text, image_url, isgif = flickr:send_flickr_photo_data(data) local text, image_url, isgif = flickr:send_flickr_photo_data(data)
@ -63,7 +64,7 @@ function flickr:action(msg, config, matches)
return return
end end
else else
utilities.send_reply(msg, text) utilities.send_reply(msg, text, 'HTML')
return return
end end
end end

View File

@ -8,7 +8,7 @@ function flickr_search:init(config)
end end
flickr_search.triggers = { flickr_search.triggers = {
"^/flickr (.*)$" "^/flickr (.+)$"
} }
end end
@ -18,29 +18,54 @@ local apikey = cred_data.flickr_apikey
local BASE_URL = 'https://api.flickr.com/services/rest' local BASE_URL = 'https://api.flickr.com/services/rest'
function flickr_search:get_flickr(term) function flickr_search:get_flickr(term)
local url = BASE_URL..'/?method=flickr.photos.search&api_key='..apikey..'&format=json&nojsoncallback=1&privacy_filter=1&safe_search=3&extras=url_o&text='..term local url = BASE_URL..'/?method=flickr.photos.search&api_key='..apikey..'&format=json&nojsoncallback=1&privacy_filter=1&safe_search=3&media=photos&sort=relevance&is_common=true&per_page=20&extras=url_l,url_o&text='..term
local b,c = https.request(url) local b,c = https.request(url)
if c ~= 200 then return nil end if c ~= 200 then return nil end
local photo = json.decode(b).photos.photo local photo = json.decode(b).photos.photo
if not photo[1] then return nil end
-- truly randomize -- truly randomize
math.randomseed(os.time()) math.randomseed(os.time())
-- random max json table size -- random max json table size
local i = math.random(#photo) local i = math.random(#photo)
local link_image = photo[i].url_o
return link_image local link_image = photo[i].url_l or photo[i].url_o
local orig_image = photo[i].url_o or link_image
local title = photo[i].title
if title:len() > 200 then
title = title:sub(1, 197) .. '...'
end
return link_image, title, orig_image
end
function flickr_search:callback(callback, msg, self, config, input)
utilities.send_typing(msg.chat.id, 'upload_photo')
local input = URL.unescape(input)
utilities.answer_callback_query(callback, 'Suche nochmal nach "'..input..'"')
local url, title, orig = flickr_search:get_flickr(input)
if not url then utilities.answer_callback_query(callback, 'Konnte nicht mit Flickr verbinden :(', true) return end
if string.ends(url, ".gif") then
utilities.send_document(msg.chat.id, url, title, msg.message_id, '{"inline_keyboard":[[{"text":"Im Browser öffnen","url":"'..orig..'"},{"text":"Nochmal suchen","callback_data":"flickr_search:'..URL.escape(input)..'"}]]}')
return
else
utilities.send_photo(msg.chat.id, url, title, msg.message_id, '{"inline_keyboard":[[{"text":"Bild öffnen","url":"'..orig..'"}, {"text":"Nochmal suchen","callback_data":"flickr_search:'..URL.escape(input)..'"}]]}')
return
end
end end
function flickr_search:action(msg, config, matches) function flickr_search:action(msg, config, matches)
local url = flickr_search:get_flickr(matches[1]) utilities.send_typing(msg.chat.id, 'upload_photo')
local url, title, orig = flickr_search:get_flickr(matches[1])
if not url then utilities.send_reply(msg, config.errors.results) return end if not url then utilities.send_reply(msg, config.errors.results) return end
local file = download_to_file(url)
if string.ends(url, ".gif") then if string.ends(url, ".gif") then
utilities.send_document(msg.chat.id, file, url) utilities.send_document(msg.chat.id, url, title, msg.message_id, '{"inline_keyboard":[[{"text":"Im Browser öffnen","url":"'..orig..'"},{"text":"Nochmal suchen","callback_data":"flickr_search:'..URL.escape(matches[1])..'"}]]}')
return return
else else
utilities.send_photo(msg.chat.id, file, url) utilities.send_photo(msg.chat.id, url, title, msg.message_id, '{"inline_keyboard":[[{"text":"Bild öffnen","url":"'..orig..'"}, {"text":"Nochmal suchen","callback_data":"flickr_search:'..URL.escape(matches[1])..'"}]]}')
return return
end end
end end