diff --git a/fHDHR_web/api/__init__.py b/fHDHR_web/api/__init__.py index a5c3ddb..6cf57c9 100644 --- a/fHDHR_web/api/__init__.py +++ b/fHDHR_web/api/__init__.py @@ -7,6 +7,7 @@ from .settings import Settings from .channels import Channels from .xmltv import xmlTV from .m3u import M3U +from .w3u import W3U from .epg import EPG from .tuners import Tuners from .debug import Debug_JSON @@ -30,6 +31,7 @@ class fHDHR_API(): self.channels = Channels(fhdhr) self.xmltv = xmlTV(fhdhr) self.m3u = M3U(fhdhr) + self.w3u = W3U(fhdhr) self.epg = EPG(fhdhr) self.tuners = Tuners(fhdhr) self.debug = Debug_JSON(fhdhr) diff --git a/fHDHR_web/api/tuners.py b/fHDHR_web/api/tuners.py index 27a0bee..24768c7 100644 --- a/fHDHR_web/api/tuners.py +++ b/fHDHR_web/api/tuners.py @@ -1,6 +1,5 @@ from flask import Response, request, redirect, abort, stream_with_context, session import urllib.parse -import uuid import json from fHDHR.exceptions import TunerError @@ -75,7 +74,7 @@ class Tuners(): "transcode": transcode, "accessed": accessed_url, "client": client_address, - "client_id": "%s_%s" % (client_address, uuid.uuid4()) + "client_id": session["session_id"] } try: diff --git a/fHDHR_web/api/w3u.py b/fHDHR_web/api/w3u.py new file mode 100644 index 0000000..b553c4d --- /dev/null +++ b/fHDHR_web/api/w3u.py @@ -0,0 +1,89 @@ +from flask import Response, request, redirect +import urllib.parse +import json + +from fHDHR.tools import channel_sort + + +class W3U(): + endpoints = ["/api/w3u"] + endpoint_name = "api_w3u" + endpoint_methods = ["GET", "POST"] + + def __init__(self, fhdhr): + self.fhdhr = fhdhr + + def __call__(self, *args): + return self.get(*args) + + def get(self, *args): + + base_url = request.url_root[:-1] + + method = request.args.get('method', default="get", type=str) + channel = request.args.get('channel', default="all", type=str) + redirect_url = request.args.get('redirect', default=None, type=str) + + if method == "get": + + channel_info_m3u = { + "name": self.fhdhr.config.dict["fhdhr"]["friendlyname"], + "image": '%s/favicon.ico' % base_url, + "epg": '%s/api/xmltv' % base_url, + "stations": [] + } + + channel_items = [] + + if channel == "all": + fileName = "channels.w3u" + 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) + elif str(channel) in [str(x) for x in self.fhdhr.device.channels.get_channel_list("number")]: + channel_obj = self.fhdhr.device.channels.get_channel_obj("number", channel) + fileName = "%s.w3u" % channel_obj.number + if channel_obj.enabled: + channel_items.append(channel_obj) + else: + return "Channel Disabled" + else: + return "Invalid Channel" + + channels_info = {} + + for channel_obj in channel_items: + + if self.fhdhr.config.dict["epg"]["images"] == "proxy" or not channel_obj.thumbnail: + logourl = ('%s/api/images?method=get&type=channel&id=%s' % + (base_url, str(channel_obj.dict['origin_id']))) + else: + logourl = channel_obj.thumbnail + + channels_info[channel_obj.number] = { + "name": str(channel_obj.dict['name']), + "url": "%s%s" % (base_url, channel_obj.api_stream_url), + "epgId": str(channel_obj.dict['origin_id']), + "image": logourl, + } + + # Sort the channels + sorted_channel_list = channel_sort(list(channels_info.keys())) + for channel in sorted_channel_list: + channel_info_m3u["stations"].append(channels_info[channel]) + + channels_info_json = json.dumps(channel_info_m3u, indent=4) + + resp = Response(status=200, response=channels_info_json, mimetype='application/json') + resp.headers["content-disposition"] = "attachment; filename=%s" % fileName + return resp + + return Response(status=200, + response=channels_info_json, + mimetype='application/json') + + if redirect_url: + return redirect("%s?retmessage=%s" % (redirect_url, urllib.parse.quote("%s Success" % method))) + else: + return "%s Success" % method diff --git a/fHDHR_web/templates/base.html b/fHDHR_web/templates/base.html index f3d751a..57ac76e 100644 --- a/fHDHR_web/templates/base.html +++ b/fHDHR_web/templates/base.html @@ -28,6 +28,8 @@ xmltv m3u + w3u +