Cache alot more so now it is usable

Fixes: #4
main
Micke Nordin 2 years ago
parent 165c98175a
commit a5a2a5abb8
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -58,7 +58,7 @@ class SVT(Channel):
if not resolved_link: if not resolved_link:
continue continue
thumbnail = make_bitmap_from_url( 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, item = Item(description, resolved_link, self.m_provider_name,
published_parsed, thumbnail, title) published_parsed, thumbnail, title)
self.m_items.append(item) self.m_items.append(item)
@ -90,7 +90,7 @@ class SVT(Channel):
if not resolved_link: if not resolved_link:
continue continue
thumbnail = make_bitmap_from_url( 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, item = Item(description,
resolved_link, resolved_link,
self.m_provider_name, self.m_provider_name,
@ -106,6 +106,8 @@ class SVT(Channel):
entries = get_svt_category(self.m_id) entries = get_svt_category(self.m_id)
for entry in entries: for entry in entries:
elem = entry["item"] elem = entry["item"]
url = elem["urls"]["svtplay"]
video_id = hash_string(url.split('/')[1])
svt_id = elem["videoSvtId"] svt_id = elem["videoSvtId"]
resolved_link = self.resolve_link(svt_id) resolved_link = self.resolve_link(svt_id)
if not resolved_link: if not resolved_link:
@ -118,7 +120,7 @@ class SVT(Channel):
elem['images']["wide"]["changed"], elem['images']["wide"]["changed"],
size=self.m_screen_width) size=self.m_screen_width)
thumbnail = make_bitmap_from_url( 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, item = Item(description, resolved_link, self.m_provider_name,
published_parsed, thumbnail, title) published_parsed, thumbnail, title)
self.m_items.append(item) self.m_items.append(item)

@ -22,6 +22,7 @@ BASEPATH = path.join(str(environ.get("HOME")), '.config/cast')
DB_FILE_NAME = 'cast.db' DB_FILE_NAME = 'cast.db'
SUB_TABLE = 'subscriptions' SUB_TABLE = 'subscriptions'
VIDEO_TABLE = 'videos' VIDEO_TABLE = 'videos'
USE_CACHED_CATEGORIES = False
def add_video(video_id: str, def add_video(video_id: str,
@ -76,7 +77,16 @@ def get_svt_thumb_from_id_changed(id: str,
size, id, changed) 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() categories: list = list()
url = "https://www.svtplay.se/kategori" url = "https://www.svtplay.se/kategori"
data = get_svt_data(url) data = get_svt_data(url)
@ -84,6 +94,9 @@ def get_all_svt_categories() -> list:
if 'genres' in entry.keys(): if 'genres' in entry.keys():
categories = entry['genres'] categories = entry['genres']
break break
with open(categoryfile, 'w') as jfile:
jfile.write(json.dumps(categories))
USE_CACHED_CATEGORIES = True
return categories return categories
@ -295,7 +308,14 @@ def make_sized_button(parent_pnl: wx.Panel, bitmap_or_str: Union[wx.Bitmap,
return btn_sizer 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) res = requests.get(logo_url)
content = res.content content = res.content
content_bytes = io.BytesIO(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() height = image.GetHeight()
size.SetHeight(int(height / scale_factor)) size.SetHeight(int(height / scale_factor))
image.Rescale(size.GetWidth(), size.GetHeight()) 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: def make_bitmap_from_file(path, size: wx.Size = SIZE) -> wx.Bitmap:

Loading…
Cancel
Save