Add more documentation
[svtplaydump.git] / svtplaydump.py
1 #!/usr/bin/env python
2 #
3 # (C) Copyright 2010 Mikael Frykholm <mikael@frykholm.com>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>
17 #
18 # Changelog:
19 # 0.2 added python 2.4 urlparse compatibility
20 # 0.1 initial release
21
22 from BeautifulSoup import BeautifulSoup
23 from subprocess import *
24 import re
25 import json
26 try:
27 import urlparse
28 except ImportError:
29 pass
30 import urllib2
31 try:
32 import urllib2.urlparse as urlparse
33 except ImportError:
34 pass
35 import sys
36
37 def main(argv=None):
38 if argv is None:
39 argv=sys.argv
40 videoid = re.findall("/video/(.*)[/]*",argv[1])[0]
41
42 soup = BeautifulSoup(urllib2.urlopen("http://www.svtplay.se/video/%s/?type=embed"%videoid).read())
43
44 flashvars = json.loads(soup.find("param", {"name":"flashvars",'value':True})['value'][5:])
45 try:
46 title = flashvars['statistics']['title']
47 except:
48 title = "unnamed"
49 if 'dynamicStreams' in flashvars:
50 url = flashvars['dynamicStreams'][0].split('url:')[1].split('.mp4,')[0] +'.mp4'
51 filename = title+".mp4"
52 print Popen(["rtmpdump",u"-o"+filename,"-r", url], stdout=PIPE).communicate()[0]
53 if 'pathflv' in flashvars:
54 rtmp = flashvars['pathflv'][0]
55 filename = title+".flv"
56 print Popen(["mplayer","-dumpstream","-dumpfile",filename, rtmp], stdout=PIPE).communicate()[0]
57 if 'video' in flashvars:
58 url = sorted(flashvars['video']['videoReferences'], key=lambda k: k['bitrate'])[-1]['url']
59 filename = title+".mp4"
60 print Popen(["rtmpdump",u"-o"+filename,"-r", url], stdout=PIPE).communicate()[0]
61
62 else:
63 print "Could not find any streams"
64 return
65
66 if __name__ == "__main__":
67 sys.exit(main())