Cache set infos locally, increases startup performance

This commit is contained in:
luxick
2017-03-01 13:13:11 +01:00
parent b8717facc6
commit 0302da2288
3 changed files with 32 additions and 10 deletions

11
mtg-collector/network.py Normal file
View File

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

View File

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

View File

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