diff --git a/otouto/plugins/flickr.lua b/otouto/plugins/flickr.lua new file mode 100644 index 0000000..8a66428 --- /dev/null +++ b/otouto/plugins/flickr.lua @@ -0,0 +1,76 @@ +local flickr = {} + +local https = require('ssl.https') +local json = require('dkjson') +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +function flickr:init(config) + if not cred_data.flickr_apikey then + print('Missing config value: flickr_apikey.') + print('flickr.lua will not be enabled.') + return + end + + flickr.triggers = { + "flickr.com/photos/([A-Za-z0-9-_-]+)/([0-9]+)" + } +end + +local BASE_URL = 'https://api.flickr.com/services/rest' + +local makeOurDate = function(dateString) + local pattern = "(%d+)%-(%d+)%-(%d+) (%d+)%:(%d+)%:(%d+)" + local year, month, day, hours, minutes, seconds = dateString:match(pattern) + return day..'.'..month..'.'..year..' um '..hours..':'..minutes..':'..seconds..' Uhr' +end + +function flickr:get_flickr_photo_data (photo_id) + local apikey = cred_data.flickr_apikey + local url = BASE_URL..'/?method=flickr.photos.getInfo&api_key='..apikey..'&photo_id='..photo_id..'&format=json&nojsoncallback=1' + local res,code = https.request(url) + if code ~= 200 then return "HTTP-FEHLER" end + local data = json.decode(res).photo + return data +end + +function flickr:send_flickr_photo_data(data) + local title = data.title._content + local username = data.owner.username + local taken = data.dates.taken + local views = data.views + if data.usage.candownload == 1 then + local text = '"'..title..'", aufgenommen am '..makeOurDate(taken)..' von '..username..' ('..comma_value(data.views)..' Aufrufe)' + local image_url = 'https://farm'..data.farm..'.staticflickr.com/'..data.server..'/'..data.id..'_'..data.originalsecret..'_o_d.'..data.originalformat + if data.originalformat == 'gif' then + return text, image_url, true + else + return text, image_url + end + else + return '"'..title..'", aufgenommen '..taken..' von '..username..' ('..data.views..' Aufrufe)\nBild konnte nicht gedownloadet werden (Keine Berechtigung)' + end +end + +function flickr:action(msg, config, matches) + local data = flickr:get_flickr_photo_data(matches[2]) + if not data then utilities.send_reply(self, msg, config.errors.connection) return end + local text, image_url, isgif = flickr:send_flickr_photo_data(data) + + if image_url then + utilities.send_typing(self, msg.chat.id, 'upload_photo') + local file = download_to_file(image_url) + if isgif then + utilities.send_document(self, msg.chat.id, file, text, msg.message_id) + return + else + utilities.send_photo(self, msg.chat.id, file, text, msg.message_id) + return + end + else + utilities.send_reply(self, msg, text) + return + end +end + +return flickr \ No newline at end of file diff --git a/otouto/plugins/flickr_search.lua b/otouto/plugins/flickr_search.lua new file mode 100644 index 0000000..4d4b9a3 --- /dev/null +++ b/otouto/plugins/flickr_search.lua @@ -0,0 +1,53 @@ +local flickr_search = {} + +local https = require('ssl.https') +local json = require('dkjson') +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +function flickr_search:init(config) + if not cred_data.flickr_apikey then + print('Missing config value: flickr_apikey.') + print('flickr_search.lua will not be enabled.') + return + end + + flickr_search.triggers = { + "^/flickr (.*)$" + } +end + +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 + local b,c = https.request(url) + if c ~= 200 then return nil end + local photo = json.decode(b).photos.photo + -- 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 +end + +function flickr_search:action(msg, config, matches) + local url = flickr_search:get_flickr(matches[1]) + if not url then utilities.send_reply(self, msg, config.errors.results) return end + + local file = download_to_file(url) + + if string.ends(url, ".gif") then + utilities.send_document(self, msg.chat.id, file, url) + return + else + utilities.send_photo(self, msg.chat.id, file, url) + return + end +end + +return flickr_search \ No newline at end of file diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index 8ecc088..20ce55c 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -70,8 +70,10 @@ function gImages:action(msg, config) 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 else utilities.send_photo(self, msg.chat.id, file, img_url) + return end end