]> code.delx.au - webdl/blob - sbs.py
Switch to requests to remove custom caching code
[webdl] / sbs.py
1 #!/usr/bin/env python
2
3 import requests_cache
4 from common import grab_html, grab_json, grab_xml, download_hls, Node, append_to_qs
5
6 import json
7
8 BASE = "http://www.sbs.com.au"
9 FULL_VIDEO_LIST = BASE + "/api/video_search/v2/?m=1&filters={section}{Programs}"
10 VIDEO_URL = BASE + "/ondemand/video/single/%s"
11
12 NS = {
13 "smil": "http://www.w3.org/2005/SMIL21/Language",
14 }
15
16
17 class SbsVideoNode(Node):
18 def __init__(self, title, parent, url):
19 Node.__init__(self, title, parent)
20 self.video_id = url.split("/")[-1]
21 self.can_download = True
22
23 def download(self):
24 with requests_cache.disabled():
25 doc = grab_html(VIDEO_URL % self.video_id)
26 player_params = self.get_player_params(doc)
27 release_url = player_params["releaseUrls"]["html"]
28
29 with requests_cache.disabled():
30 doc = grab_xml(release_url if not release_url.startswith("//") else "https:" + release_url)
31 video = doc.xpath("//smil:video", namespaces=NS)[0]
32 video_url = video.attrib["src"]
33 if not video_url:
34 raise Exception("Unsupported video %s: %s" % (self.video_id, self.title))
35 filename = self.title + ".ts"
36 return download_hls(filename, video_url)
37
38 def get_player_params(self, doc):
39 for script in doc.xpath("//script"):
40 if not script.text:
41 continue
42 for line in script.text.split("\n"):
43 s = "var playerParams = {"
44 if s in line:
45 p1 = line.find(s) + len(s) - 1
46 p2 = line.find("};", p1) + 1
47 if p1 >= 0 and p2 > 0:
48 return json.loads(line[p1:p2])
49 raise Exception("Unable to find player params for %s: %s" % (self.video_id, self.title))
50
51
52 class SbsNavNode(Node):
53 def create_video_node(self, entry_data):
54 SbsVideoNode(entry_data["title"], self, entry_data["id"])
55
56 def find_existing_child(self, path):
57 for child in self.children:
58 if child.title == path:
59 return child
60
61 class SbsRootNode(SbsNavNode):
62 def __init__(self, parent):
63 Node.__init__(self, "SBS", parent)
64
65 def fill_children(self):
66 all_video_entries = self.load_all_video_entries()
67 category_and_entry_data = self.explode_videos_to_unique_categories(all_video_entries)
68 for category_path, entry_data in category_and_entry_data:
69 nav_node = self.create_nav_node(self, category_path)
70 nav_node.create_video_node(entry_data)
71
72 def load_all_video_entries(self):
73 offset = 1
74 amount = 500
75 while True:
76 url = append_to_qs(FULL_VIDEO_LIST, {"range": "%s-%s" % (offset, offset+amount)})
77 data = grab_json(url)
78 entries = data["entries"]
79 if len(entries) == 0:
80 break
81 for entry in entries:
82 yield entry
83 offset += amount
84
85 def explode_videos_to_unique_categories(self, all_video_entries):
86 for entry_data in all_video_entries:
87 for category_data in entry_data["media$categories"]:
88 category_path = self.calculate_category_path(
89 category_data["media$scheme"],
90 category_data["media$name"],
91 )
92 if category_path:
93 yield category_path, entry_data
94
95 def calculate_category_path(self, scheme, name):
96 if not scheme:
97 return
98 if scheme == name:
99 return
100 name = name.split("/")
101 if name[0] != scheme:
102 name.insert(0, scheme)
103 return name
104
105 def create_nav_node(self, parent, category_path):
106 if not category_path:
107 return parent
108
109 current_path = category_path[0]
110 current_node = parent.find_existing_child(current_path)
111 if not current_node:
112 current_node = SbsNavNode(current_path, parent)
113 return self.create_nav_node(current_node, category_path[1:])
114
115 def fill_nodes(root_node):
116 SbsRootNode(root_node)