split stuff into files, added some gOOPy settings that's not really very good OOP, made it display the window

This commit is contained in:
Lynne Megido 2020-08-21 20:32:43 +10:00
parent 52f1741160
commit 1d947c2c1e
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
5 changed files with 113 additions and 14 deletions

63
buypeeb.py Normal file → Executable file
View file

@ -16,22 +16,59 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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)

38
functions.py Normal file
View file

@ -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'))

View file

@ -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

View file

@ -1 +1,2 @@
requests==2.24
PyGObject==3.36

23
settings.py Normal file
View file

@ -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")