This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/miku/plugins/kickstarter_search.lua

56 lines
2.0 KiB
Lua

local kickstarter_search = {}
function kickstarter_search:init(config)
kickstarter_search.triggers = {
"/[Kk][Ii][Cc][Kk][Ss] (.+)$"
}
kickstarter_search.doc = [[*
]]..config.cmd_pat..[[kicks* _<Suchbegriff>_: Sucht ein Projekt auf Kickstarter]]
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
utilities.send_reply(msg, config.errors.connection)
return
elseif text == 'NOTFOUND' then
utilities.send_reply(msg, config.errors.results)
return
end
if image_url then
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(image_url, query..'.png')
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
end
utilities.send_reply(msg, text, 'HTML')
end
return kickstarter_search