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 '..data.brand..''
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 = '\nAuf Amazon.de ansehen.'
else
amazon_link = ''
end
if data.images[1] then
image_url = data.images[1]
else
image_url = nil
end
local text = ''..name..''..brand..'\nAuf upcitemdb ansehen.'..amazon_link..'\n\n'..description..''
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, 'Diesen EAN gibt es nicht oder er wurde nicht gefunden!', '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