From b17b070ed5aa2cd90357d41d2c0c1e38c356d4d5 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 6 Oct 2016 15:50:22 +0200 Subject: [PATCH] Flickr-Plugins verbessert --- otouto/plugins/flickr.lua | 5 ++-- otouto/plugins/flickr_search.lua | 47 ++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/otouto/plugins/flickr.lua b/otouto/plugins/flickr.lua index 3f06aa6..f15d567 100644 --- a/otouto/plugins/flickr.lua +++ b/otouto/plugins/flickr.lua @@ -43,11 +43,12 @@ function flickr:send_flickr_photo_data(data) return text, image_url end else - return '"'..title..'", aufgenommen '..taken..' von '..username..' ('..data.views..' Aufrufe)\nBild konnte nicht gedownloadet werden (Keine Berechtigung)' + return ''..title..', aufgenommen '..taken..' von '..username..' ('..data.views..' Aufrufe)\nBild konnte nicht gedownloadet werden (Keine Berechtigung)' end end function flickr:action(msg, config, matches) + utilities.send_typing(msg.chat.id, 'upload_photo') local data = flickr:get_flickr_photo_data(matches[2]) if not data then utilities.send_reply(msg, config.errors.connection) return end local text, image_url, isgif = flickr:send_flickr_photo_data(data) @@ -63,7 +64,7 @@ function flickr:action(msg, config, matches) return end else - utilities.send_reply(msg, text) + utilities.send_reply(msg, text, 'HTML') return end end diff --git a/otouto/plugins/flickr_search.lua b/otouto/plugins/flickr_search.lua index 1bd9493..72ae2d9 100644 --- a/otouto/plugins/flickr_search.lua +++ b/otouto/plugins/flickr_search.lua @@ -8,7 +8,7 @@ function flickr_search:init(config) end flickr_search.triggers = { - "^/flickr (.*)$" + "^/flickr (.+)$" } end @@ -17,30 +17,55 @@ flickr_search.command = 'flickr ' local apikey = cred_data.flickr_apikey local BASE_URL = 'https://api.flickr.com/services/rest' -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 +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&media=photos&sort=relevance&is_common=true&per_page=20&extras=url_l,url_o&text='..term local b,c = https.request(url) if c ~= 200 then return nil end local photo = json.decode(b).photos.photo + if not photo[1] then return nil end + -- truly randomize math.randomseed(os.time()) -- random max json table size 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 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 - - local file = download_to_file(url) - + 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 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 end end