From 6d74c223bc89a861dc1cdb071a7f1a9b07814c08 Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Fri, 24 Nov 2017 16:52:02 +0200 Subject: cache top program pages It seems that caching the downloaded pages is not useful enough. It only cuts the time of the programs page from 42 seconds to 25 seconds. So let's just cache the reuslting entries. --- default.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/default.py b/default.py index 460abf3..3a647e7 100644 --- a/default.py +++ b/default.py @@ -67,6 +67,15 @@ 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) """ + for item in page_list: + title, url_data = item + url = self.build_url(url_data) + li = xbmcgui.ListItem(title) + self.add_directory_item(url=url, listitem=li, isFolder=isFolder) + self.end_directory() + def read_url(url): cache_id = PLUGIN_NAME + '.url.' + url @@ -95,23 +104,28 @@ def get_show_title(base_url, path): 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) + return + + trace("Show top menu for " + name + ": no cached copy") tvshows_url = KAN_URL + "/video/{}.aspx".format(name) main_page = read_url(tvshows_url) parsed = BeautifulSoup(main_page, "html.parser") anchors = parsed.find_all('a', class_="program_category_link w-inline-block") trace("got anchors: " + str(len(anchors))) + items = [] for a in anchors: path = a.get('href') show_id = re.sub('.*=', '', path) - #trace("Checking link to " + path) title = get_show_title(KAN_URL, path) - url = page.build_url({'mode': 'show', 'id': show_id}) - trace("Item: URL: {}, title: {}.".format(url, title.encode('utf8'))) - li = xbmcgui.ListItem(title) - page.add_directory_item(url=url, listitem=li, isFolder=True, - totalItems=len(anchors)) - page.end_directory() + items.append((title, {'mode': 'show', 'id': show_id})) + page.build_page(items) + SiteCache.set(cache_id, items, expiration=datetime.timedelta(days=1)) def show_menu(page): @@ -130,7 +144,6 @@ def show_menu(page): title = titles[0].string iframe = item.find_all('iframe')[0] youtube_url = iframe['src'] - #trace("Youtube URL: {}.".format(youtube_url)) 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) @@ -173,6 +186,7 @@ def main(): page.placeholder_folder(name) else: trace("No handler for mode '{}'".format(mode)) + trace("Done showing page") if __name__ == '__main__': -- cgit v1.2.3