]>
code.delx.au - webdl/blob - iview.py
1 from common
import grab_json
, grab_xml
, Node
, download_hls
5 API_URL
= "http://iview.abc.net.au/api"
6 AUTH_URL
= "http://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 index_list
in info
["index"]:
70 for ep_info
in index_list
["episodes"]:
71 self
.add_series(ep_info
)
73 def add_series(self
, ep_info
):
74 title
= ep_info
["seriesTitle"]
75 if title
in self
.unique_series
:
77 self
.unique_series
.add(title
)
78 url
= API_URL
+ "/" + ep_info
["href"]
79 IviewSeriesNode(title
, self
, url
)
81 class IviewSeriesNode(Node
):
82 def __init__(self
, title
, parent
, url
):
83 Node
.__init
__(self
, title
, parent
)
86 def fill_children(self
):
87 ep_info
= grab_json(self
.url
)
88 series_slug
= ep_info
["href"].split("/")[1]
89 series_url
= API_URL
+ "/series/" + series_slug
+ "/" + ep_info
["seriesHouseNumber"]
90 info
= grab_json(series_url
)
91 for ep_info
in info
["episodes"]:
92 add_episode(self
, ep_info
)
94 class IviewFlatNode(Node
):
95 def __init__(self
, title
, parent
, url
):
96 Node
.__init
__(self
, title
, parent
)
99 def fill_children(self
):
100 info
= grab_json(self
.url
)
102 add_episode(self
, ep_info
)
105 class IviewRootNode(Node
):
106 def load_categories(self
):
107 by_category_node
= Node("By Category", self
)
108 def category(name
, slug
):
109 IviewIndexNode(name
, by_category_node
, API_URL
+ "/category/" + slug
)
111 category("Arts & Culture", "arts")
112 category("Comedy", "comedy")
113 category("Documentary", "docs")
114 category("Drama", "drama")
115 category("Education", "education")
116 category("Lifestyle", "lifestyle")
117 category("News & Current Affairs", "news")
118 category("Panel & Discussion", "panel")
119 category("Sport", "sport")
121 def load_channels(self
):
122 by_channel_node
= Node("By Channel", self
)
123 def channel(name
, slug
):
124 IviewIndexNode(name
, by_channel_node
, API_URL
+ "/channel/" + slug
)
126 channel("ABC1", "abc1")
127 channel("ABC2", "abc2")
128 channel("ABC3", "abc3")
129 channel("ABC4Kids", "abc4kids")
130 channel("News", "news")
131 channel("ABC Arts", "abcarts")
132 channel("iView Exclusives", "iview")
134 def load_featured(self
):
135 IviewFlatNode("Featured", self
, API_URL
+ "/featured")
137 def fill_children(self
):
138 self
.load_categories()
143 def fill_nodes(root_node
):
144 IviewRootNode("ABC iView", root_node
)