From 06124b67777c912ef80be4ed352fe66a1f3233e9 Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Sat, 25 Nov 2017 16:26:48 +0200 Subject: cache specific program pages Cache just as before. Add a simple checksum of the program name (basically: crc32 of the title) to invalidate cache if numbering changes. --- default.py | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/default.py b/default.py index 3a647e7..db61881 100644 --- a/default.py +++ b/default.py @@ -18,6 +18,7 @@ import urlparse import xbmc import xbmcgui import xbmcplugin +import zlib KAN_URL = 'http://www.kan.org.il' @@ -67,12 +68,13 @@ class Page: .format(self.base_url, self.addon_handle, self.mode, self.content_type) - def build_page(self, page_list, isFolder=True): - """ Creates a complete page from a list of items (title, url_data) """ + def build_page(self, page_list, isFolder=False, isPlayable=False): + """ Creates a complete page from a list of items (title, url) """ for item in page_list: - title, url_data = item - url = self.build_url(url_data) + title, url = item li = xbmcgui.ListItem(title) + if isPlayable: + li.setProperty('IsPlayable','true') self.add_directory_item(url=url, listitem=li, isFolder=isFolder) self.end_directory() @@ -90,7 +92,7 @@ def read_url(url): SiteCache.set(cache_id, response.content, expiration=datetime.timedelta(days=1)) - return response.content + return response.text def get_show_title(base_url, path): @@ -102,13 +104,22 @@ def get_show_title(base_url, path): return title +def title_checksum(title): + """ A simple checksum to see that the title did not change + + Should only be good enough for the case that pages got renumbered + and the title no longer matches page number. + """ + return zlib.adler32(title.encode('utf-8')) + + def video_top_menu(page, name): """ Display a menu of all the TV shows """ trace("Show top menu for " + name) cache_id = PLUGIN_NAME + '.toppage.' + name cached_items = SiteCache.get(cache_id) if cached_items: - page.build_page(cached_items) + page.build_page(cached_items, isFolder=True) return trace("Show top menu for " + name + ": no cached copy") @@ -123,14 +134,24 @@ def video_top_menu(page, name): path = a.get('href') show_id = re.sub('.*=', '', path) title = get_show_title(KAN_URL, path) - items.append((title, {'mode': 'show', 'id': show_id})) - page.build_page(items) + checksum = title_checksum(title) + url = page.build_url({'mode': 'show', 'id': show_id, + 'checksum': str(checksum)}) + items.append((title, url)) + page.build_page(items, isFolder=True) SiteCache.set(cache_id, items, expiration=datetime.timedelta(days=1)) def show_menu(page): """ Display a menu of items in a specific show """ show_id = page.args['id'][0] + checksum = page.args['checksum'][0] + cache_id = PLUGIN_NAME + '.progpage.' + show_id + cached_items = SiteCache.get(cache_id, checksum=checksum) + if cached_items: + page.build_page(cached_items, isPlayable=True) + return + show_url = KAN_URL + '/Program/?catId=' + show_id trace("URL for show {}: {}".format(show_id, show_url)) @@ -139,6 +160,7 @@ def show_menu(page): items = parsed.find_all('li', class_="program_list_item w-clearfix") trace("got items: " + str(len(items))) + page_items = [] for item in items: titles = item.find_all('h2') title = titles[0].string @@ -147,10 +169,10 @@ def show_menu(page): youtube_id = re.sub('.*/embed/([0-9A-Za-z]+)(\?.*)?', r'\1', youtube_url) trace("Add link for ID {} ({}).".format(youtube_id, title.encode('utf-8'))) url = 'plugin://plugin.video.youtube/play/?video_id={}'.format(youtube_id) - li = xbmcgui.ListItem(title) - li.setProperty('IsPlayable','true') - page.add_directory_item(url=url, listitem=li, totalItems=len(items)) - page.end_directory() + page_items.append((title, url)) + page.build_page(page_items, isFolder=True) + SiteCache.set(cache_id, page_items, checksum=checksum, + expiration=datetime.timedelta(days=1)) def main_page(page): -- cgit v1.2.3