From 79a22cc06e6dac1a2da4fc134a0159c753121b7f Mon Sep 17 00:00:00 2001 From: Lynne Date: Sat, 22 Aug 2020 18:55:15 +1000 Subject: [PATCH] fix deprecated code, better save/load, allow adding items to and displaying watchlist --- buypeeb.py | 39 +++++++++++++++++++++++++++++++++++--- listing.py | 29 ++++++++++++++++++++-------- settings.py | 54 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 92 insertions(+), 30 deletions(-) diff --git a/buypeeb.py b/buypeeb.py index 65e918f..6ffe0f6 100755 --- a/buypeeb.py +++ b/buypeeb.py @@ -52,11 +52,15 @@ class BuypeebApp: builder.add_from_string(open(path.join(path.dirname(__file__), "ui/main.glade"), 'r').read()) builder.connect_signals(self) wndMain = builder.get_object('wndMain') + self.items = builder.get_object('lstItems') + self.items.clear() # ensure list is empty self.window = wndMain self.builder = builder app.add_window(self.window) + self.setExchangeRate() self.settings.load() + self.renderList() def activate(self, app): self.window.show_all() @@ -66,7 +70,14 @@ class BuypeebApp: self.app.quit() def msgBox(self, title, text, form = Gtk.MessageType.WARNING): - msgbox = Gtk.MessageDialog(self.window, 0, form, Gtk.ButtonsType.OK, title) + msgbox = Gtk.MessageDialog( + parent = self.window, + flags = 0, + message_type = form, + buttons = Gtk.ButtonsType.OK, + message_format = title + ) + msgbox.format_secondary_text(text) response = msgbox.run() msgbox.destroy() @@ -102,11 +113,33 @@ class BuypeebApp: def entryBoxResponse(self, widget, entrybox, response): entrybox.response(response) + def setExchangeRate(self): + self.rate = functions.get_exchange_rate() + + def renderList(self): + self.items.clear() + print(self.settings.watchlist) + for item in self.settings.watchlist: + print("heeh hoo") + self.items.append([item.name, item.price_aud(self.rate), "heenlo", item.id]) + def btnAddClicked(self, widget): - print(self.entryBox("Add URL", "Enter the URL of the item you want to add.")) + url = self.entryBox("Add URL", "Enter the URL of the item you want to add.") + if url: + if functions.rmatch("", url): + self.settings.watch(url) + + self.renderList() def btnQuitClicked(self, widget): - prompt = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, "Really quit?") + prompt = Gtk.MessageDialog( + parent = self.window, + flags = 0, + message_type = Gtk.MessageType.QUESTION, + buttons = Gtk.ButtonsType.OK_CANCEL, + message_format = "Really quit?" + ) + prompt.format_secondary_text("Are you sure you want to quit buypeeb?") response = prompt.run() if response: diff --git a/listing.py b/listing.py index 272c0ba..55fd083 100644 --- a/listing.py +++ b/listing.py @@ -8,15 +8,25 @@ import functions JST = timezone(timedelta(hours = 9)) class YahooAuctionsItem: - def __init__(self, name: str, url: str): + def __init__(self, url: str, name: str = None, from_json: dict = None): # 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" + if url == None and from_json != None: + self.id = from_json['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}" + + else: + 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).group(1) # extract "x12345" from "https://buyee.jp/whatever/blah/x12345" + + self.last_checked = datetime.fromisoformat('1970-01-01') self.available = True self.update() @@ -26,7 +36,7 @@ class YahooAuctionsItem: # 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 = 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: @@ -38,6 +48,9 @@ class YahooAuctionsItem: 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}" diff --git a/settings.py b/settings.py index 8548cf5..cfd3bf1 100644 --- a/settings.py +++ b/settings.py @@ -1,4 +1,4 @@ -import json, os +import json, os, pickle from os import path import functions @@ -6,31 +6,47 @@ from listing import YahooAuctionsItem class BuypeebSettings: def __init__(self, location: str): - self.s = {} - self.s['updateInterval'] = 10 * 60 - self.s['favouriteUpdateInterval'] = 5 * 60 - self.s['updateIntervalCritical'] = 60 - self.s['favouriteUpdateIntervalCritical'] = 30 - self.s['watchlist'] = [] + self.updateInterval = 10 * 60 + self.favouriteUpdateInterval = 5 * 60 + self.updateIntervalCritical = 60 + self.favouriteUpdateIntervalCritical = 30 + self.watchlist = [] + self.jwatchlist = [] self.location = location def save(self): - json.dump(self.s, open(self.location + "config.json", 'w')) + for item in self.watchlist: + self.jwatchlist.append({ + "id": item.id, + "name": item.name, + "original_name": item.original_name, + "favourite": False + }) + + self.watchlist = [] + + print(self.__dict__) + json.dump(self.__dict__, open(self.location + "config.json", 'w')) def load(self): if not path.isfile(self.location + "config.json"): - os.makedirs(self.location, 0o755) + os.makedirs(self.location, 0o755, True) open(self.location + "config.json", 'x') - try: - self.s.update(json.load(open(self.location + "config.json", 'r'))) - except: - print("Couldn't load settings - using defaults") + else: + try: + j = json.load(open(self.location + "config.json", 'r')) + for key in j: + setattr(self, key, j[key]) + for item in self.jwatchlist: + self.watchlist.append(YahooAuctionsItem(None, from_json = item)) - def get(self, setting: str): # javalicious - return self.s.get(setting, None) + self.jwatchlist = [] - def set(self, setting: str, value): - self.s['setting'] = value + except: + raise + print("Couldn't load settings - using defaults") - def watch(self, name: str, url: str): - self.s['watchlist'].add(YahooAuctionsItem(name, url)) + def watch(self, url: str, name: str = None): + self.watchlist.append(YahooAuctionsItem(url, name)) + for item in self.watchlist: + print(item.name, item.price)