]>
code.delx.au - webdl/blob - iview.py
1 from common
import grab_json
, grab_xml
, Node
, download_hls
5 API_URL
= "https://iview.abc.net.au/api"
6 AUTH_URL
= "https://iview.abc.net.au/auth"
8 def format_episode_title(series
, ep
):
10 return series
+ " " + ep
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
)
20 IviewEpisodeNode(episode_title
, parent
, video_key
)
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
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"]:
34 return video
[quality
].replace("http:", "https:")
35 raise Exception("Missing program stream for " + self
.video_key
)
37 def get_auth_details(self
):
38 with requests_cache
.disabled():
39 auth_doc
= grab_xml(AUTH_URL
)
41 "auth": "http://www.abc.net.au/iView/Services/iViewHandshaker",
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
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
)
55 info
= grab_json(API_URL
+ "/programs/" + self
.video_key
)
56 video_url
= self
.find_hls_url(info
["playlist"])
57 token
, token_hostname
= self
.get_auth_details()
58 video_url
= self
.add_auth_token_to_url(video_url
, token
, token_hostname
)
59 return download_hls(self
.filename
, video_url
)
61 class IviewIndexNode(Node
):
62 def __init__(self
, title
, parent
, url
):
63 Node
.__init
__(self
, title
, parent
)
65 self
.unique_series
= set()
67 def fill_children(self
):
68 info
= grab_json(self
.url
)
69 for key
in ["carousels", "collections", "index"]:
70 for collection_list
in info
[key
]:
71 if isinstance(collection_list
, dict):
72 for ep_info
in collection_list
.get("episodes", []):
73 self
.add_series(ep_info
)
75 def add_series(self
, ep_info
):
76 title
= ep_info
["seriesTitle"]
77 if title
in self
.unique_series
:
79 self
.unique_series
.add(title
)
80 url
= API_URL
+ "/" + ep_info
["href"]
81 IviewSeriesNode(title
, self
, url
)
83 class IviewSeriesNode(Node
):
84 def __init__(self
, title
, parent
, url
):
85 Node
.__init
__(self
, title
, parent
)
88 def fill_children(self
):
89 ep_info
= grab_json(self
.url
)
90 series_slug
= ep_info
["href"].split("/")[1]
91 series_url
= API_URL
+ "/series/" + series_slug
+ "/" + ep_info
["seriesHouseNumber"]
92 info
= grab_json(series_url
)
93 for ep_info
in info
.get("episodes", []):
94 add_episode(self
, ep_info
)
96 class IviewFlatNode(Node
):
97 def __init__(self
, title
, parent
, url
):
98 Node
.__init
__(self
, title
, parent
)
101 def fill_children(self
):
102 info
= grab_json(self
.url
)
104 add_episode(self
, ep_info
)
107 class IviewRootNode(Node
):
108 def load_categories(self
):
109 by_category_node
= Node("By Category", self
)
110 def category(name
, slug
):
111 IviewIndexNode(name
, by_category_node
, API_URL
+ "/category/" + slug
)
113 category("Arts & Culture", "arts")
114 category("Comedy", "comedy")
115 category("Documentary", "docs")
116 category("Drama", "drama")
117 category("Education", "education")
118 category("Lifestyle", "lifestyle")
119 category("News & Current Affairs", "news")
120 category("Panel & Discussion", "panel")
121 category("Regional Australia", "regional")
122 category("Sport", "sport")
124 def load_channels(self
):
125 by_channel_node
= Node("By Channel", self
)
126 def channel(name
, slug
):
127 IviewIndexNode(name
, by_channel_node
, API_URL
+ "/channel/" + slug
)
129 channel("ABC1", "abc1")
130 channel("ABC2", "abc2")
131 channel("ABC3", "abc3")
132 channel("ABC4Kids", "abc4kids")
133 channel("News", "news")
134 channel("ABC Arts", "abcarts")
135 channel("iView Exclusives", "iview")
137 def load_featured(self
):
138 IviewFlatNode("Featured", self
, API_URL
+ "/featured")
140 def fill_children(self
):
141 self
.load_categories()
146 def fill_nodes(root_node
):
147 IviewRootNode("ABC iView", root_node
)