aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorGravatar jesopo2018-07-25 13:05:37 +0100
committerGravatar jesopo2018-07-25 13:05:37 +0100
commita7a28adde7c57ecd329122532b156afdb26ce086 (patch)
treea4da2ceb13a18a962f22f26da73ca4d3fc96f962 /modules
parentIncluded notices in print_activity, don't always print channel name (diff)
signature
Add upc.py
Diffstat (limited to 'modules')
-rw-r--r--modules/upc.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/modules/upc.py b/modules/upc.py
new file mode 100644
index 00000000..c8da6083
--- /dev/null
+++ b/modules/upc.py
@@ -0,0 +1,44 @@
+import Utils
+
+UPCITEMDB_URL = "https://api.upcitemdb.com/prod/trial/lookup"
+
+class Module(object):
+ _name = "UPC"
+ def __init__(self, bot):
+ self.bot = bot
+ bot.events.on("received").on("command").on("upc", "ean").hook(
+ self.upc, min_args=1, usage="<UPC|EAN>",
+ help="Look up a product by UPC or EAN")
+
+ def upc(self, event):
+ arg_len = len(event["args_split"][0])
+ if not arg_len == 12 and not arg_len == 13:
+ event["stderr"].write("Invalid UPC/EAN provided")
+ return
+
+ page = Utils.get_url(UPCITEMDB_URL,
+ get_params={"upc": event["args_split"][0]},
+ json=True)
+ if page:
+ if not len(page["items"]):
+ event["stderr"].write("UPC/EAN not found")
+ return
+ item = page["items"][0]
+
+ brand = item["brand"]
+ title = item["title"]
+ description = item["description"]
+
+ weight = item["weight"]
+ size = item["dimension"]
+
+ currency = item["currency"]
+ lowest_price = item["lowest_recorded_price"]
+ highest_price = item["highest_recorded_price"]
+
+ event["stdout"].write("%s - %s: %s (weight: %s"
+ ", size: %s, price: %s to %s %s)" % (
+ brand, title, description, weight, size,
+ lowest_price, highest_price, currency))
+ else:
+ event["stderr"].write("Failed to load results")