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/ean_search.lua

62 lines
1.8 KiB
Lua

local ean_search = {}
ean_search.triggers = {'^/[Ee][Aa][Nn] (.+)$'}
local BASE_URL = 'https://api.upcitemdb.com/prod/trial/lookup?upc='
function ean_search:get_ean_info(ean)
local url = BASE_URL..ean
local res,code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end
local data = json.decode(res).items[1]
if data == nil then return 'NOTFOUND' end
return data
end
function ean_search:send_ean_info(data)
local name = data.title
if data.brand == "" then
brand = ''
else
brand = ' von <b>'..data.brand..'</b>'
end
if string.len(data.description) > 400 then
description = string.sub(unescape(data.description:gsub("%b<>", "")), 1, 400)..'...'
else
description = unescape(data.description)
end
local link = 'http://www.upcitemdb.com/upc/'..data.ean
if data.asin then
amazon_link = '\n<a href="https://www.amazon.de/dp/'..data.asin..'">Auf Amazon.de ansehen.</a>'
else
amazon_link = ''
end
if data.images[1] then
image_url = data.images[1]
else
image_url = nil
end
local text = '<b>'..name..'</b>'..brand..'\n<a href="'..link..'">Auf upcitemdb ansehen.</a>'..amazon_link..'\n\n<i>'..description..'</i>'
return text, image_url
end
function ean_search:action(msg, config, matches)
local ean = matches[1]
local data = ean_search:get_ean_info(ean)
if data == 'HTTP-FEHLER' or data == 'NOTFOUND' then
utilities.send_reply(msg, '<b>Diesen EAN gibt es nicht oder er wurde nicht gefunden!</b>', 'HTML')
return
else
local output, image_url = ean_search:send_ean_info(data)
utilities.send_reply(msg, output, 'HTML')
if image_url then
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, image_url, nil, msg.message_id)
end
end
end
return ean_search