fix deprecated code, better save/load, allow adding items to and displaying watchlist
This commit is contained in:
parent
081bbe9f42
commit
79a22cc06e
3 changed files with 92 additions and 30 deletions
39
buypeeb.py
39
buypeeb.py
|
@ -52,11 +52,15 @@ class BuypeebApp:
|
||||||
builder.add_from_string(open(path.join(path.dirname(__file__), "ui/main.glade"), 'r').read())
|
builder.add_from_string(open(path.join(path.dirname(__file__), "ui/main.glade"), 'r').read())
|
||||||
builder.connect_signals(self)
|
builder.connect_signals(self)
|
||||||
wndMain = builder.get_object('wndMain')
|
wndMain = builder.get_object('wndMain')
|
||||||
|
self.items = builder.get_object('lstItems')
|
||||||
|
self.items.clear() # ensure list is empty
|
||||||
self.window = wndMain
|
self.window = wndMain
|
||||||
self.builder = builder
|
self.builder = builder
|
||||||
app.add_window(self.window)
|
app.add_window(self.window)
|
||||||
|
self.setExchangeRate()
|
||||||
|
|
||||||
self.settings.load()
|
self.settings.load()
|
||||||
|
self.renderList()
|
||||||
|
|
||||||
def activate(self, app):
|
def activate(self, app):
|
||||||
self.window.show_all()
|
self.window.show_all()
|
||||||
|
@ -66,7 +70,14 @@ class BuypeebApp:
|
||||||
self.app.quit()
|
self.app.quit()
|
||||||
|
|
||||||
def msgBox(self, title, text, form = Gtk.MessageType.WARNING):
|
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)
|
msgbox.format_secondary_text(text)
|
||||||
response = msgbox.run()
|
response = msgbox.run()
|
||||||
msgbox.destroy()
|
msgbox.destroy()
|
||||||
|
@ -102,11 +113,33 @@ class BuypeebApp:
|
||||||
def entryBoxResponse(self, widget, entrybox, response):
|
def entryBoxResponse(self, widget, entrybox, response):
|
||||||
entrybox.response(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):
|
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):
|
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?")
|
prompt.format_secondary_text("Are you sure you want to quit buypeeb?")
|
||||||
response = prompt.run()
|
response = prompt.run()
|
||||||
if response:
|
if response:
|
||||||
|
|
19
listing.py
19
listing.py
|
@ -8,15 +8,25 @@ import functions
|
||||||
JST = timezone(timedelta(hours = 9))
|
JST = timezone(timedelta(hours = 9))
|
||||||
|
|
||||||
class YahooAuctionsItem:
|
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!
|
# note - incoming url is not validated in any way!
|
||||||
self.name = name
|
self.name = name
|
||||||
|
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:
|
try:
|
||||||
self.url = re.match(r"([^?]+)", url.rstrip("/")).group(1) # remove trailing slashes and query params
|
self.url = re.match(r"([^?]+)", url.rstrip("/")).group(1) # remove trailing slashes and query params
|
||||||
except:
|
except:
|
||||||
# ???
|
# ???
|
||||||
raise
|
raise
|
||||||
self.id = re.match(r".+\/(.+?)$", self.url) # extract "x12345" from "https://buyee.jp/whatever/blah/x12345"
|
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.last_checked = datetime.fromisoformat('1970-01-01')
|
||||||
self.available = True
|
self.available = True
|
||||||
self.update()
|
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 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
|
# 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()
|
r = open("yahoo.html").read()
|
||||||
j = json.loads(re.match(r'.*var pageData ?= ?(\{.*?\});', r, re.DOTALL).group(1))
|
j = json.loads(re.match(r'.*var pageData ?= ?(\{.*?\});', r, re.DOTALL).group(1))
|
||||||
except:
|
except:
|
||||||
|
@ -38,6 +48,9 @@ class YahooAuctionsItem:
|
||||||
self.start_date = datetime.fromisoformat(j['starttime']).replace(tzinfo = JST)
|
self.start_date = datetime.fromisoformat(j['starttime']).replace(tzinfo = JST)
|
||||||
self.end_date = datetime.fromisoformat(j['endtime']).replace(tzinfo = JST)
|
self.end_date = datetime.fromisoformat(j['endtime']).replace(tzinfo = JST)
|
||||||
self.last_checked = datetime.now(JST)
|
self.last_checked = datetime.now(JST)
|
||||||
|
self.original_name = j['productName']
|
||||||
|
if self.name == None:
|
||||||
|
self.name = j['productName']
|
||||||
|
|
||||||
def price_jpy(self):
|
def price_jpy(self):
|
||||||
return f"¥{self.price:.2f}"
|
return f"¥{self.price:.2f}"
|
||||||
|
|
52
settings.py
52
settings.py
|
@ -1,4 +1,4 @@
|
||||||
import json, os
|
import json, os, pickle
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
import functions
|
import functions
|
||||||
|
@ -6,31 +6,47 @@ from listing import YahooAuctionsItem
|
||||||
|
|
||||||
class BuypeebSettings:
|
class BuypeebSettings:
|
||||||
def __init__(self, location: str):
|
def __init__(self, location: str):
|
||||||
self.s = {}
|
self.updateInterval = 10 * 60
|
||||||
self.s['updateInterval'] = 10 * 60
|
self.favouriteUpdateInterval = 5 * 60
|
||||||
self.s['favouriteUpdateInterval'] = 5 * 60
|
self.updateIntervalCritical = 60
|
||||||
self.s['updateIntervalCritical'] = 60
|
self.favouriteUpdateIntervalCritical = 30
|
||||||
self.s['favouriteUpdateIntervalCritical'] = 30
|
self.watchlist = []
|
||||||
self.s['watchlist'] = []
|
self.jwatchlist = []
|
||||||
self.location = location
|
self.location = location
|
||||||
|
|
||||||
def save(self):
|
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):
|
def load(self):
|
||||||
if not path.isfile(self.location + "config.json"):
|
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')
|
open(self.location + "config.json", 'x')
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
self.s.update(json.load(open(self.location + "config.json", 'r')))
|
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))
|
||||||
|
|
||||||
|
self.jwatchlist = []
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
raise
|
||||||
print("Couldn't load settings - using defaults")
|
print("Couldn't load settings - using defaults")
|
||||||
|
|
||||||
def get(self, setting: str): # javalicious
|
def watch(self, url: str, name: str = None):
|
||||||
return self.s.get(setting, None)
|
self.watchlist.append(YahooAuctionsItem(url, name))
|
||||||
|
for item in self.watchlist:
|
||||||
def set(self, setting: str, value):
|
print(item.name, item.price)
|
||||||
self.s['setting'] = value
|
|
||||||
|
|
||||||
def watch(self, name: str, url: str):
|
|
||||||
self.s['watchlist'].add(YahooAuctionsItem(name, url))
|
|
||||||
|
|
Loading…
Reference in a new issue