2016-06-18 17:22:30 +02:00
local flickr = { }
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
2016-10-06 15:50:22 +02:00
return ' <b> ' .. title .. ' </b>, aufgenommen ' .. taken .. ' von <b> ' .. username .. ' </b> <i>( ' .. data.views .. ' Aufrufe)</i> \n Bild konnte nicht gedownloadet werden (Keine Berechtigung) '
2016-06-18 17:22:30 +02:00
end
end
function flickr : action ( msg , config , matches )
2016-10-06 15:50:22 +02:00
utilities.send_typing ( msg.chat . id , ' upload_photo ' )
2016-06-18 17:22:30 +02:00
local data = flickr : get_flickr_photo_data ( matches [ 2 ] )
2016-08-24 15:38:29 +02:00
if not data then utilities.send_reply ( msg , config.errors . connection ) return end
2016-06-18 17:22:30 +02:00
local text , image_url , isgif = flickr : send_flickr_photo_data ( data )
if image_url then
2016-08-24 15:38:29 +02:00
utilities.send_typing ( msg.chat . id , ' upload_photo ' )
2016-06-18 17:22:30 +02:00
local file = download_to_file ( image_url )
if isgif then
2016-08-24 15:38:29 +02:00
utilities.send_document ( msg.chat . id , file , text , msg.message_id )
2016-06-18 17:22:30 +02:00
return
else
2016-08-24 15:38:29 +02:00
utilities.send_photo ( msg.chat . id , file , text , msg.message_id )
2016-06-18 17:22:30 +02:00
return
end
else
2016-10-06 15:50:22 +02:00
utilities.send_reply ( msg , text , ' HTML ' )
2016-06-18 17:22:30 +02:00
return
end
end
return flickr