Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
57 lines (49 loc) · 2.51 KB
/
Copy pathexample.py
File metadata and controls
57 lines (49 loc) · 2.51 KB
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
51
52
53
54
55
56
57
"""Weekly Coles half-price tracker + cheapest-per-100g finder.
1) Pulls every half-price special from Coles (coles.com.au) and writes
coles_halfprice.csv, biggest dollar saving first.
2) Searches a few keywords and prints the cheapest products by unit price.
Usage:
pip install -r requirements.txt
APIFY_TOKEN=<YOUR_APIFY_TOKEN> python example.py
"""
import csv
import os
from apify_client import ApifyClient
TOKEN = os.environ.get("APIFY_TOKEN", "<YOUR_APIFY_TOKEN>")
ACTOR = "rel8ble/coles-scraper"
KEYWORDS = ["coffee", "chocolate"]
client = ApifyClient(TOKEN)
# 1) This week's half-price list (new specials start each Wednesday).
# maxItems 0 = the whole list, usually ~1,500 products (~$2.30 at $1.50/1,000).
# Set a smaller maxItems for a cheap first try.
run = client.actor(ACTOR).call(run_input={"specials": "halfprice", "maxItems": 200})
specials = list(client.dataset(run["defaultDatasetId"]).iterate_items())
specials.sort(key=lambda r: r.get("saveAmount") or 0, reverse=True)
fields = ["brand", "name", "size", "price", "wasPrice", "saveAmount",
"unitPriceText", "department", "available", "url"]
with open("coles_halfprice.csv", "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
w.writeheader()
w.writerows(specials)
print(f"{len(specials)} half-price products -> coles_halfprice.csv")
for r in specials[:5]:
print(f" save ${r['saveAmount']:.2f} {r['brand']} {r['name']} {r['size']} ${r['price']} (was ${r['wasPrice']})")
# 2) Cheapest per unit for each keyword (compares only rows sharing the same unit basis).
run = client.actor(ACTOR).call(run_input={"searchQueries": KEYWORDS, "maxItems": 40})
rows = [r for r in client.dataset(run["defaultDatasetId"]).iterate_items() if r.get("unitPrice")]
for kw in KEYWORDS:
hits = [r for r in rows if r.get("sourceQuery") == kw]
if not hits:
continue
# Use the most common unit basis for this keyword, e.g. "100 g".
bases = {}
for r in hits:
key = (r["unitPriceQuantity"], r["unitPriceUnit"])
bases[key] = bases.get(key, 0) + 1
basis = max(bases, key=bases.get)
same = sorted((r for r in hits if (r["unitPriceQuantity"], r["unitPriceUnit"]) == basis),
key=lambda r: r["unitPrice"])
print(f"\nCheapest '{kw}' per {basis[0]}{basis[1]}:")
for r in same[:5]:
tag = f" [{r['specialType']}]" if r.get("specialType") else ""
print(f" {r['unitPriceText']:>14} {r['brand']} {r['name']} {r['size']} ${r['price']}{tag}")