From 6c5bf5f19a9f20332e87bdd6aa65ea1850f34a89 Mon Sep 17 00:00:00 2001 From: Akamaru Date: Sun, 16 Oct 2016 19:24:36 +0200 Subject: [PATCH] Neues Plugin: ean_search.lua --- miku/plugins/ean_search.lua | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 miku/plugins/ean_search.lua diff --git a/miku/plugins/ean_search.lua b/miku/plugins/ean_search.lua new file mode 100644 index 0000000..09387ac --- /dev/null +++ b/miku/plugins/ean_search.lua @@ -0,0 +1,62 @@ +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 \ No newline at end of file