buypeeb/listing.py

58 lines
1.7 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:
def __init__(self, url: str, id: str, name: str = None, from_json: dict = None):
2020-08-21 10:52:03 +00:00
# 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
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
# r = requests.get(f"https://page.auctions.yahoo.co.jp/jp/auction/{self.id}").text
2020-08-10 09:34:24 +00:00
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']
2020-08-10 09:34:24 +00:00
def price_jpy(self):
return f"¥{self.price:.2f}"
def price_aud(self, rate = 75.0):
return f"${self.price / rate:.2f}"