From a5a2a5abb8f3693e69ab6a85039f7ef090481e7a Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Fri, 8 Jul 2022 15:39:37 +0200 Subject: [PATCH] Cache alot more so now it is usable Fixes: #4 --- src/Channel/SVT/__init__.py | 8 +++++--- src/Utils/__init__.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Channel/SVT/__init__.py b/src/Channel/SVT/__init__.py index 60c54c9..5422845 100644 --- a/src/Channel/SVT/__init__.py +++ b/src/Channel/SVT/__init__.py @@ -58,7 +58,7 @@ class SVT(Channel): if not resolved_link: continue thumbnail = make_bitmap_from_url( - thumbnail_link, wx.Size(int(self.m_screen_width), 150)) + thumbnail_link, wx.Size(int(self.m_screen_width), 150), video_id) item = Item(description, resolved_link, self.m_provider_name, published_parsed, thumbnail, title) self.m_items.append(item) @@ -90,7 +90,7 @@ class SVT(Channel): if not resolved_link: continue thumbnail = make_bitmap_from_url( - thumbnail_link, wx.Size(int(self.m_screen_width), 150)) + thumbnail_link, wx.Size(int(self.m_screen_width), 150), video_id) item = Item(description, resolved_link, self.m_provider_name, @@ -106,6 +106,8 @@ class SVT(Channel): entries = get_svt_category(self.m_id) for entry in entries: elem = entry["item"] + url = elem["urls"]["svtplay"] + video_id = hash_string(url.split('/')[1]) svt_id = elem["videoSvtId"] resolved_link = self.resolve_link(svt_id) if not resolved_link: @@ -118,7 +120,7 @@ class SVT(Channel): elem['images']["wide"]["changed"], size=self.m_screen_width) thumbnail = make_bitmap_from_url( - thumbnail_link, wx.Size(int(self.m_screen_width), 150)) + thumbnail_link, wx.Size(int(self.m_screen_width), 150), video_id) item = Item(description, resolved_link, self.m_provider_name, published_parsed, thumbnail, title) self.m_items.append(item) diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index 658b368..48720e8 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -22,6 +22,7 @@ BASEPATH = path.join(str(environ.get("HOME")), '.config/cast') DB_FILE_NAME = 'cast.db' SUB_TABLE = 'subscriptions' VIDEO_TABLE = 'videos' +USE_CACHED_CATEGORIES = False def add_video(video_id: str, @@ -76,7 +77,16 @@ def get_svt_thumb_from_id_changed(id: str, size, id, changed) -def get_all_svt_categories() -> list: +def get_all_svt_categories(basepath=BASEPATH) -> list: + global USE_CACHED_CATEGORIES + categoryfile = path.join(basepath,"categories.json") + if USE_CACHED_CATEGORIES: + if path.isfile(categoryfile): + with open(categoryfile, 'r') as jfile: + return(json.loads(jfile.read())) + + + categories: list = list() url = "https://www.svtplay.se/kategori" data = get_svt_data(url) @@ -84,6 +94,9 @@ def get_all_svt_categories() -> list: if 'genres' in entry.keys(): categories = entry['genres'] break + with open(categoryfile, 'w') as jfile: + jfile.write(json.dumps(categories)) + USE_CACHED_CATEGORIES = True return categories @@ -295,7 +308,14 @@ def make_sized_button(parent_pnl: wx.Panel, bitmap_or_str: Union[wx.Bitmap, return btn_sizer -def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE) -> wx.Bitmap: +def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE, video_id: str = "") -> wx.Bitmap: + if not video_id: + video_id = hash_string(logo_url) + thumbpath = path.join(BASEPATH, 'thumbnails') + thumbnail = path.join(thumbpath, video_id) + if path.isfile(thumbnail): + return make_bitmap_from_file(thumbnail, size) + res = requests.get(logo_url) content = res.content content_bytes = io.BytesIO(content) @@ -305,7 +325,12 @@ def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE) -> wx.Bitmap: height = image.GetHeight() size.SetHeight(int(height / scale_factor)) image.Rescale(size.GetWidth(), size.GetHeight()) - return wx.Bitmap(image) + bitmap = wx.Bitmap(image) + if not path.isdir(thumbpath): + makedirs(thumbpath) + if not path.isfile(thumbnail): + bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG) + return bitmap def make_bitmap_from_file(path, size: wx.Size = SIZE) -> wx.Bitmap: