1
0
mirror of https://github.com/fHDHR/fHDHR_NextPVR.git synced 2025-12-06 09:36:59 -05:00

Reconfigure /watch to /auto

This commit is contained in:
deathbybandaid 2020-10-12 13:05:55 -04:00
parent 1d6c6d9354
commit 4b78e912e5
4 changed files with 41 additions and 61 deletions

View File

@ -69,11 +69,11 @@ class HDHR_Hub():
def get_channels_m3u(self, base_url):
return self.m3u.get_channels_m3u(base_url)
def get_stream_info(self, request_args):
return self.watch.get_stream_info(request_args)
def get_stream_info(self, stream_args):
return self.watch.get_stream_info(stream_args)
def get_stream(self, channel_id, method, channelUri, content_type, duration):
return self.watch.get_stream(channel_id, method, channelUri, content_type, duration)
def get_stream(self, stream_args):
return self.watch.get_stream(stream_args)
hdhr = HDHR_Hub()
@ -174,31 +174,17 @@ class HDHR_HTTP_Server():
@app.route('/auto/<channel>')
def auto(channel):
request_args = {
stream_args = {
"channel": channel.replace('v', ''),
"method": hdhr.config.dict["fhdhr"]["stream_type"],
"method": request.args.get('method', default=hdhr.config.dict["fhdhr"]["stream_type"], type=str),
"duration": request.args.get('duration', default=0, type=int),
}
channel_id = request_args["channel"]
method, channelUri, content_type, duration = hdhr.get_stream_info(request_args)
if channelUri:
if method == "direct":
return Response(hdhr.get_stream(channel_id, method, channelUri, content_type, duration), content_type=content_type, direct_passthrough=True)
elif method == "ffmpeg":
return Response(stream_with_context(hdhr.get_stream(channel_id, method, channelUri, content_type, duration)), mimetype="video/mpeg")
abort(503)
@app.route('/watch', methods=['GET'])
def watch():
if 'method' in list(request.args.keys()) and 'channel' in list(request.args.keys()):
channel_id = str(request.args["channel"])
method = str(request.args["method"])
method, channelUri, content_type, duration = hdhr.get_stream_info(request.args)
if channelUri:
if method == "direct":
return Response(hdhr.get_stream(channel_id, method, channelUri, content_type, duration), content_type=content_type, direct_passthrough=True)
elif method == "ffmpeg":
return Response(stream_with_context(hdhr.get_stream(channel_id, method, channelUri, content_type, duration)), mimetype="video/mpeg")
stream_args = hdhr.get_stream_info(stream_args)
if stream_args["channelUri"]:
if stream_args["method"] == "direct":
return Response(hdhr.get_stream(stream_args), content_type=stream_args["content_type"], direct_passthrough=True)
elif stream_args["method"] == "ffmpeg":
return Response(stream_with_context(hdhr.get_stream(stream_args)), mimetype="video/mpeg")
abort(503)
@app.route('/lineup.post', methods=['POST'])

View File

@ -45,10 +45,9 @@ class channels_M3U():
fakefile.write(
"%s\n" % (
('%s%s/watch?method=%s&channel=%s' %
('%s%s/auto/v%s' %
("http://",
base_url,
self.config.dict["fhdhr"]["stream_type"],
str(channel['number'])))
)
)

View File

@ -13,20 +13,20 @@ class WatchStream():
self.tuners = tuners
self.web = fHDHR.tools.WebReq()
def direct_stream(self, channel_id, method, channelUri, content_type, duration):
def direct_stream(self, stream_args):
chunksize = int(self.tuners.config.dict["direct_stream"]['chunksize'])
if not duration == 0:
duration += time.time()
if not stream_args["duration"] == 0:
stream_args["duration"] += time.time()
req = self.web.session.get(channelUri, stream=True)
req = self.web.session.get(stream_args["channelUri"], stream=True)
def generate():
try:
for chunk in req.iter_content(chunk_size=chunksize):
if not duration == 0 and not time.time() < duration:
if not stream_args["duration"] == 0 and not time.time() < stream_args["duration"]:
req.close()
print("Requested Duration Expired.")
break
@ -40,12 +40,12 @@ class WatchStream():
return generate()
def ffmpeg_stream(self, channel_id, method, channelUri, content_type, duration):
def ffmpeg_stream(self, stream_args):
bytes_per_read = int(self.config.dict["ffmpeg"]["bytes_per_read"])
ffmpeg_command = [self.config.dict["ffmpeg"]["ffmpeg_path"],
"-i", channelUri,
"-i", stream_args["channelUri"],
"-c", "copy",
"-f", "mpegts",
"-nostats", "-hide_banner",
@ -53,8 +53,8 @@ class WatchStream():
"pipe:stdout"
]
if not duration == 0:
duration += time.time()
if not stream_args["duration"] == 0:
stream_args["duration"] += time.time()
ffmpeg_proc = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE)
@ -62,7 +62,7 @@ class WatchStream():
try:
while True:
if not duration == 0 and not time.time() < duration:
if not stream_args["duration"] == 0 and not time.time() < stream_args["duration"]:
ffmpeg_proc.terminate()
ffmpeg_proc.communicate()
print("Requested Duration Expired.")
@ -88,33 +88,30 @@ class WatchStream():
return generate()
def get_stream(self, channel_id, method, channelUri, content_type, duration):
def get_stream(self, stream_args):
try:
self.tuners.tuner_grab()
except TunerError:
print("A " + method + " stream request for channel " +
str(channel_id) + " was rejected do to a lack of available tuners.")
print("A " + stream_args["method"] + " stream request for channel " +
str(stream_args["channel"]) + " was rejected do to a lack of available tuners.")
return
print("Attempting a " + method + " stream request for channel " + str(channel_id))
print("Attempting a " + stream_args["method"] + " stream request for channel " + str(stream_args["channel"]))
if method == "ffmpeg":
return self.ffmpeg_stream(channel_id, method, channelUri, content_type, duration)
elif method == "direct":
return self.direct_stream(channel_id, method, channelUri, content_type, duration)
if stream_args["method"] == "ffmpeg":
return self.ffmpeg_stream(stream_args)
elif stream_args["method"] == "direct":
return self.direct_stream(stream_args)
def get_stream_info(self, request_args):
def get_stream_info(self, stream_args):
method = str(request_args["method"])
channel_id = str(request_args["channel"])
duration = int(request_args["duration"])
stream_args["channelUri"] = self.origserv.get_channel_stream(str(stream_args["channel"]))
if not stream_args["channelUri"]:
print("Could not Obtain Channel Stream.")
stream_args["content_type"] = "video/mpeg"
else:
channelUri_headers = self.web.session.head(stream_args["channelUri"]).headers
stream_args["content_type"] = channelUri_headers['Content-Type']
channelUri = self.origserv.get_channel_stream(channel_id)
if not channelUri:
return None, None, None, None
channelUri_headers = self.web.session.head(channelUri).headers
content_type = channelUri_headers['Content-Type']
return method, channelUri, content_type, duration
return stream_args

View File

@ -13,7 +13,6 @@ class OriginService():
if not self.serviceorigin.login():
raise LoginError(self.config.dict["main"]["servicename"] + " Login Failed.")
self.streamtype = self.config.dict["fhdhr"]["stream_type"]
self.channels = {
"list": {},
"list_updated": None,
@ -47,10 +46,9 @@ class OriginService():
return channel_list
def get_fhdhr_stream_url(self, base_url, channel):
return ('%s%s/watch?method=%s&channel=%s' %
return ('%s%s/auto/v%s' %
("http://",
base_url,
self.streamtype,
channel['number']))
def get_station_list(self, base_url):