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