]> code.delx.au - webdl/blob - iview.py
Return result code for iView
[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
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, vpath):
16 Node.__init__(self, title, parent)
17 self.vpath = vpath
18 self.can_download = True
19
20 def download(self):
21 auth_doc = grab_xml(PARAMS["auth"], 0)
22 vbase = auth_doc.xpath("//auth:server/text()", namespaces=NS)[0]
23 token = auth_doc.xpath("//auth:token/text()", namespaces=NS)[0]
24 vbase += "?auth=" + token
25 vpath, ext = self.vpath.rsplit(".", 1)
26 vpath = ext + ":" + vpath
27 filename = self.title + "." + ext
28 return download_rtmp(filename, vbase, vpath, HASH_URL)
29
30
31 def fill_nodes(root_node):
32 config_doc = grab_xml(CONFIG_URL, 24*3600)
33 global PARAMS
34 PARAMS = dict((p.attrib["name"], p.attrib["value"]) for p in config_doc.xpath("/config/param"))
35
36 categories_doc = grab_xml(BASE_URL + PARAMS["categories"], 24*3600)
37 categories_map = {}
38 for category in categories_doc.xpath("//category[@genre='true']"):
39 cid = category.attrib["id"]
40 category_name = category.xpath("name/text()")[0]
41 category_node = Node(category_name, root_node)
42 categories_map[cid] = category_node
43
44 # Create a duplicate of each series within each category that it appears
45 series_list_doc = grab_json(PARAMS["api"] + "seriesIndex", 3600)
46 now = datetime.now()
47 for series in series_list_doc:
48 categories = series["e"].split()
49 sid = series["a"]
50 max_age = None
51 for episode in series["f"]:
52 air_date = datetime.strptime(episode["f"], "%Y-%m-%d %H:%M:%S")
53 diff = now - air_date
54 diff = 24*3600*diff.days + diff.seconds
55 if max_age is None or diff < max_age:
56 max_age = diff
57
58 if max_age is None:
59 continue
60
61 series_title = series["b"].replace("&amp;", "&")
62 series_nodes = []
63 for cid in categories:
64 category_node = categories_map.get(cid, None)
65 if category_node:
66 series_nodes.append(Node(series_title, category_node))
67 if not series_nodes:
68 continue
69
70 series_doc = grab_json(PARAMS["api"] + "series=" + sid, max_age)[0]
71 for episode in series_doc["f"]:
72 vpath = episode["n"]
73 episode_title = episode["b"].strip()
74 if series_title != episode_title:
75 episode_title = series_title + " " + episode_title
76 for series_node in series_nodes:
77 IviewNode(episode_title, series_node, vpath)
78
79