buypeeb/listing.py

57 lines
1.7 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, url: str, id: str, name: str = None, from_json: dict = None):
# note - incoming url is not validated in any way!
self.name = name
if url == None and from_json != None and id != None:
self.id = id
self.name = from_json['name']
self.original_name = from_json['original_name']
self.favourite = from_json['favourite']
self.url = f"https://buyee.jp/item/yahoo/auction/{self.id}"
elif url != None and id != None:
self.url = url
self.id = id
else:
raise RuntimeError
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(f"https://page.auctions.yahoo.co.jp/jp/auction/{self.id}").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)
self.original_name = j['productName']
if self.name == None:
self.name = j['productName']
def price_jpy(self):
return f"¥{self.price:.2f}"
def price_aud(self, rate = 75.0):
return f"${self.price / rate:.2f}"