From 1d947c2c1ee79d13a436cbaaeb6e17775dba3095 Mon Sep 17 00:00:00 2001 From: Lynne Date: Fri, 21 Aug 2020 20:32:43 +1000 Subject: [PATCH] split stuff into files, added some gOOPy settings that's not really very good OOP, made it display the window --- buypeeb.py | 63 ++++++++++++++++++++++++++++++++++++++---------- functions.py | 38 +++++++++++++++++++++++++++++ listing.py | 2 +- requirements.txt | 1 + settings.py | 23 ++++++++++++++++++ 5 files changed, 113 insertions(+), 14 deletions(-) mode change 100644 => 100755 buypeeb.py create mode 100644 functions.py create mode 100644 settings.py diff --git a/buypeeb.py b/buypeeb.py old mode 100644 new mode 100755 index 41f4345..1a7626f --- a/buypeeb.py +++ b/buypeeb.py @@ -16,22 +16,59 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import requests +import requests, gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, Gio, Gdk -from listing import JPItem +import functions +from listing import YahooAuctionsItem +from settings import BuypeebSettings -import json +import json, sys +from os import path -def get_exchange_rate(): - try: - assert False - return requests.get("https://api.exchangeratesapi.io/latest?base=AUD&symbols=JPY", timeout=10).json()['rates']['JPY'] - except: - return 76.15 +isWindows = sys.platform.startswith('win') -print("Getting JPY/AUD exchange rate...") -rate = get_exchange_rate() -print(rate) +if isWindows: + settingsLocation = path.join(os.getenv("APPDATA"), "Lynnear Software", "bypeeb") +else: + settingsLocation = path.expanduser("~/.config/Lynnear Software/buypeeb/") # dotfiles in ~ need to die. begone dot -x = JPItem("My cool product", "peeb.us") +# print("Getting JPY/AUD exchange rate...") +rate = functions.get_exchange_rate() + +x = YahooAuctionsItem("My cool product", "peeb.us") print(f"Bidding for item \"{x.name}\" started at {x.start_date} and will end at {x.end_date}. Current price is {x.price_jpy()}, or {x.price_aud()}.") + +class BuypeebApp: + def __init__(self): + self.app = Gtk.Application.new('com.lynnearsoftware.buypeeb', 0) + self.app.connect('startup', self.startup) + self.app.connect('activate', self.activate) + self.app.connect('shutdown', self.shutdown) + self.window = None + self.settings = BuypeebSettings(settingsLocation) + + def run(self, argv): + self.app.run(argv) + + def startup(self, app): + builder = Gtk.Builder() + 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.window = wndMain + app.add_window(self.window) + + self.settings.load() + + def activate(self, app): + self.window.show_all() + + def shutdown(self, app): + self.settings.save() + self.app.quit() + +if __name__ == '__main__': + app = BuypeebApp() + app.run(sys.argv) diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..e9dd55b --- /dev/null +++ b/functions.py @@ -0,0 +1,38 @@ +import requests, gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, Gio, Gdk + +import re +from os import path + +def get_exchange_rate(): + try: + assert False + return requests.get("https://api.exchangeratesapi.io/latest?base=AUD&symbols=JPY", timeout=10).json()['rates']['JPY'] + except: + return 76.15 + +def msgBox(title, text, form = Gtk.MessageType.WARNING): + msgbox = Gtk.MessageDialog(app.window, 0, form, Gtk.ButtonsType.OK, title) + msgbox.format_secondary_text(text) + response = msgbox.run() + msgbox.destroy() + return response + +def rmatch(pattern, string): + return re.match(pattern, string) != None + +def readSettings(): + global settings + if not path.isfile(settingsLocation + "config.json"): + os.makedirs(settingsLocation, 0o755) + open(settingsLocation + "config.dat", 'x') + try: + settings = pickle.load(open(settingsLocation + "config.dat", 'rb')) + except: + MsgBox("Error", "Failed to load settings. The default settings will be used instead.") + settings = {"setting":"value", "save database": True} #indent me when releasing! + +def writeSettings(): + global settings + pickle.dump(settings, open(settingsLocation + "config.dat", 'wb')) diff --git a/listing.py b/listing.py index c22040f..e3df0c2 100644 --- a/listing.py +++ b/listing.py @@ -6,7 +6,7 @@ from datetime import datetime, timezone, timedelta JST = timezone(timedelta(hours = 9)) -class JPItem: +class YahooAuctionsItem: def __init__(self, name, url): self.name = name self.url = url diff --git a/requirements.txt b/requirements.txt index 49dc816..35aca7f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests==2.24 +PyGObject==3.36 diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..e2192f6 --- /dev/null +++ b/settings.py @@ -0,0 +1,23 @@ +import json, os +from os import path + +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.location = location + + def save(self): + json.dump(self.s, open(self.location + "config.json", 'w')) + + def load(self): + if not path.isfile(self.location + "config.json"): + os.makedirs(self.location, 0o755) + open(self.location + "config.json", 'x') + try: + self.s = json.load(open(self.location + "config.json", 'r')) + except: + print("Couldn't load settings - using defaults")