prevent duplicate items from being watched

This commit is contained in:
Lynne Megido 2020-08-22 19:26:09 +10:00
parent 1d9a704f68
commit 42eecc28cc
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
4 changed files with 38 additions and 27 deletions

View File

@ -75,7 +75,7 @@ class BuypeebApp:
flags = 0, flags = 0,
message_type = form, message_type = form,
buttons = Gtk.ButtonsType.OK, buttons = Gtk.ButtonsType.OK,
message_format = title text = title
) )
msgbox.format_secondary_text(text) msgbox.format_secondary_text(text)
@ -119,15 +119,18 @@ class BuypeebApp:
def renderList(self): def renderList(self):
self.items.clear() self.items.clear()
print(self.settings.watchlist) print(self.settings.watchlist)
for item in self.settings.watchlist: for id, item in self.settings.watchlist.items():
print("heeh hoo") print("heeh hoo")
self.items.append([item.name, item.price_aud(self.rate), "heenlo", item.id]) self.items.append([item.name, item.price_aud(self.rate), "heenlo", id])
def btnAddClicked(self, widget): def btnAddClicked(self, widget):
url = 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 url:
if functions.rmatch("", url): # vry simpl url validation for simpol creachers
if functions.rmatch("https?.+yahoo.+", url):
self.settings.watch(url) self.settings.watch(url)
else:
self.msgBox("Invalid URL", "The provided URL was invalid.", Gtk.MessageType.ERROR)
self.renderList() self.renderList()
@ -137,7 +140,7 @@ class BuypeebApp:
flags = 0, flags = 0,
message_type = Gtk.MessageType.QUESTION, message_type = Gtk.MessageType.QUESTION,
buttons = Gtk.ButtonsType.OK_CANCEL, buttons = Gtk.ButtonsType.OK_CANCEL,
message_format = "Really quit?" text = "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?")

View File

@ -12,5 +12,15 @@ def get_exchange_rate():
except: except:
return 76.15 return 76.15
def id_from_url(url: str):
try:
url = re.match(r"([^?]+)", url.rstrip("/")).group(1) # remove trailing slashes and query params
except:
# ???
raise
id = re.match(r".+\/(.+?)$", url).group(1) # extract "x12345" from "https://buyee.jp/whatever/blah/x12345"
return id
def rmatch(pattern, string): def rmatch(pattern, string):
return re.match(pattern, string) != None return re.match(pattern, string) != None

View File

@ -8,24 +8,22 @@ import functions
JST = timezone(timedelta(hours = 9)) JST = timezone(timedelta(hours = 9))
class YahooAuctionsItem: class YahooAuctionsItem:
def __init__(self, url: str, name: str = None, from_json: dict = None): def __init__(self, url: str, id: 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: if url == None and from_json != None and id != None:
self.id = from_json['id'] self.id = id
self.name = from_json['name'] self.name = from_json['name']
self.original_name = from_json['original_name'] self.original_name = from_json['original_name']
self.favourite = from_json['favourite'] self.favourite = from_json['favourite']
self.url = f"https://buyee.jp/item/yahoo/auction/{self.id}" self.url = f"https://buyee.jp/item/yahoo/auction/{self.id}"
else: elif url != None and id != None:
try: self.url = url
self.url = re.match(r"([^?]+)", url.rstrip("/")).group(1) # remove trailing slashes and query params self.id = id
except:
# ???
raise
self.id = re.match(r".+\/(.+?)$", self.url).group(1) # extract "x12345" from "https://buyee.jp/whatever/blah/x12345"
else:
raise RuntimeError
self.last_checked = datetime.fromisoformat('1970-01-01') self.last_checked = datetime.fromisoformat('1970-01-01')
self.available = True self.available = True

View File

@ -10,20 +10,19 @@ class BuypeebSettings:
self.favouriteUpdateInterval = 5 * 60 self.favouriteUpdateInterval = 5 * 60
self.updateIntervalCritical = 60 self.updateIntervalCritical = 60
self.favouriteUpdateIntervalCritical = 30 self.favouriteUpdateIntervalCritical = 30
self.watchlist = [] self.watchlist = {}
self.jwatchlist = [] self.jwatchlist = {}
self.location = location self.location = location
def save(self): def save(self):
for item in self.watchlist: for id, item in self.watchlist.items():
self.jwatchlist.append({ self.jwatchlist[id] = {
"id": item.id,
"name": item.name, "name": item.name,
"original_name": item.original_name, "original_name": item.original_name,
"favourite": False "favourite": False
}) }
self.watchlist = [] self.watchlist = {}
print(self.__dict__) print(self.__dict__)
json.dump(self.__dict__, open(self.location + "config.json", 'w')) json.dump(self.__dict__, open(self.location + "config.json", 'w'))
@ -37,16 +36,17 @@ class BuypeebSettings:
j = json.load(open(self.location + "config.json", 'r')) j = json.load(open(self.location + "config.json", 'r'))
for key in j: for key in j:
setattr(self, key, j[key]) setattr(self, key, j[key])
for item in self.jwatchlist: for key in self.jwatchlist:
self.watchlist.append(YahooAuctionsItem(None, from_json = item)) self.watchlist[key] = YahooAuctionsItem(None, from_json = self.jwatchlist[key], id = key)
self.jwatchlist = [] self.jwatchlist = {}
except: except:
raise raise
print("Couldn't load settings - using defaults") print("Couldn't load settings - using defaults")
def watch(self, url: str, name: str = None): def watch(self, url: str, name: str = None):
self.watchlist.append(YahooAuctionsItem(url, name)) id = functions.id_from_url(url)
for item in self.watchlist: self.watchlist[id] = YahooAuctionsItem(url, id, name)
for item in self.watchlist.values():
print(item.name, item.price) print(item.name, item.price)