woodshop/scripts/fetch_kent_prices.py

61 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Best-effort refresh of WoodShop's lumber price book from Kent (kent.ca, NB).
Kent renders prices in the browser (the price is not in the page HTML), so a
plain HTTP fetch usually finds nothing. For reliable results install Playwright:
pip install playwright && playwright install chromium
Then run:
python scripts/fetch_kent_prices.py # show what it can read
python scripts/fetch_kent_prices.py --write # also save to the price book
This is intentionally honest: if Kent changes their site this may stop working
and you'll need to update KENT_URLS / the parser in src/woodshop/prices.py, or
just edit prices by hand in the app (Cost tab → "Edit prices…").
"""
import argparse
import sys
from woodshop import prices as P
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description="Refresh WoodShop prices from Kent NB.")
ap.add_argument("--write", action="store_true", help="Save fetched prices to the price book")
ap.add_argument("--no-browser", action="store_true",
help="Skip the Playwright render (static HTTP only — likely finds nothing)")
args = ap.parse_args(argv)
print(f"Fetching {len(P.KENT_URLS)} product(s) from Kent…")
fetched = P.fetch_kent_prices(use_browser=not args.no_browser)
if not fetched:
print("\n ⚠ Could not read any live prices.")
print(" Kent injects prices client-side; install Playwright for reliable results:")
print(" pip install playwright && playwright install chromium")
print(" Or edit prices by hand in the app (Cost tab → Edit prices…).")
return 1
print("\n Read these prices:")
for stock, price in sorted(fetched.items()):
print(f" {stock:<10} ${price:,.2f}")
missing = sorted(set(P.KENT_URLS) - set(fetched))
if missing:
print(f"\n (couldn't read: {', '.join(missing)})")
if args.write:
book = P.load_prices()
book.update(fetched)
P.save_prices(book)
print(f"\n ✓ saved to {P._config_path()}")
else:
print("\n (dry run — pass --write to save these to the price book)")
return 0
if __name__ == "__main__":
sys.exit(main())