Last active 1706137835

Revision 07cd2daf59429ea993f08b7e997bb1f63d129216

conk.py Raw
1#!/usr/bin/env python3
2from requests import Session, get, post
3from bs4 import BeautifulSoup as Soup
4
5# Config
6webhook = "" # discord webhook
7
8shop = {
9 # add entries for things you want to track
10 # copy the numeric IDs from urls
11 # e.g:
12 # https://www.coles.com.au/product/monster-ultra-rosa-multipack-cans-4-x-500ml-4-pack-3990033 => 3990033
13 "coke": ("2993706", "623034", "848631"),
14 "monster": ("3990033", "143253", None),
15}
16# end Config
17
18class Price:
19 def __init__(self, coles, woolies, iga):
20 self.coles = coles
21 self.woolies = woolies
22 self.iga = iga
23
24 def sorted(self):
25 price = {}
26 if self.coles:
27 price["coles"] = self.coles
28 if self.woolies:
29 price["woolies"] = self.woolies
30 if self.iga:
31 price["iga"] = self.iga
32 return "\n".join(f"{name}: ${price:.2f}" for name,price in sorted(price.items(), key=lambda x: x[1]))
33
34out = {}
35
36for name,ids in shop.items():
37 coles,woolies,iga = None,None,None
38 if ids[0]:
39 coles = Soup(get(f"https://www.coles.com.au/product/{ids[0]}").text, "html.parser")
40 if ids[2]:
41 iga = get(f"https://www.igashop.com.au/api/storefront/stores/84996/products/{ids[2]}").json()
42
43 if ids[1]:
44 session = Session()
45 session.headers["User-Agent"] = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/116.0'
46 session.get(f"https://www.woolworths.com.au/shop/productdetails/{ids[1]}")
47 woolies = session.get(f"https://www.woolworths.com.au/api/v3/ui/schemaorg/product/{ids[1]}").json()
48
49 out[name] = Price(
50 float(coles.findAll("span", class_="price__value")[0].text[1:]) if coles else 0.0,
51 woolies["offers"]["price"] if woolies else 0.0,
52 float(iga["price"][1:]) if iga else 0.0
53 )
54
55post(webhook, json={"content": "\n\n".join(f"**{name}**\n{price.sorted()}" for name,price in out.items())})