From 39649a11f811d93ec8fd557f5ae73194ca5a1141 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 1 Jan 2021 18:05:36 -0500 Subject: [PATCH] Enhance Channel Loading --- fHDHR/device/channels/__init__.py | 88 ++++++++++----------- fHDHR/device/epg/__init__.py | 2 +- fHDHR/device/epg/blocks.py | 2 +- fHDHR_web/api/channels.py | 2 +- fHDHR_web/api/m3u.py | 2 +- fHDHR_web/hdhr/lineup_json.py | 2 +- fHDHR_web/hdhr/lineup_xml.py | 2 +- fHDHR_web/pages/channels_editor.py | 2 +- fHDHR_web/pages/channels_html.py | 6 +- fHDHR_web/rmg/devices_devicekey_channels.py | 2 +- 10 files changed, 53 insertions(+), 57 deletions(-) diff --git a/fHDHR/device/channels/__init__.py b/fHDHR/device/channels/__init__.py index 09d8d20..ff12fad 100644 --- a/fHDHR/device/channels/__init__.py +++ b/fHDHR/device/channels/__init__.py @@ -1,7 +1,6 @@ -import datetime import time -from fHDHR.tools import hours_between_datetime, humanized_time +from fHDHR.tools import humanized_time from .channel import Channel from .chan_ident import Channel_IDs @@ -17,27 +16,26 @@ class Channels(): self.id_system = Channel_IDs(fhdhr) self.list = {} - self.list_update_time = None self.get_db_channels() def get_channel_obj(self, keyfind, valfind): if keyfind == "number": - return next(self.list[fhdhr_id] for fhdhr_id in list(self.list.keys()) if self.list[fhdhr_id].number == valfind) or None + return next(self.list[fhdhr_id] for fhdhr_id in [x["id"] for x in self.get_channels()] if self.list[fhdhr_id].number == valfind) or None else: - return next(self.list[fhdhr_id] for fhdhr_id in list(self.list.keys()) if self.list[fhdhr_id].dict[keyfind] == valfind) or None + return next(self.list[fhdhr_id] for fhdhr_id in [x["id"] for x in self.get_channels()] if self.list[fhdhr_id].dict[keyfind] == valfind) or None def get_channel_list(self, keyfind): if keyfind == "number": - return [self.list[x].number for x in list(self.list.keys())] + return [self.list[x].number for x in [x["id"] for x in self.get_channels()]] else: - return [self.list[x].dict[keyfind] for x in list(self.list.keys())] + return [self.list[x].dict[keyfind] for x in [x["id"] for x in self.get_channels()]] def set_channel_status(self, keyfind, valfind, updatedict): self.get_channel_obj(keyfind, valfind).set_status(updatedict) def set_channel_enablement_all(self, enablement): - for fhdhr_id in list(self.list.keys()): + for fhdhr_id in [x["id"] for x in self.get_channels()]: self.list[fhdhr_id].set_enablement(enablement) def set_channel_enablement(self, keyfind, valfind, enablement): @@ -56,6 +54,10 @@ class Channels(): channel_id = channel_obj.dict["id"] self.list[channel_id] = channel_obj + def save_db_channels(self): + channel_ids = [x["id"] for x in self.get_channels()] + self.fhdhr.db.set_fhdhr_value("channels", "list", channel_ids) + def get_channels(self, forceupdate=False): """Pull Channels from origin. @@ -64,56 +66,50 @@ class Channels(): Don't pull more often than 12 hours. """ - updatelist = False - if not self.list_update_time: - updatelist = True - elif hours_between_datetime(self.list_update_time, datetime.datetime.now()) > 12: - updatelist = True - elif forceupdate: - updatelist = True + if not len(list(self.list.keys())): + self.get_db_channels() - if updatelist: - channel_origin_id_list = [str(self.list[x].dict["origin_id"]) for x in list(self.list.keys())] + if not forceupdate: + return [self.list[x].dict for x in list(self.list.keys())] - self.fhdhr.logger.info("Performing Channel Scan.") + channel_origin_id_list = [str(self.list[x].dict["origin_id"]) for x in list(self.list.keys())] - channel_dict_list = self.origin.get_channels() - self.fhdhr.logger.info("Found %s channels for %s." % (len(channel_dict_list), self.fhdhr.config.dict["main"]["servicename"])) + self.fhdhr.logger.info("Performing Channel Scan.") - self.fhdhr.logger.info("Performing Channel Import, This can take some time, Please wait.") + channel_dict_list = self.origin.get_channels() + self.fhdhr.logger.info("Found %s channels for %s." % (len(channel_dict_list), self.fhdhr.config.dict["main"]["servicename"])) - newchan = 0 - chan_scan_start = time.time() - for channel_info in channel_dict_list: + self.fhdhr.logger.info("Performing Channel Import, This can take some time, Please wait.") - chan_existing = False - if str(channel_info["id"]) in channel_origin_id_list: - chan_existing = True - channel_obj = self.get_channel_obj("origin_id", channel_info["id"]) - else: - channel_obj = Channel(self.fhdhr, self.id_system, origin_id=channel_info["id"]) + newchan = 0 + chan_scan_start = time.time() + for channel_info in channel_dict_list: - channel_id = channel_obj.dict["id"] - channel_obj.basics(channel_info) - if not chan_existing: - self.list[channel_id] = channel_obj - newchan += 1 + chan_existing = str(channel_info["id"]) in channel_origin_id_list - self.fhdhr.logger.info("Channel Import took %s" % humanized_time(time.time() - chan_scan_start)) + if chan_existing: + channel_obj = self.get_channel_obj("origin_id", channel_info["id"]) + else: + channel_obj = Channel(self.fhdhr, self.id_system, origin_id=channel_info["id"]) - if not newchan: - newchan = "no" - self.fhdhr.logger.info("Found %s NEW channels." % newchan) + channel_id = channel_obj.dict["id"] + channel_obj.basics(channel_info) + if not chan_existing: + self.list[channel_id] = channel_obj + newchan += 1 - self.fhdhr.logger.info("Total Channel Count: %s" % len(self.list.keys())) + self.fhdhr.logger.info("Channel Import took %s" % humanized_time(time.time() - chan_scan_start)) - self.list_update_time = datetime.datetime.now() - self.fhdhr.db.set_fhdhr_value("channels", "scanned_time", time.time()) + if not newchan: + newchan = "no" + self.fhdhr.logger.info("Found %s NEW channels." % newchan) - channel_list = [] - for chan_obj in list(self.list.keys()): - channel_list.append(self.list[chan_obj].dict) - return channel_list + self.fhdhr.logger.info("Total Channel Count: %s" % len(self.list.keys())) + self.save_db_channels() + + self.fhdhr.db.set_fhdhr_value("channels", "scanned_time", time.time()) + + return [self.list[x].dict for x in list(self.list.keys())] def get_channel_stream(self, channel_number): return self.origin.get_channel_stream(self.get_channel_dict("number", channel_number)) diff --git a/fHDHR/device/epg/__init__.py b/fHDHR/device/epg/__init__.py index d0c623f..cf69470 100644 --- a/fHDHR/device/epg/__init__.py +++ b/fHDHR/device/epg/__init__.py @@ -271,7 +271,7 @@ class EPG(): # if a stock method, generate Blocks EPG for missing channels if method in ["blocks", "origin", self.fhdhr.config.dict["main"]["dictpopname"]]: timestamps = self.blocks.timestamps - for fhdhr_id in list(self.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: chan_obj = self.channels.list[fhdhr_id] if str(chan_obj.number) not in list(programguide.keys()): programguide[str(chan_obj.number)] = chan_obj.epgdict diff --git a/fHDHR/device/epg/blocks.py b/fHDHR/device/epg/blocks.py index ff4a390..a938595 100644 --- a/fHDHR/device/epg/blocks.py +++ b/fHDHR/device/epg/blocks.py @@ -13,7 +13,7 @@ class blocksEPG(): timestamps = self.timestamps - for fhdhr_id in list(self.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: chan_obj = self.channels.list[fhdhr_id] if str(chan_obj.number) not in list(programguide.keys()): diff --git a/fHDHR_web/api/channels.py b/fHDHR_web/api/channels.py index 66d9733..4b8813e 100644 --- a/fHDHR_web/api/channels.py +++ b/fHDHR_web/api/channels.py @@ -21,7 +21,7 @@ class Channels(): if method == "get": channels_info = [] - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] channel_dict = channel_obj.dict.copy() channel_dict["play_url"] = channel_obj.play_url diff --git a/fHDHR_web/api/m3u.py b/fHDHR_web/api/m3u.py index 9211efd..5c87468 100644 --- a/fHDHR_web/api/m3u.py +++ b/fHDHR_web/api/m3u.py @@ -42,7 +42,7 @@ class M3U(): if channel == "all": fileName = "channels.m3u" - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] if channel_obj.enabled: channel_items.append(channel_obj) diff --git a/fHDHR_web/hdhr/lineup_json.py b/fHDHR_web/hdhr/lineup_json.py index 1277cf6..fae562d 100644 --- a/fHDHR_web/hdhr/lineup_json.py +++ b/fHDHR_web/hdhr/lineup_json.py @@ -19,7 +19,7 @@ class Lineup_JSON(): show = request.args.get('show', default="all", type=str) jsonlineup = [] - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] if channel_obj.enabled or show == "found": lineup_dict = channel_obj.lineup_dict diff --git a/fHDHR_web/hdhr/lineup_xml.py b/fHDHR_web/hdhr/lineup_xml.py index 198cb0d..e52d9a3 100644 --- a/fHDHR_web/hdhr/lineup_xml.py +++ b/fHDHR_web/hdhr/lineup_xml.py @@ -22,7 +22,7 @@ class Lineup_XML(): show = request.args.get('show', default="all", type=str) out = xml.etree.ElementTree.Element('Lineup') - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] if channel_obj.enabled or show == "found": program_out = sub_el(out, 'Program') diff --git a/fHDHR_web/pages/channels_editor.py b/fHDHR_web/pages/channels_editor.py index 4f3549f..ea8906f 100644 --- a/fHDHR_web/pages/channels_editor.py +++ b/fHDHR_web/pages/channels_editor.py @@ -14,7 +14,7 @@ class Channels_Editor_HTML(): def get(self, *args): channelslist = [] - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] channel_dict = channel_obj.dict.copy() diff --git a/fHDHR_web/pages/channels_html.py b/fHDHR_web/pages/channels_html.py index 8525e86..13de699 100644 --- a/fHDHR_web/pages/channels_html.py +++ b/fHDHR_web/pages/channels_html.py @@ -14,12 +14,12 @@ class Channels_HTML(): def get(self, *args): channels_dict = { - "Total Channels": len(list(self.fhdhr.device.channels.list.keys())), - "Enabled": 0, + "Total Channels": len(self.fhdhr.device.channels.get_channels()), + "Enabled": 0 } channelslist = [] - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] channel_dict = channel_obj.dict.copy() diff --git a/fHDHR_web/rmg/devices_devicekey_channels.py b/fHDHR_web/rmg/devices_devicekey_channels.py index 93d98a0..c2c6f40 100644 --- a/fHDHR_web/rmg/devices_devicekey_channels.py +++ b/fHDHR_web/rmg/devices_devicekey_channels.py @@ -22,7 +22,7 @@ class RMG_Devices_DeviceKey_Channels(): out = xml.etree.ElementTree.Element('MediaContainer') if devicekey == self.fhdhr.config.dict["main"]["uuid"]: out.set('size', str(len(self.fhdhr.device.channels.list))) - for fhdhr_id in list(self.fhdhr.device.channels.list.keys()): + for fhdhr_id in [x["id"] for x in self.fhdhr.device.channels.get_channels()]: channel_obj = self.fhdhr.device.channels.list[fhdhr_id] if channel_obj.enabled: sub_el(out, 'Channel',