1
0
mirror of https://github.com/fHDHR/fHDHR_NextPVR.git synced 2025-12-06 10:16:58 -05:00

Move Origin Web Page and Create API framework

This commit is contained in:
deathbybandaid 2020-12-10 09:41:45 -05:00
parent 8b870430ad
commit e00cec1ed6
14 changed files with 100 additions and 87 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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):

View File

@ -0,0 +1,11 @@
class OriginChannels_StandIN():
def __init__(self):
pass
def get_channels(self):
return []
def get_channel_stream(self, chandict):
return None

View File

@ -0,0 +1,8 @@
class OriginEPG_StandIN():
def __init__(self):
pass
def update_epg(self, channels):
return {}

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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)

View File

@ -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"

View File

@ -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)