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