-#!/usr/bin/env python
-# vim:ts=4:sts=4:sw=4:noet
-
-from common import grab_xml, grab_json, download_rtmp, Node
-from datetime import datetime
-
-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):
- Node.__init__(self, title, parent)
- self.params = params
- self.vpath = vpath
- self.can_download = True
-
- def download(self):
- auth_doc = grab_xml(self.params["auth"], 0)
- vbase = auth_doc.xpath("//auth:server/text()", namespaces=NS)[0]
- token = auth_doc.xpath("//auth:token/text()", namespaces=NS)[0]
- vbase += "?auth=" + token
- vpath, ext = self.vpath.rsplit(".", 1)
- vpath = ext + ":" + vpath
- filename = self.title + "." + ext
- return download_rtmp(filename, vbase, vpath, HASH_URL)
-
+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.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):
+ 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 IviewIndexNode(Node):
+ def __init__(self, title, parent, url):
+ Node.__init__(self, title, parent)
+ self.url = url
+ self.unique_series = set()
+
+ def fill_children(self):
+ 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)