From e00cec1ed674af6f7a0d03497c9175c4912c5401 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 10 Dec 2020 09:41:45 -0500 Subject: [PATCH] Move Origin Web Page and Create API framework --- fHDHR/config/__init__.py | 4 +- fHDHR/device/cluster.py | 2 +- fHDHR/originwrapper/__init__.py | 42 ++----------------- .../originwrapper/origin_channels_standin.py | 11 +++++ fHDHR/originwrapper/origin_epg_standin.py | 8 ++++ fHDHR_web/__init__.py | 6 ++- fHDHR_web/pages/__init__.py | 2 - fHDHR_web/pages/origin_html.py | 18 -------- origin/__init__.py | 3 +- origin/origin_service.py | 33 ++++----------- origin/origin_web/__init__.py | 12 ++++++ .../origin_web}/origin.html | 0 origin/origin_web/origin_api.py | 16 +++++++ origin/origin_web/origin_html.py | 30 +++++++++++++ 14 files changed, 100 insertions(+), 87 deletions(-) create mode 100644 fHDHR/originwrapper/origin_channels_standin.py create mode 100644 fHDHR/originwrapper/origin_epg_standin.py delete mode 100644 fHDHR_web/pages/origin_html.py create mode 100644 origin/origin_web/__init__.py rename {fHDHR_web/templates => origin/origin_web}/origin.html (100%) create mode 100644 origin/origin_web/origin_api.py create mode 100644 origin/origin_web/origin_html.py diff --git a/fHDHR/config/__init__.py b/fHDHR/config/__init__.py index 1921cf7..3891d66 100644 --- a/fHDHR/config/__init__.py +++ b/fHDHR/config/__init__.py @@ -32,12 +32,14 @@ class Config(): data_dir = pathlib.Path(script_dir).joinpath('data') fHDHR_web_dir = pathlib.Path(script_dir).joinpath('fHDHR_web') www_dir = pathlib.Path(fHDHR_web_dir).joinpath('www_dir') + origin_dir = pathlib.Path(script_dir).joinpath('origin') self.internal["paths"] = { "script_dir": script_dir, "data_dir": data_dir, "alternative_epg": pathlib.Path(script_dir).joinpath('alternative_epg'), - "origin": pathlib.Path(script_dir).joinpath('origin'), + "origin": origin_dir, + "origin_web": pathlib.Path(origin_dir).joinpath('origin_web'), "cache_dir": pathlib.Path(data_dir).joinpath('cache'), "internal_config": pathlib.Path(data_dir).joinpath('internal_config'), "www_dir": www_dir, diff --git a/fHDHR/device/cluster.py b/fHDHR/device/cluster.py index 4d5a5ab..c9e7dfc 100644 --- a/fHDHR/device/cluster.py +++ b/fHDHR/device/cluster.py @@ -67,7 +67,7 @@ class fHDHR_Cluster(): self.fhdhr.logger.info("Found %s clustered services." % str(len(list(cluster.keys())))) for location in list(cluster.keys()): if location != self.fhdhr.api.base: - self.fhdhr.logger.info("Checking Cluster Syncronization information from %s." % location) + self.fhdhr.logger.debug("Checking Cluster Syncronization information from %s." % location) sync_url = location + "/api/cluster?method=get" try: sync_open = self.fhdhr.web.session.get(sync_url) diff --git a/fHDHR/originwrapper/__init__.py b/fHDHR/originwrapper/__init__.py index 58efe77..2e59e70 100644 --- a/fHDHR/originwrapper/__init__.py +++ b/fHDHR/originwrapper/__init__.py @@ -1,27 +1,9 @@ - +from .origin_channels_standin import OriginChannels_StandIN +from .origin_epg_standin import OriginEPG_StandIN import fHDHR.exceptions -class OriginEPG_StandIN(): - def __init__(self): - pass - - def update_epg(self, channels): - return {} - - -class OriginChannels_StandIN(): - def __init__(self): - pass - - def get_channels(self): - return [] - - def get_channel_stream(self, chandict): - return None - - class OriginServiceWrapper(): def __init__(self, fhdhr, origin): @@ -40,6 +22,7 @@ class OriginServiceWrapper(): self.setup_success = True self.fhdhr.logger.info("%s Setup Success" % self.servicename) except fHDHR.exceptions.OriginSetupError as e: + self.originservice = None self.fhdhr.logger.error(e) self.setup_success = False @@ -59,25 +42,6 @@ class OriginServiceWrapper(): def update_epg(self, channels): return self.epg.update_epg(channels) - def get_status_dict(self): - - if self.setup_success: - status_dict = { - "Setup": "Success", - } - - try: - full_status_dict = self.origin.get_status_dict() - for status_key in list(full_status_dict.keys()): - status_dict[status_key] = full_status_dict[status_key] - return status_dict - except AttributeError: - return status_dict - else: - return { - "Setup": "Failed", - } - def __getattr__(self, name): ''' will only get called for undefined attributes ''' if hasattr(self.fhdhr, name): diff --git a/fHDHR/originwrapper/origin_channels_standin.py b/fHDHR/originwrapper/origin_channels_standin.py new file mode 100644 index 0000000..f5dc99f --- /dev/null +++ b/fHDHR/originwrapper/origin_channels_standin.py @@ -0,0 +1,11 @@ + + +class OriginChannels_StandIN(): + def __init__(self): + pass + + def get_channels(self): + return [] + + def get_channel_stream(self, chandict): + return None diff --git a/fHDHR/originwrapper/origin_epg_standin.py b/fHDHR/originwrapper/origin_epg_standin.py new file mode 100644 index 0000000..a0830a6 --- /dev/null +++ b/fHDHR/originwrapper/origin_epg_standin.py @@ -0,0 +1,8 @@ + + +class OriginEPG_StandIN(): + def __init__(self): + pass + + def update_epg(self, channels): + return {} diff --git a/fHDHR_web/__init__.py b/fHDHR_web/__init__.py index 7b01fdb..4371ba1 100644 --- a/fHDHR_web/__init__.py +++ b/fHDHR_web/__init__.py @@ -43,6 +43,10 @@ class fHDHR_HTTP_Server(): self.api = fHDHR_API(fhdhr) self.add_endpoints(self.api, "api") + self.fhdhr.logger.info("Loading HTTP Origin Endpoints.") + self.origin_endpoints = self.fhdhr.originwrapper.origin.origin_web.fHDHR_Origin_Web(fhdhr) + self.add_endpoints(self.origin_endpoints, "origin_endpoints") + self.app.before_request(self.before_request) self.app.after_request(self.after_request) self.app.before_first_request(self.before_first_request) @@ -69,7 +73,7 @@ class fHDHR_HTTP_Server(): endpoint_methods = eval("self." + str(index_name) + "." + str(item) + ".endpoint_methods") except AttributeError: endpoint_methods = ['GET'] - self.fhdhr.logger.info("Adding endpoint %s available at %s with %s methods." % (endpoint_name, ",".join(endpoints), ",".join(endpoint_methods))) + self.fhdhr.logger.debug("Adding endpoint %s available at %s with %s methods." % (endpoint_name, ",".join(endpoints), ",".join(endpoint_methods))) for endpoint in endpoints: self.add_endpoint(endpoint=endpoint, endpoint_name=endpoint_name, diff --git a/fHDHR_web/pages/__init__.py b/fHDHR_web/pages/__init__.py index 95d6eea..f0c4acd 100644 --- a/fHDHR_web/pages/__init__.py +++ b/fHDHR_web/pages/__init__.py @@ -1,7 +1,6 @@ from .index_html import Index_HTML -from .origin_html import Origin_HTML from .channels_html import Channels_HTML from .guide_html import Guide_HTML from .cluster_html import Cluster_HTML @@ -19,7 +18,6 @@ class fHDHR_Pages(): self.fhdhr = fhdhr self.index_html = Index_HTML(fhdhr) - self.origin_html = Origin_HTML(fhdhr) self.channels_html = Channels_HTML(fhdhr) self.channels_editor = Channels_Editor_HTML(fhdhr) self.guide_html = Guide_HTML(fhdhr) diff --git a/fHDHR_web/pages/origin_html.py b/fHDHR_web/pages/origin_html.py deleted file mode 100644 index 998f847..0000000 --- a/fHDHR_web/pages/origin_html.py +++ /dev/null @@ -1,18 +0,0 @@ -from flask import request, render_template - - -class Origin_HTML(): - endpoints = ["/origin", "/origin.html"] - endpoint_name = "page_origin_html" - - def __init__(self, fhdhr): - self.fhdhr = fhdhr - - def __call__(self, *args): - return self.get(*args) - - def get(self, *args): - - origin_status_dict = self.fhdhr.originwrapper.get_status_dict() - origin_status_dict["Total Channels"] = len(self.fhdhr.device.channels.list) - return render_template('origin.html', request=request, fhdhr=self.fhdhr, origin_status_dict=origin_status_dict, list=list) diff --git a/origin/__init__.py b/origin/__init__.py index 6f01e4f..6014624 100644 --- a/origin/__init__.py +++ b/origin/__init__.py @@ -2,6 +2,7 @@ from .origin_service import * from .origin_channels import * from .origin_epg import * +from .origin_web import * -ORIGIN_NAME = "fHDHR_NextPVR" +ORIGIN_NAME = "fHDHR_Locast" ORIGIN_VERSION = "v0.5.0-beta" diff --git a/origin/origin_service.py b/origin/origin_service.py index 8769f10..ac222e0 100644 --- a/origin/origin_service.py +++ b/origin/origin_service.py @@ -10,6 +10,12 @@ class OriginService(): def __init__(self, fhdhr): self.fhdhr = fhdhr + self.nextpvr_address = ('%s%s:%s' % + ("https://" if self.fhdhr.config.dict["origin"]["ssl"] else "http://", + self.fhdhr.config.dict["origin"]["address"], + str(self.fhdhr.config.dict["origin"]["port"]), + )) + self.login() def login(self): @@ -25,11 +31,7 @@ class OriginService(): if self.fhdhr.config.dict["origin"]["sid"]: return self.fhdhr.config.dict["origin"]["sid"] - initiate_url = ('%s%s:%s/service?method=session.initiate&ver=1.0&device=fhdhr' % - ("https://" if self.fhdhr.config.dict["origin"]["ssl"] else "http://", - self.fhdhr.config.dict["origin"]["address"], - str(self.fhdhr.config.dict["origin"]["port"]), - )) + initiate_url = '%s/service?method=session.initiate&ver=1.0&device=fhdhr' % self.nextpvr_address initiate_req = self.fhdhr.web.session.get(initiate_url) initiate_dict = xmltodict.parse(initiate_req.content) @@ -40,13 +42,8 @@ class OriginService(): string = ':%s:%s' % (md5PIN, salt) clientKey = hashlib.md5(string.encode('utf-8')).hexdigest() - login_url = ('%s%s:%s/service?method=session.login&sid=%s&md5=%s' % - ("https://" if self.fhdhr.config.dict["origin"]["ssl"] else "http://", - self.fhdhr.config.dict["origin"]["address"], - str(self.fhdhr.config.dict["origin"]["port"]), - sid, - clientKey - )) + login_url = ('%s/service?method=session.login&sid=%s&md5=%s' % + (self.nextpvr_address, sid, clientKey)) login_req = self.fhdhr.web.session.get(login_url) login_dict = xmltodict.parse(login_req.content) @@ -56,15 +53,3 @@ class OriginService(): loginsuccess = sid return loginsuccess - - def get_status_dict(self): - nextpvr_address = ('%s%s:%s' % - ("https://" if self.fhdhr.config.dict["origin"]["ssl"] else "http://", - self.fhdhr.config.dict["origin"]["address"], - str(self.fhdhr.config.dict["origin"]["port"]), - )) - ret_status_dict = { - "Login": "Success", - "Address": nextpvr_address, - } - return ret_status_dict diff --git a/origin/origin_web/__init__.py b/origin/origin_web/__init__.py new file mode 100644 index 0000000..4b6008e --- /dev/null +++ b/origin/origin_web/__init__.py @@ -0,0 +1,12 @@ + +from .origin_api import Origin_API +from .origin_html import Origin_HTML + + +class fHDHR_Origin_Web(): + + def __init__(self, fhdhr): + self.fhdhr = fhdhr + + self.origin_api = Origin_API(fhdhr) + self.origin_html = Origin_HTML(fhdhr) diff --git a/fHDHR_web/templates/origin.html b/origin/origin_web/origin.html similarity index 100% rename from fHDHR_web/templates/origin.html rename to origin/origin_web/origin.html diff --git a/origin/origin_web/origin_api.py b/origin/origin_web/origin_api.py new file mode 100644 index 0000000..a23494e --- /dev/null +++ b/origin/origin_web/origin_api.py @@ -0,0 +1,16 @@ + + +class Origin_API(): + endpoints = ["/api/origin"] + endpoint_name = "api_origin" + endpoint_methods = ["GET", "POST"] + + def __init__(self, fhdhr): + self.fhdhr = fhdhr + + def __call__(self, *args): + return self.get(*args) + + def get(self, *args): + + return "Success" diff --git a/origin/origin_web/origin_html.py b/origin/origin_web/origin_html.py new file mode 100644 index 0000000..c6e9632 --- /dev/null +++ b/origin/origin_web/origin_html.py @@ -0,0 +1,30 @@ +from flask import request, render_template_string +import pathlib +from io import StringIO + + +class Origin_HTML(): + endpoints = ["/origin", "/origin.html"] + endpoint_name = "page_origin_html" + + def __init__(self, fhdhr): + self.fhdhr = fhdhr + + self.template_file = pathlib.Path(self.fhdhr.config.internal["paths"]["origin_web"]).joinpath('origin.html') + self.template = StringIO() + self.template.write(open(self.template_file).read()) + + def __call__(self, *args): + return self.get(*args) + + def get(self, *args): + + if self.fhdhr.originwrapper.setup_success: + origin_status_dict = { + "Setup": "Success", + "Address": self.fhdhr.originwrapper.originservice.nextpvr_address, + "Total Channels": len(self.fhdhr.device.channels.list) + } + else: + origin_status_dict = {"Setup": "Failed"} + return render_template_string(self.template.getvalue(), request=request, fhdhr=self.fhdhr, origin_status_dict=origin_status_dict, list=list)