From ca28a4dc127ffdcebb938a074012bf7006b7d21c Mon Sep 17 00:00:00 2001 From: luxick Date: Tue, 14 Feb 2017 14:46:28 +0100 Subject: [PATCH] Display card images and local image caching for faster loading times --- config.py | 5 +++++ gui.py | 8 ++++++-- search.py | 24 ++++++++++++++---------- util.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 config.py create mode 100644 util.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..d5f5e6e --- /dev/null +++ b/config.py @@ -0,0 +1,5 @@ +# Title of the Program Window +applicationtitle="MTG Collector (working title) v0.1" + +# Path of image cache +cachepath=".cache/" \ No newline at end of file diff --git a/gui.py b/gui.py index eeb6af6..d7d0716 100644 --- a/gui.py +++ b/gui.py @@ -1,14 +1,15 @@ import gi import collection import search - +import config +import util gi.require_version('Gtk', '3.0') from gi.repository import Gtk class MainWindow(Gtk.Window): def __init__(self): - Gtk.Window.__init__(self, title="MTG Collector (working title) v0.1") + Gtk.Window.__init__(self, title=config.applicationtitle) self.set_border_width(2) self.set_size_request(1000, 700) @@ -30,7 +31,10 @@ class MainWindow(Gtk.Window): + win = MainWindow() +# Load local image Data +util.imagecache = util.reload_image_cache() win.connect('delete-event', Gtk.main_quit) win.show_all() Gtk.main() diff --git a/search.py b/search.py index 8ffb2b3..c71bc97 100644 --- a/search.py +++ b/search.py @@ -1,7 +1,6 @@ import gi from gi.repository import Pango -from psutil._compat import xrange - +import util gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GdkPixbuf from mtgsdk import Card @@ -71,14 +70,19 @@ class SearchView(Gtk.Grid): def online_search_clicked(self, button): term = self.searchEntry.get_text() - print("Search for \"" + term + "\" online.") + if not term == "": + print("Search for \"" + term + "\" online.") + + cards = Card.where(name=term).all() + self.store.clear() + for card in cards: + if card.multiverse_id is not None: + print("Found ID: " + card.multiverse_id.__str__() + " | " + card.name) + self.store.append([util.load_card_image(card), + card.name, + card.original_text]) + util.reload_image_cache() + - cards = Card.where(name=term).all() - self.store.clear() - for card in cards: - self.store.append([self.add_test_image(), card.name, card.original_text]) - print("Found: " + card.name) - def add_test_image(self): - return GdkPixbuf.Pixbuf.new_from_file_at_size('./resources/images/demo.jpg', 63*2, 88*2) diff --git a/util.py b/util.py new file mode 100644 index 0000000..b886b3f --- /dev/null +++ b/util.py @@ -0,0 +1,52 @@ +import os +import gi + +import config + +gi.require_version('Gtk', '3.0') +from gi.repository import GdkPixbuf +from PIL import Image as PImage +from urllib import request + +# Loacally stored images for faster loading times +imagecache = [] + + +def reload_image_cache(): + if not os.path.exists(config.cachepath): + os.makedirs(config.cachepath) + + # return array of images + imageslist = os.listdir(config.cachepath) + loadedimages = [] + for image in imageslist: + img = PImage.open(config.cachepath + image) + loadedimages.append(img) + return loadedimages + + +def add_test_image(): + return GdkPixbuf.Pixbuf.new_from_file_at_size('./resources/images/demo.jpg', 63 * 2, 88 * 2) + + +def load_card_image_online(card): + url = card.image_url + if url is None: + print("No Image URL provided") + return add_test_image() + filename = ".cache/" + card.multiverse_id.__str__() + ".PNG" + print("Loading image from: " + url) + response = request.urlretrieve(url, filename) + return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, 63 * 2, 88 * 2) + + +def load_card_image(card): + # Try loading from disk, if file exists + for image in imagecache: + filename = os.path.basename(image.filename) + if filename == card.multiverse_id.__str__() + ".PNG": + print("Using local file: " + filename) + return GdkPixbuf.Pixbuf.new_from_file_at_size(image.filename, 63 * 2, 88 * 2) + + # No file in local cache found + return load_card_image_online(card)