Skip to content

Commit

Permalink
updated costco
Browse files Browse the repository at this point in the history
  • Loading branch information
builder555 committed Jan 26, 2024
1 parent 543f686 commit 24a60d6
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions app/readers/costco_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@
import httpx
import re
from .__agents import agents
import base64

SITE = "www.costco.ca"

def is_base64(s: str) -> bool:
try:
return base64.b64encode(base64.b64decode(s)) == s.encode('utf-8')
except Exception:
return False

def extract_using_pattern(html: str, pattern: str) -> float | None:
matches = re.findall(pattern, html)
if len(matches) == 0:
return None
if is_base64(matches[0]):
matches[0] = base64.b64decode(matches[0]).decode("utf-8")
return float(matches[0].strip().strip("$").strip())

def get_price(url: str, client: httpx.Client) -> float:
agent = random.choice(agents)
r = client.get(url, headers={"user-agent": agent}, timeout=5)
html = r.text
pattern = r"priceMin: '(\d+\.\d+)'"
matches = re.findall(pattern, html)
price = matches[0]
return float(price.strip().strip("$").strip())
patterns = [
r"priceMin: '(\d+\.\d+)'",
r"price: (\d+\.\d+)",
r"price: '([A-Za-z0-9+/=]+)'"
]
for pattern in patterns:
price = extract_using_pattern(html, pattern)
if price is not None:
return price
raise Exception("Price not found")

0 comments on commit 24a60d6

Please sign in to comment.