2016-08-16 13:40:34 +02:00
local kickstarter_search = { }
function kickstarter_search : init ( config )
kickstarter_search.triggers = {
" /[Kk][Ii][Cc][Kk][Ss] (.+)$ "
}
kickstarter_search.doc = [ [ *
2016-08-25 23:17:46 +02:00
] ] .. config.cmd_pat .. [[kicks* _<Suchbegriff>_: Sucht ein Projekt auf Kickstarter]]
2016-08-16 13:40:34 +02:00
kickstarter_search.command = ' kicks <Suchbegriff> '
end
function kickstarter_search : search_it ( query )
local url = ' https://www.kickstarter.com/projects/search.json?term= ' .. query
local res , code = https.request ( url )
if code ~= 200 then return nil end
local data = json.decode ( res ) . projects [ 1 ]
if not data then return ' NOTFOUND ' end
local title = data.name
local from = data.creator . name
local country = data.country
local desc = data.blurb
local pledged = comma_value ( string.gsub ( data.pledged , " %.(.*) " , " " ) )
local goal = comma_value ( data.goal )
local currency = data.currency_symbol
local created = string.gsub ( convert_timestamp ( data.launched_at , ' %d.%m.%Y ' ) , ' % \n ' , ' ' )
local ending = convert_timestamp ( data.deadline , ' %d.%m.%Y ' )
local url = data.urls . web.project
if data.photo . full then
image_url = data.photo . full
end
local text = ' <b> ' .. title .. ' </b> von <i> ' .. from .. ' ( ' .. country .. ' )</i> \n ' .. pledged .. currency .. ' von ' .. goal .. currency .. ' erreicht \n ' .. ' Läuft von ' .. created .. ' bis ' .. ending .. ' \n <i> ' .. desc .. ' </i> \n <a href=" ' .. url .. ' ">Projektseite aufrufen</a> '
return text , image_url
end
function kickstarter_search : action ( msg , config , matches )
local query = matches [ 1 ]
local text , image_url = kickstarter_search : search_it ( query , slug )
if not text then
2016-08-24 17:18:17 +02:00
utilities.send_reply ( msg , config.errors . connection )
2016-08-16 13:40:34 +02:00
return
elseif text == ' NOTFOUND ' then
2016-08-24 17:18:17 +02:00
utilities.send_reply ( msg , config.errors . results )
2016-08-16 13:40:34 +02:00
return
end
if image_url then
2016-08-24 17:18:17 +02:00
utilities.send_typing ( msg.chat . id , ' upload_photo ' )
2016-08-16 13:40:34 +02:00
local file = download_to_file ( image_url , query .. ' .png ' )
2016-08-24 17:18:17 +02:00
utilities.send_photo ( msg.chat . id , file , nil , msg.message_id )
2016-08-16 13:40:34 +02:00
end
2016-08-24 17:18:17 +02:00
utilities.send_reply ( msg , text , ' HTML ' )
2016-08-16 13:40:34 +02:00
end
return kickstarter_search