]>
code.delx.au - webdl/blob - sbs.py
2 from common
import grab_html
, grab_json
, grab_xml
, download_hls
, download_mpd
, Node
, append_to_qs
7 BASE
= "https://www.sbs.com.au"
8 FULL_VIDEO_LIST
= BASE
+ "/api/video_feed/f/Bgtm9B/sbs-section-programs/"
9 VIDEO_URL
= BASE
+ "/ondemand/video/single/%s"
12 "smil": "http://www.w3.org/2005/SMIL21/Language",
16 class SbsVideoNode(Node
):
17 def __init__(self
, title
, parent
, url
):
18 Node
.__init
__(self
, title
, parent
)
19 self
.video_id
= url
.split("/")[-1]
20 self
.can_download
= True
23 with requests_cache
.disabled():
24 doc
= grab_html(VIDEO_URL
% self
.video_id
)
25 player_params
= self
.get_player_params(doc
)
27 error
= player_params
.get("error", None)
29 print("Cannot download:", error
)
32 release_url
= player_params
["releaseUrls"]["html"]
33 filename
= self
.title
+ ".ts"
35 hls_url
= self
.get_hls_url(release_url
)
37 return download_hls(filename
, hls_url
)
39 return download_mpd(filename
, release_url
)
41 def get_player_params(self
, doc
):
42 for script
in doc
.xpath("//script"):
45 for line
in script
.text
.split("\n"):
46 s
= "var playerParams = {"
48 p1
= line
.find(s
) + len(s
) - 1
49 p2
= line
.find("};", p1
) + 1
50 if p1
>= 0 and p2
> 0:
51 return json
.loads(line
[p1
:p2
])
52 raise Exception("Unable to find player params for %s: %s" % (self
.video_id
, self
.title
))
54 def get_hls_url(self
, release_url
):
55 with requests_cache
.disabled():
56 doc
= grab_xml("https:" + release_url
.replace("http:", "").replace("https:", ""))
57 video
= doc
.xpath("//smil:video", namespaces
=NS
)
60 video_url
= video
[0].attrib
["src"]
63 class SbsNavNode(Node
):
64 def create_video_node(self
, entry_data
):
65 SbsVideoNode(entry_data
["title"], self
, entry_data
["id"])
67 def find_existing_child(self
, path
):
68 for child
in self
.children
:
69 if child
.title
== path
:
72 class SbsRootNode(SbsNavNode
):
73 def __init__(self
, parent
):
74 Node
.__init
__(self
, "SBS", parent
)
76 def fill_children(self
):
77 all_video_entries
= self
.load_all_video_entries()
78 category_and_entry_data
= self
.explode_videos_to_unique_categories(all_video_entries
)
79 for category_path
, entry_data
in category_and_entry_data
:
80 nav_node
= self
.create_nav_node(self
, category_path
)
81 nav_node
.create_video_node(entry_data
)
83 def load_all_video_entries(self
):
88 url
= append_to_qs(FULL_VIDEO_LIST
, {"range": "%s-%s" % (offset
, offset
+amount
-1)})
90 if "entries" not in data
:
91 raise Exception("Missing data in SBS response", data
)
92 entries
= data
["entries"]
95 for i
, entry
in enumerate(entries
):
96 if entry
["guid"] not in uniq
:
97 uniq
.add(entry
["guid"])
100 sys
.stdout
.write(".")
104 def explode_videos_to_unique_categories(self
, all_video_entries
):
105 for entry_data
in all_video_entries
:
106 for category_data
in entry_data
["media$categories"]:
107 category_path
= self
.calculate_category_path(
108 category_data
["media$scheme"],
109 category_data
["media$name"],
112 yield category_path
, entry_data
114 def calculate_category_path(self
, scheme
, name
):
119 name
= name
.split("/")
120 if name
[0] != scheme
:
121 name
.insert(0, scheme
)
124 def create_nav_node(self
, parent
, category_path
):
125 if not category_path
:
128 current_path
= category_path
[0]
129 current_node
= parent
.find_existing_child(current_path
)
131 current_node
= SbsNavNode(current_path
, parent
)
132 return self
.create_nav_node(current_node
, category_path
[1:])
134 def fill_nodes(root_node
):
135 SbsRootNode(root_node
)