X-Git-Url: https://code.delx.au/webdl/blobdiff_plain/c4336df677690d0d3392cbe380a16e8fda616892..HEAD:/iview.py diff --git a/iview.py b/iview.py index c51d73f..e7c198c 100644 --- a/iview.py +++ b/iview.py @@ -1,133 +1,153 @@ -from common import grab_xml, grab_json, download_rtmp, Node -import itertools - -BASE_URL = "http://www.abc.net.au/iview/" -CONFIG_URL = BASE_URL + "xml/config.xml" -HASH_URL = BASE_URL + "images/iview.jpg" -NS = { - "auth": "http://www.abc.net.au/iView/Services/iViewHandshaker", -} - -class IviewNode(Node): - def __init__(self, title, parent, params, vpath): +from common import append_to_qs, grab_json, grab_text, Node, download_hls +import hashlib +import hmac +import requests_cache +import string +import time +import urllib.parse + +BASE_URL = "https://iview.abc.net.au" +API_URL = "https://iview.abc.net.au/api" + +def format_episode_title(series, ep): + if ep: + return series + " " + ep + else: + return series + +def add_episode(parent, ep_info): + video_key = ep_info["episodeHouseNumber"] + series_title = ep_info["seriesTitle"] + title = ep_info.get("title", None) + episode_title = format_episode_title(series_title, title) + + IviewEpisodeNode(episode_title, parent, video_key) + +class IviewEpisodeNode(Node): + def __init__(self, title, parent, video_key): Node.__init__(self, title, parent) - self.params = params - self.vpath = vpath - self.filename = self.title + "." + vpath.rsplit(".", 1)[1] + self.video_key = video_key + self.filename = title + ".ts" self.can_download = True + def find_hls_url(self, playlist): + for video in playlist: + if video["type"] in ["program", "livestream"]: + streams = video["streams"]["hls"] + for quality in ["720", "sd", "sd-low"]: + if quality in streams: + return streams[quality] + raise Exception("Missing program stream for " + self.video_key + " -- " + self.title) + + def get_auth_token(self): + path = "/auth/hls/sign?ts=%s&hn=%s&d=android-tablet" % (int(time.time()), self.video_key) + sig = hmac.new(b'android.content.res.Resources', path.encode("utf-8"), hashlib.sha256).hexdigest() + auth_url = BASE_URL + path + "&sig=" + sig + with requests_cache.disabled(): + auth_token = grab_text(auth_url) + return auth_token + def download(self): - auth_doc = grab_xml(self.params["auth"], 0) - server = self.params["server_streaming"] - token = auth_doc.xpath("//auth:token/text()", namespaces=NS)[0] - playpath = auth_doc.xpath("//auth:path/text()", namespaces=NS)[0] - if playpath == "playback/_definst_/": - playpath = "flash/" + playpath - vbase = server + "?auth=" + token - vpath, ext = self.vpath.rsplit(".", 1) - vpath = ext + ":" + playpath + vpath - return download_rtmp(self.filename, vbase, vpath, HASH_URL) + info = grab_json(API_URL + "/programs/" + self.video_key) + if "playlist" not in info: + return False + video_url = self.find_hls_url(info["playlist"]) + auth_token = self.get_auth_token() + video_url = append_to_qs(video_url, {"hdnea": auth_token}) + return download_hls(self.filename, video_url) -class IviewSeriesNode(Node): - def __init__(self, title, parent, params, series_ids): + +class IviewIndexNode(Node): + def __init__(self, title, parent, url): Node.__init__(self, title, parent) - self.params = params - self.series_ids = series_ids + self.url = url + self.unique_series = set() def fill_children(self): - for series_id in self.series_ids: - self.fill_children_for_id(series_id) - - def fill_children_for_id(self, series_id): - series_doc = grab_json(self.params["api"] + "series=" + series_id, 3600) - for episode_list in series_doc: - if episode_list["a"] == series_id: - episode_list = episode_list["f"] - break - else: + info = grab_json(self.url) + for key in ["carousels", "collections", "index"]: + for collection_list in info.get(key, None): + if isinstance(collection_list, dict): + for ep_info in collection_list.get("episodes", []): + self.add_series(ep_info) + + def add_series(self, ep_info): + title = ep_info["seriesTitle"] + if title in self.unique_series: return + self.unique_series.add(title) + url = API_URL + "/" + ep_info["href"] + IviewSeriesNode(title, self, url) - for episode in episode_list: - vpath = episode["n"] - episode_title = episode["b"].strip() - if not episode_title.startswith(self.title): - episode_title = self.title + " " + episode_title - if episode_title.lower().endswith(" (final)"): - episode_title = episode_title[:-8] - IviewNode(episode_title, self, self.params, vpath) +class IviewSeriesNode(Node): + def __init__(self, title, parent, url): + Node.__init__(self, title, parent) + self.url = url -class SeriesInfo(object): - def __init__(self, title): - self.title = title - self.series_ids = set() - self.categories = set() + def fill_children(self): + ep_info = grab_json(self.url) + series_slug = ep_info["href"].split("/")[1] + series_url = API_URL + "/series/" + series_slug + "/" + ep_info["seriesHouseNumber"] + info = grab_json(series_url) + for ep_info in info.get("episodes", []): + add_episode(self, ep_info) + +class IviewFlatNode(Node): + def __init__(self, title, parent, url): + Node.__init__(self, title, parent) + self.url = url - def add_series_id(self, series_id): - self.series_ids.add(series_id) + def fill_children(self): + info = grab_json(self.url) + for ep_info in info: + add_episode(self, ep_info) - def add_categories(self, categories): - self.categories.update(categories) class IviewRootNode(Node): - def __init__(self, parent): - Node.__init__(self, "ABC iView", parent) - self.params = {} - self.series_info = {} - self.categories_map = {} - - def load_params(self): - config_doc = grab_xml(CONFIG_URL, 24*3600) - for p in config_doc.xpath("/config/param"): - key = p.attrib["name"] - value = p.attrib["value"] - self.params[key] = value - - def load_series(self): - series_list_doc = grab_json(self.params["api"] + "seriesIndex", 3600) - for series in series_list_doc: - title = series["b"].replace("&", "&") - sid = series["a"] - categories = series["e"].split() - info = self.series_info.get(title, None) - if not info: - info = SeriesInfo(title) - self.series_info[title] = info - info.add_categories(categories) - info.add_series_id(sid) - def load_categories(self): - categories_doc = grab_xml(BASE_URL + self.params["categories"], 24*3600) - by_channel = Node("By Channel", self) - by_genre = Node("By Genre", self) - for category in categories_doc.xpath("//category"): - cid = category.attrib["id"] - category_name = category.xpath("name/text()")[0] - if "genre" in category.attrib: - parent = by_genre - elif cid in ["abc1", "abc2", "abc3", "abc4", "original"]: - parent = by_channel - elif cid in ["featured", "recent", "last-chance", "trailers"]: - parent = self - else: - continue - node = Node(category_name, parent) - self.categories_map[cid] = node - - def link_series(self): - # Create a duplicate within each category for each series - for s in self.series_info.itervalues(): - for cid in s.categories: - parent = self.categories_map.get(cid) - if parent: - IviewSeriesNode(s.title, parent, self.params, s.series_ids) + by_category_node = Node("By Category", self) + + data = grab_json(API_URL + "/categories") + categories = data["categories"] + + for category_data in categories: + category_title = category_data["title"] + category_title = string.capwords(category_title) + + category_href = category_data["href"] + + IviewIndexNode(category_title, by_category_node, API_URL + "/" + category_href) + + def load_channels(self): + by_channel_node = Node("By Channel", self) + + data = grab_json(API_URL + "/channel") + channels = data["channels"] + + for channel_data in channels: + channel_id = channel_data["categoryID"] + channel_title = { + "abc1": "ABC1", + "abc2": "ABC2", + "abc3": "ABC3", + "abc4kids": "ABC4Kids", + "news": "News", + "abcarts": "ABC Arts", + }.get(channel_id, channel_data["title"]) + + channel_href = channel_data["href"] + + IviewIndexNode(channel_title, by_channel_node, API_URL + "/" + channel_href) + + def load_featured(self): + IviewFlatNode("Featured", self, API_URL + "/featured") def fill_children(self): - self.load_params() - self.load_series() self.load_categories() - self.link_series() + self.load_channels() + self.load_featured() def fill_nodes(root_node): - IviewRootNode(root_node) + IviewRootNode("ABC iView", root_node)