]> code.delx.au - monosys/blob - ripping/grab-abc-stream
youtube.cgi: rewrote get_video_url()
[monosys] / ripping / grab-abc-stream
1 #!/usr/bin/env python
2
3 from lxml import etree
4 import os
5 import subprocess
6 import sys
7 import tempfile
8 import urllib
9 import urlparse
10
11
12 def exec_subprocess(cmd):
13 try:
14 p = subprocess.Popen(cmd)
15 ret = p.wait()
16 if ret != 0:
17 print >>sys.stderr, cmd[0], "exited with error code:", ret
18 return False
19 else:
20 return True
21 except OSError, e:
22 print >>sys.stderr, "Failed to run", cmd[0], e
23 except KeyboardInterrupt:
24 print "Cancelled", cmd
25 try:
26 p.terminate()
27 p.wait()
28 except KeyboardInterrupt:
29 p.send_signal(signal.SIGKILL)
30 p.wait()
31 return False
32
33 def mplayer_convert(stream, author, title):
34 print "Downloading", stream
35 wmfile = tempfile.NamedTemporaryFile()
36 cmd = [
37 "mplayer",
38 "-nocache",
39 "-noconsolecontrols",
40 "-ao",
41 "pcm:file=" + wmfile.name,
42 stream,
43 ]
44 if not exec_subprocess(cmd):
45 return False
46
47 print "Converting", wmfile.name, "to mp3"
48 cmd = [
49 "lame",
50 "--add-id3v2",
51 "--ta", author,
52 "--tt", title,
53 wmfile.name,
54 os.path.splitext(os.path.basename(stream))[0] + ".mp3",
55 ]
56 if not exec_subprocess(cmd):
57 return False
58
59 return True
60
61
62 def grab(u):
63 qs = urlparse.parse_qs(urlparse.urlparse(u).query)
64 wmfile = qs["w"][0]
65 doc = etree.parse(urllib.urlopen(wmfile))
66 streams = doc.xpath("//ref/@href")
67
68 author = qs["pgm"][0]
69 title = qs["t"][0]
70
71 for stream in streams:
72 if not stream.startswith("mms://"):
73 continue
74 if mplayer_convert(stream, author, title):
75 return
76
77 print "Paste 'Listen Now' URLs from ABC... Press CTRL-D to finish"
78 try:
79 for line in sys.stdin:
80 if not line.strip():
81 continue
82 grab(line)
83 except KeyboardInterrupt:
84 print "\nExiting..."
85