buypeeb/listing.py

47 lines
1.4 KiB
Python
Raw Normal View History

2020-08-10 09:34:24 +00:00
import requests
import re, json
from datetime import datetime, timezone, timedelta
2020-08-21 10:52:03 +00:00
import functions
2020-08-10 09:34:24 +00:00
JST = timezone(timedelta(hours = 9))
class YahooAuctionsItem:
2020-08-21 10:52:03 +00:00
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"
2020-08-21 10:52:03 +00:00
self.last_checked = datetime.fromisoformat('1970-01-01')
self.available = True
self.update()
2020-08-10 09:34:24 +00:00
def update(self):
try:
2020-08-21 10:52:03 +00:00
# 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
2020-08-10 09:34:24 +00:00
# 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}"