buypeeb/listing.py

47 lines
1.4 KiB
Python

import requests
import re, json
from datetime import datetime, timezone, timedelta
import functions
JST = timezone(timedelta(hours = 9))
class YahooAuctionsItem:
def __init__(self, name: str, url: str):
# note - incoming url is not validated in any way!
self.name = name
try:
self.url = re.match(r"([^?]+)", url.rstrip("/")).group(1) # remove trailing slashes and query params
except:
# ???
raise
self.id = re.match(r".+\/(.+?)$", self.url) # extract "x12345" from "https://buyee.jp/whatever/blah/x12345"
self.last_checked = datetime.fromisoformat('1970-01-01')
self.available = True
self.update()
def update(self):
try:
# the good news is, yahoo japan returns all the data we need in handy json format
# the bad news is that the only way to get that json format is to download the whole auction page and grep it
# r = requests.get("https://page.auctions.yahoo.co.jp/jp/auction/k487846283").text
r = open("yahoo.html").read()
j = json.loads(re.match(r'.*var pageData ?= ?(\{.*?\});', r, re.DOTALL).group(1))
except:
raise
j = j['items']
self.price = float(j['price'])
self.bids = j['bids']
self.start_date = datetime.fromisoformat(j['starttime']).replace(tzinfo = JST)
self.end_date = datetime.fromisoformat(j['endtime']).replace(tzinfo = JST)
self.last_checked = datetime.now(JST)
def price_jpy(self):
return f"¥{self.price:.2f}"
def price_aud(self, rate = 75.0):
return f"${self.price / rate:.2f}"