From 0302da228866c171b28338ad042b4060041fcefd Mon Sep 17 00:00:00 2001 From: luxick Date: Wed, 1 Mar 2017 13:13:11 +0100 Subject: [PATCH] Cache set infos locally, increases startup performance --- mtg-collector/network.py | 11 +++++++++++ mtg-collector/search.py | 6 +++--- mtg-collector/util.py | 25 ++++++++++++++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 mtg-collector/network.py diff --git a/mtg-collector/network.py b/mtg-collector/network.py new file mode 100644 index 0000000..ffd3cf7 --- /dev/null +++ b/mtg-collector/network.py @@ -0,0 +1,11 @@ +from urllib import request +from urllib.error import URLError, HTTPError +from mtgsdk import Set + + +def net_load_sets(): + try: + sets = Set.all() + except: + return "" + return sets diff --git a/mtg-collector/search.py b/mtg-collector/search.py index 4d77a9b..7d82ec3 100644 --- a/mtg-collector/search.py +++ b/mtg-collector/search.py @@ -1,4 +1,4 @@ -from urllib.error import URLError +from urllib.error import URLError, HTTPError import gi from gi.repository import Pango @@ -235,7 +235,7 @@ class SearchView(Gtk.Grid): .where(rarity=rarityfilter) \ .where(pageSize=50)\ .where(page=1).all() - except URLError as err: + except (URLError, HTTPError) as err: print("Error connecting to the internet") GObject.idle_add(util.show_message, "Connection Error", str(err.reason), priority=GObject.PRIORITY_DEFAULT) GObject.idle_add(self.do_activate_controls, True, priorty=GObject.PRIORITY_DEFAULT) @@ -338,7 +338,7 @@ class SearchView(Gtk.Grid): def do_activate_controls(self, active): self.searchEntry.set_editable(active) self.searchEntry.set_sensitive(active) - #self.searchbutton.set_sensitive(active) + self.searchbutton.set_sensitive(active) self.red_mana_button.set_sensitive(active) self.blue_mana_button.set_sensitive(active) self.black_mana_button.set_sensitive(active) diff --git a/mtg-collector/util.py b/mtg-collector/util.py index 9f1fb10..38ba7f2 100644 --- a/mtg-collector/util.py +++ b/mtg-collector/util.py @@ -1,13 +1,18 @@ +import json import os +import pickle + import gi import re + import config +import network gi.require_version('Gtk', '3.0') from gi.repository import GdkPixbuf, Gtk from PIL import Image as PImage from urllib import request from mtgsdk import Set -from urllib.error import URLError +from urllib.error import URLError, HTTPError # Loacally stored images for faster loading times imagecache = [] @@ -20,12 +25,18 @@ status_bar = None def load_sets(): - #setfile = os.open(config.cachepath + "/sets") - try: - sets = Set.all() - except URLError as err: - show_message("Connection Error", str(err.reason)) - return + path = config.cachepath + "sets" + if not os.path.isfile(path): + # use mtgsdk api to retrieve al list of all sets + new_sets = network.net_load_sets() + + if new_sets == "": + show_message("API Error", "Could not retrieve Set infos") + return + # Serialize the loaded data to a file + pickle.dump(new_sets, open(config.cachepath + "sets", 'wb')) + # Deserialize set data from local file + sets = pickle.load(open(config.cachepath + "sets", 'rb')) for set in sets: set_list.append(set)