]> code.delx.au - webdl/blob - iview.py
brightcove: seems to work well, no need to mark it experimental anymore
[webdl] / iview.py
1 #!/usr/bin/env python
2 # vim:ts=4:sts=4:sw=4:noet
3
4 from common import grab_xml, grab_json, download_rtmp, Node
5 import itertools
6
7 BASE_URL = "http://www.abc.net.au/iview/"
8 CONFIG_URL = BASE_URL + "xml/config.xml"
9 HASH_URL = BASE_URL + "images/iview.jpg"
10 NS = {
11 "auth": "http://www.abc.net.au/iView/Services/iViewHandshaker",
12 }
13
14 class IviewNode(Node):
15 def __init__(self, title, parent, params, vpath):
16 Node.__init__(self, title, parent)
17 self.params = params
18 self.vpath = vpath
19 self.filename = self.title + "." + vpath.rsplit(".", 1)[1]
20 self.can_download = True
21
22 def download(self):
23 auth_doc = grab_xml(self.params["auth"], 0)
24 server = self.params["server_streaming"]
25 token = auth_doc.xpath("//auth:token/text()", namespaces=NS)[0]
26 playpath = auth_doc.xpath("//auth:path/text()", namespaces=NS)[0]
27 if playpath == "playback/_definst_/":
28 playpath = "flash/" + playpath
29 vbase = server + "?auth=" + token
30 vpath, ext = self.vpath.rsplit(".", 1)
31 vpath = ext + ":" + playpath + vpath
32 return download_rtmp(self.filename, vbase, vpath, HASH_URL)
33
34 class IviewSeriesNode(Node):
35 def __init__(self, title, parent, params, series_ids):
36 Node.__init__(self, title, parent)
37 self.params = params
38 self.series_ids = series_ids
39
40 def fill_children(self):
41 for series_id in self.series_ids:
42 self.fill_children_for_id(series_id)
43
44 def fill_children_for_id(self, series_id):
45 series_doc = grab_json(self.params["api"] + "series=" + series_id, 3600)
46 for episode_list in series_doc:
47 if episode_list["a"] == series_id:
48 episode_list = episode_list["f"]
49 break
50 else:
51 return
52
53 for episode in episode_list:
54 vpath = episode["n"]
55 episode_title = episode["b"].strip()
56 if not episode_title.startswith(self.title):
57 episode_title = self.title + " " + episode_title
58 if episode_title.lower().endswith(" (final)"):
59 episode_title = episode_title[:-8]
60 IviewNode(episode_title, self, self.params, vpath)
61
62 class SeriesInfo(object):
63 def __init__(self, title):
64 self.title = title
65 self.series_ids = set()
66 self.categories = set()
67
68 def add_series_id(self, series_id):
69 self.series_ids.add(series_id)
70
71 def add_categories(self, categories):
72 self.categories.update(categories)
73
74 class IviewRootNode(Node):
75 def __init__(self, parent):
76 Node.__init__(self, "ABC iView", parent)
77 self.params = {}
78 self.series_info = {}
79 self.categories_map = {}
80
81 def load_params(self):
82 config_doc = grab_xml(CONFIG_URL, 24*3600)
83 for p in config_doc.xpath("/config/param"):
84 key = p.attrib["name"]
85 value = p.attrib["value"]
86 self.params[key] = value
87
88 def load_series(self):
89 series_list_doc = grab_json(self.params["api"] + "seriesIndex", 3600)
90 for series in series_list_doc:
91 title = series["b"].replace("&", "&")
92 sid = series["a"]
93 categories = series["e"].split()
94 info = self.series_info.get(title, None)
95 if not info:
96 info = SeriesInfo(title)
97 self.series_info[title] = info
98 info.add_categories(categories)
99 info.add_series_id(sid)
100
101 def load_categories(self):
102 categories_doc = grab_xml(BASE_URL + self.params["categories"], 24*3600)
103 by_channel = Node("By Channel", self)
104 by_genre = Node("By Genre", self)
105 for category in categories_doc.xpath("//category"):
106 cid = category.attrib["id"]
107 category_name = category.xpath("name/text()")[0]
108 if "genre" in category.attrib:
109 parent = by_genre
110 elif cid in ["abc1", "abc2", "abc3", "abc4", "original"]:
111 parent = by_channel
112 elif cid in ["featured", "recent", "last-chance", "trailers"]:
113 parent = self
114 else:
115 continue
116 node = Node(category_name, parent)
117 self.categories_map[cid] = node
118
119 def link_series(self):
120 # Create a duplicate within each category for each series
121 for s in self.series_info.itervalues():
122 for cid in s.categories:
123 parent = self.categories_map.get(cid)
124 if parent:
125 IviewSeriesNode(s.title, parent, self.params, s.series_ids)
126
127 def fill_children(self):
128 self.load_params()
129 self.load_series()
130 self.load_categories()
131 self.link_series()
132
133
134 def fill_nodes(root_node):
135 IviewRootNode(root_node)
136