149 lines
5.6 KiB
Python
149 lines
5.6 KiB
Python
import xmltodict
|
|
import base64
|
|
|
|
from random import randint
|
|
|
|
|
|
class OriginChannels():
|
|
|
|
def __init__(self, fhdhr, origin):
|
|
self.fhdhr = fhdhr
|
|
self.origin = origin
|
|
|
|
def get_channels(self):
|
|
|
|
stations_url = ('http://' + self.fhdhr.config.dict["origin"]["ceton_ip"] + '/view_channel_map.cgi?page=0&xml=1')
|
|
url_headers = {'accept': 'application/xml;q=0.9, */*;q=0.8'}
|
|
|
|
try:
|
|
stationsReq = self.fhdhr.web.session.get(stations_url, headers=url_headers)
|
|
stationsReq.raise_for_status()
|
|
except self.fhdhr.web.exceptions.HTTPError as err:
|
|
self.fhdhr.logger.error('Error while getting stations: %s' % err)
|
|
return []
|
|
|
|
stationsRes = xmltodict.parse(stationsReq.content)
|
|
cleaned_channels = []
|
|
|
|
for station_item in stationsRes['channels']['channel']:
|
|
nameTmp = station_item["name"]
|
|
nameTmp_bytes = nameTmp.encode('ascii')
|
|
namebytes = base64.b64decode(nameTmp_bytes)
|
|
name = namebytes.decode('ascii')
|
|
clean_station_item = {
|
|
"name": name,
|
|
"callsign": name,
|
|
"number": station_item["number"],
|
|
"eia": station_item["eia"],
|
|
"id": station_item["sourceid"],
|
|
}
|
|
|
|
cleaned_channels.append(clean_station_item)
|
|
return cleaned_channels
|
|
|
|
def get_ceton_getvar(self, instance, query):
|
|
getVarUrl = ('http://' + self.fhdhr.config.dict["origin"]["ceton_ip"] + '/get_var?i=' + str(instance) + query)
|
|
|
|
try:
|
|
getVarUrlReq = self.fhdhr.web.session.get(getVarUrl)
|
|
getVarUrlReq.raise_for_status()
|
|
except self.fhdhr.web.exceptions.HTTPError as err:
|
|
self.fhdhr.logger.error('Error while getting tuner variable for: %s - %s' % (query, err))
|
|
return None
|
|
|
|
return
|
|
|
|
def get_ceton_tuner_status(self, chandict):
|
|
found = 0
|
|
count = int(self.fhdhr.config.dict["fhdhr"]["tuner_count"])
|
|
for instance in range(count):
|
|
getVarUrl = ('http://' + self.fhdhr.config.dict["origin"]["ceton_ip"] + '/get_var?i=' + str(instance) + '&s=av&v=TransportState')
|
|
|
|
try:
|
|
getVarUrlReq = self.fhdhr.web.session.get(getVarUrl)
|
|
getVarUrlReq.raise_for_status()
|
|
except self.fhdhr.web.exceptions.HTTPError as err:
|
|
self.fhdhr.logger.error('Error while getting tuner status: %s' % err)
|
|
return None
|
|
|
|
if "STOPPED" in getVarUrlReq.text:
|
|
self.fhdhr.logger.info('Tuner %s selected' % str(instance))
|
|
found = 1
|
|
break
|
|
|
|
return found, instance
|
|
|
|
def startstop_ceton_tuner(self, instance, startstop):
|
|
if not startstop:
|
|
port = 0
|
|
self.fhdhr.logger.info('Tuner %s to be stopped' % str(instance))
|
|
else:
|
|
port = randint(41001, 49999)
|
|
self.fhdhr.logger.info('Tuner %s to be started' % str(instance))
|
|
|
|
StartStopUrl = ('http://%s/stream_request.cgi' % self.fhdhr.config.dict["origin"]["ceton_ip"])
|
|
|
|
StartStop_data = {"instance_id": instance,
|
|
"dest_ip": self.fhdhr.config.dict["fhdhr"]["address"],
|
|
"dest_port": port,
|
|
"protocol": 0,
|
|
"start": startstop}
|
|
# StartStopUrl_headers = {
|
|
# 'Content-Type': 'application/json',
|
|
# 'User-Agent': "curl/7.64.1"}
|
|
|
|
try:
|
|
StartStopUrlReq = self.fhdhr.web.session.post(StartStopUrl, StartStop_data)
|
|
StartStopUrlReq.raise_for_status()
|
|
except self.fhdhr.web.exceptions.HTTPError as err:
|
|
self.fhdhr.logger.error('Error while setting station stream: %s' % err)
|
|
return None
|
|
|
|
return port
|
|
|
|
def set_ceton_tuner(self, chandict, instance):
|
|
tuneChannelUrl = ('http://%s/channel_request.cgi' % self.fhdhr.config.dict["origin"]["ceton_ip"])
|
|
tuneChannel_data = {"instance_id": 0,
|
|
"channel": chandict['number']}
|
|
|
|
try:
|
|
tuneChannelUrlReq = self.fhdhr.web.session.post(tuneChannelUrl, tuneChannel_data)
|
|
tuneChannelUrlReq.raise_for_status()
|
|
except self.fhdhr.web.exceptions.HTTPError as err:
|
|
self.fhdhr.logger.error('Error while tuning station URL: %s' % err)
|
|
return None
|
|
|
|
return 1
|
|
|
|
def get_channel_stream(self, chandict):
|
|
# streamurl = "rtsp://admin:password1@199.254.167.199:554"
|
|
# print (streamurl)
|
|
# return streamurl
|
|
|
|
found, instance = self.get_ceton_tuner_status(chandict)
|
|
|
|
# 1 to start or 0 to stop
|
|
if found:
|
|
port = self.startstop_ceton_tuner(instance, 1)
|
|
self.fhdhr.logger.error('No tuners available')
|
|
else:
|
|
port = 0
|
|
|
|
if port:
|
|
tuned = self.set_ceton_tuner(chandict, instance)
|
|
self.fhdhr.logger.error('Preparing tuner ' + str(instance) + ' on port:' + str(port))
|
|
else:
|
|
tuned = 0
|
|
|
|
self.get_ceton_getvar(instance, "&s=tuner&v=Frequency")
|
|
self.get_ceton_getvar(instance, "&s=mux&v=ProgramNumber")
|
|
self.get_ceton_getvar(instance, "&s=diag&v=CopyProtectionStatus")
|
|
|
|
if tuned:
|
|
self.fhdhr.logger.error('Initiate streaming from tuner ' + str(instance))
|
|
streamurl = "udp://127.0.0.1:" + str(port)
|
|
else:
|
|
streamurl = None
|
|
|
|
return streamurl
|