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