]> code.delx.au - webdl/blob - iview.py
iview: don't blow up if video is unavailable
[webdl] / iview.py
1 from common import grab_json, grab_xml, Node, download_hls
2 import requests_cache
3 import urllib.parse
4
5 API_URL = "https://iview.abc.net.au/api"
6 AUTH_URL = "https://iview.abc.net.au/auth"
7
8 def format_episode_title(series, ep):
9 if ep:
10 return series + " " + ep
11 else:
12 return series
13
14 def add_episode(parent, ep_info):
15 video_key = ep_info["episodeHouseNumber"]
16 series_title = ep_info["seriesTitle"]
17 title = ep_info.get("title", None)
18 episode_title = format_episode_title(series_title, title)
19
20 IviewEpisodeNode(episode_title, parent, video_key)
21
22 class IviewEpisodeNode(Node):
23 def __init__(self, title, parent, video_key):
24 Node.__init__(self, title, parent)
25 self.video_key = video_key
26 self.filename = title + ".ts"
27 self.can_download = True
28
29 def find_hls_url(self, playlist):
30 for video in playlist:
31 if video["type"] == "program":
32 for quality in ["hls-plus", "hls-high"]:
33 if quality in video:
34 return video[quality].replace("http:", "https:")
35 raise Exception("Missing program stream for " + self.video_key)
36
37 def get_auth_details(self):
38 with requests_cache.disabled():
39 auth_doc = grab_xml(AUTH_URL)
40 NS = {
41 "auth": "http://www.abc.net.au/iView/Services/iViewHandshaker",
42 }
43 token = auth_doc.xpath("//auth:tokenhd/text()", namespaces=NS)[0]
44 token_url = auth_doc.xpath("//auth:server/text()", namespaces=NS)[0]
45 token_hostname = urllib.parse.urlparse(token_url).netloc
46 return token, token_hostname
47
48 def add_auth_token_to_url(self, video_url, token, token_hostname):
49 parsed_url = urllib.parse.urlparse(video_url)
50 hacked_url = parsed_url._replace(netloc=token_hostname, query="hdnea=" + token)
51 video_url = urllib.parse.urlunparse(hacked_url)
52 return video_url
53
54 def download(self):
55 info = grab_json(API_URL + "/programs/" + self.video_key)
56 if "playlist" not in info:
57 return False
58 video_url = self.find_hls_url(info["playlist"])
59 token, token_hostname= self.get_auth_details()
60 video_url = self.add_auth_token_to_url(video_url, token, token_hostname)
61 return download_hls(self.filename, video_url)
62
63 class IviewIndexNode(Node):
64 def __init__(self, title, parent, url):
65 Node.__init__(self, title, parent)
66 self.url = url
67 self.unique_series = set()
68
69 def fill_children(self):
70 info = grab_json(self.url)
71 for key in ["carousels", "collections", "index"]:
72 for collection_list in info[key]:
73 if isinstance(collection_list, dict):
74 for ep_info in collection_list.get("episodes", []):
75 self.add_series(ep_info)
76
77 def add_series(self, ep_info):
78 title = ep_info["seriesTitle"]
79 if title in self.unique_series:
80 return
81 self.unique_series.add(title)
82 url = API_URL + "/" + ep_info["href"]
83 IviewSeriesNode(title, self, url)
84
85 class IviewSeriesNode(Node):
86 def __init__(self, title, parent, url):
87 Node.__init__(self, title, parent)
88 self.url = url
89
90 def fill_children(self):
91 ep_info = grab_json(self.url)
92 series_slug = ep_info["href"].split("/")[1]
93 series_url = API_URL + "/series/" + series_slug + "/" + ep_info["seriesHouseNumber"]
94 info = grab_json(series_url)
95 for ep_info in info.get("episodes", []):
96 add_episode(self, ep_info)
97
98 class IviewFlatNode(Node):
99 def __init__(self, title, parent, url):
100 Node.__init__(self, title, parent)
101 self.url = url
102
103 def fill_children(self):
104 info = grab_json(self.url)
105 for ep_info in info:
106 add_episode(self, ep_info)
107
108
109 class IviewRootNode(Node):
110 def load_categories(self):
111 by_category_node = Node("By Category", self)
112 def category(name, slug):
113 IviewIndexNode(name, by_category_node, API_URL + "/category/" + slug)
114
115 category("Arts & Culture", "arts")
116 category("Comedy", "comedy")
117 category("Documentary", "docs")
118 category("Drama", "drama")
119 category("Education", "education")
120 category("Lifestyle", "lifestyle")
121 category("News & Current Affairs", "news")
122 category("Panel & Discussion", "panel")
123 category("Regional Australia", "regional")
124 category("Sport", "sport")
125
126 def load_channels(self):
127 by_channel_node = Node("By Channel", self)
128 def channel(name, slug):
129 IviewIndexNode(name, by_channel_node, API_URL + "/channel/" + slug)
130
131 channel("ABC1", "abc1")
132 channel("ABC2", "abc2")
133 channel("ABC3", "abc3")
134 channel("ABC4Kids", "abc4kids")
135 channel("News", "news")
136 channel("ABC Arts", "abcarts")
137 channel("iView Exclusives", "iview")
138
139 def load_featured(self):
140 IviewFlatNode("Featured", self, API_URL + "/featured")
141
142 def fill_children(self):
143 self.load_categories()
144 self.load_channels()
145 self.load_featured()
146
147
148 def fill_nodes(root_node):
149 IviewRootNode("ABC iView", root_node)
150