]> code.delx.au - webdl/blobdiff - iview.py
Fixed silly bug in removing ffmpeg detection
[webdl] / iview.py
index 55101d1f4950b5607aed928b74fd8435094a5eb8..e7c198cbd325a443a3263dcd05299a78eb890a4b 100644 (file)
--- a/iview.py
+++ b/iview.py
@@ -1,9 +1,13 @@
-from common import grab_json, grab_xml, Node, download_hls
+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
 
-API_URL = "http://iview.abc.net.au/api"
-AUTH_URL = "http://iview.abc.net.au/auth"
+BASE_URL = "https://iview.abc.net.au"
+API_URL = "https://iview.abc.net.au/api"
 
 def format_episode_title(series, ep):
     if ep:
@@ -28,36 +32,31 @@ class IviewEpisodeNode(Node):
 
     def find_hls_url(self, playlist):
         for video in playlist:
-            if video["type"] == "program":
-                for quality in ["hls-plus", "hls-high"]:
-                    if quality in video:
-                        return video[quality].replace("http:", "https:")
-        raise Exception("Missing program stream for " + self.video_key)
-
-    def get_auth_details(self):
+            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_doc = grab_xml(AUTH_URL)
-        NS = {
-            "auth": "http://www.abc.net.au/iView/Services/iViewHandshaker",
-        }
-        token = auth_doc.xpath("//auth:tokenhd/text()", namespaces=NS)[0]
-        token_url = auth_doc.xpath("//auth:server/text()", namespaces=NS)[0]
-        token_hostname = urllib.parse.urlparse(token_url).netloc
-        return token, token_hostname
-
-    def add_auth_token_to_url(self, video_url, token, token_hostname):
-        parsed_url = urllib.parse.urlparse(video_url)
-        hacked_url = parsed_url._replace(netloc=token_hostname, query="hdnea=" + token)
-        video_url = urllib.parse.urlunparse(hacked_url)
-        return video_url
+            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"])
-        token, token_hostname= self.get_auth_details()
-        video_url = self.add_auth_token_to_url(video_url, token, token_hostname)
+        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)
@@ -67,9 +66,10 @@ class IviewIndexNode(Node):
     def fill_children(self):
         info = grab_json(self.url)
         for key in ["carousels", "collections", "index"]:
-            for collection_list in info[key]:
-                for ep_info in collection_list["episodes"]:
-                    self.add_series(ep_info)
+            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"]
@@ -89,7 +89,7 @@ class IviewSeriesNode(Node):
         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["episodes"]:
+        for ep_info in info.get("episodes", []):
             add_episode(self, ep_info)
 
 class IviewFlatNode(Node):
@@ -106,31 +106,38 @@ class IviewFlatNode(Node):
 class IviewRootNode(Node):
     def load_categories(self):
         by_category_node = Node("By Category", self)
-        def category(name, slug):
-            IviewIndexNode(name, by_category_node, API_URL + "/category/" + slug)
-
-        category("Arts & Culture", "arts")
-        category("Comedy", "comedy")
-        category("Documentary", "docs")
-        category("Drama", "drama")
-        category("Education", "education")
-        category("Lifestyle", "lifestyle")
-        category("News & Current Affairs", "news")
-        category("Panel & Discussion", "panel")
-        category("Sport", "sport")
+
+        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)
-        def channel(name, slug):
-            IviewIndexNode(name, by_channel_node, API_URL + "/channel/" + slug)
-
-        channel("ABC1", "abc1")
-        channel("ABC2", "abc2")
-        channel("ABC3", "abc3")
-        channel("ABC4Kids", "abc4kids")
-        channel("News", "news")
-        channel("ABC Arts", "abcarts")
-        channel("iView Exclusives", "iview")
+
+        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")