aboutsummaryrefslogtreecommitdiff
path: root/modules/upc.py
blob: f67f06b93b5c7f4f64a49b40cee78d04f8efa801 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from src import ModuleManager, utils

UPCITEMDB_URL = "https://api.upcitemdb.com/prod/trial/lookup"

class Module(ModuleManager.BaseModule):
    _name = "UPC"

    @utils.hook("received.command.upc|ean|gtin", min_args=1)
    def upc(self, event):
        """
        :help: Look up a product by UPC, EAN or GTIN
        :usage: <UPC|EAN|GTIN>
        """
        arg_len = len(event["args_split"][0])
        if not arg_len == 12 and not arg_len == 13:
            raise utils.EventError("Invalid UPC/EAN/GTIN provided")

        page = utils.http.get_url(UPCITEMDB_URL,
            get_params={"upc": event["args_split"][0]},
            json=True)
        if page:
            if not len(page["items"]):
                raise utils.EventError("UPC/EAN not found")
            item = page["items"][0]

            brand = item.get("brand", None)
            brand = "" if not brand else "%s - " % brand
            title = item["title"]
            description = item.get("description", None)
            description = " " if not description else ": %s " % description

            weight = item.get("weight", None)
            weight = weight or "unknown"
            size = item.get("dimension", None)
            size = size or "unknown"

            currency = item.get("currency", None)
            lowest_price = item.get("lowest_recorded_price", None)
            highest_price = item.get("highest_recorded_price", None)

            pricing = "price: unknown"
            if lowest_price and highest_price and currency:
                pricing = "price: %s to %s %s" % (
                    lowest_price, highest_price, currency)

            event["stdout"].write("%s%s%s(weight: %s"
                ", size: %s, price: %s)" % (
                brand, title, description, weight, size, pricing))
        else:
            event["stderr"].write("Failed to load results")