]> code.delx.au - monosys/commitdiff
Added SBS downloader
authorJames Bunton <jamesbunton@fastmail.fm>
Tue, 10 May 2011 13:30:07 +0000 (23:30 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Tue, 10 May 2011 13:30:07 +0000 (23:30 +1000)
scripts/sbs-downloader [new file with mode: 0755]

diff --git a/scripts/sbs-downloader b/scripts/sbs-downloader
new file mode 100755 (executable)
index 0000000..b31eeb3
--- /dev/null
@@ -0,0 +1,110 @@
+#!/usr/bin/env python
+
+from lxml import etree
+import signal
+import subprocess
+import urllib
+
+
+BASE_URL = "http://player.sbs.com.au"
+
+def grab_xml(path):
+       f = urllib.urlopen(BASE_URL + path)
+       doc = etree.parse(f)
+       f.close()
+       return doc
+
+def choose(options):
+       skeys = sorted(options.keys())
+       for i, key in enumerate(skeys):
+               print " %d) %s" % (i+1, key)
+       print " 0) Back"
+       while True:
+               try:
+                       value = int(raw_input("Choose> "))
+                       if value == 0:
+                               return
+                       if value > 0 and value <= len(skeys):
+                               return options[skeys[value-1]]
+               except ValueError:
+                       pass
+
+def get_menu_list():
+       settings = grab_xml("/playerassets/programs/config/standalone_settings.xml")
+       menu_url = settings.xpath("/settings/setting[@name='menuURL']/@value")[0]
+
+       root_menu = grab_xml(menu_url)
+       menu_list = {}
+       for menu in root_menu.xpath("//menu"):
+               try:
+                       title = menu.xpath("title/text()")[0]
+                       playlist_url = menu.xpath("playlist/@xmlSrc")[0]
+                       if title in menu_list:
+                               i = 2
+                               while True:
+                                       if (title+str(i)) not in menu_list:
+                                               title += str(i)
+                                               break
+                                       i += 1
+                       menu_list[title] = playlist_url
+               except IndexError:
+                       continue
+       
+       return menu_list
+
+def get_video_list(playlist_url):
+       video_list = {}
+       playlist = grab_xml(playlist_url)
+       for video_desc in playlist.xpath("//video"):
+               video_desc_url = video_desc.xpath("@src")[0]
+               title = video_desc.xpath("title/text()")[0]
+               video_list[title] = (title, video_desc_url)
+       return video_list
+
+def download_video((title, video_desc_url)):
+       video = grab_xml(video_desc_url)
+       vbase = video.xpath("//meta/@base")[0]
+       bestrate = 0
+       bestvpath = None
+       for vpath in video.xpath("//switch/video"):
+               rate = float(vpath.xpath("@system-bitrate")[0])
+               if rate > bestrate:
+                       bestrate = rate
+                       bestvpath = vpath.xpath("@src")[0]
+       title += "." + bestvpath.rsplit(".", 1)[1]
+       if bestvpath.endswith(".flv"):
+               bestvpath = bestvpath[:-4]
+       cmd = [
+               "rtmpdump",
+               "-o", title,
+               "-r", vbase,
+               "-y", bestvpath,
+       ]
+       try:
+               p = subprocess.Popen(cmd)
+               p.wait()
+       except KeyboardInterrupt:
+               print "Cancelled", cmd
+               try:
+                       p.terminate()
+                       p.wait()
+               except KeyboardInterrupt:
+                       p.send_signal(signal.SIGKILL)
+                       p.wait()
+
+def main():
+       while True:
+               menu_list = get_menu_list()
+               playlist_url = choose(menu_list)
+               if playlist_url is None:
+                       continue
+               video_list = get_video_list(playlist_url)
+               while True:
+                       video_desc_url = choose(video_list)
+                       if video_desc_url is None:
+                               break
+                       download_video(video_desc_url)
+
+if __name__ == "__main__":
+       main()
+