annotate iviewget.py @ 3:34471d354603

Use os.path.join to construct the output filename, hopefully more portable.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 20 Aug 2009 17:35:14 +0930
parents 6edc508e0775
children c4188feb8f26
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import iview
2
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
4 import os.path
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
5 import re
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import subprocess
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import sys
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import time
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 outpath = '.'
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 outext = 'flv16x9'
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 flvstreamerpath = '/home/darius/projects/flvstreamer/flvstreamer_x86'
2
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
13 flvpattern = re.compile("FLVStreamer v")
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 def listChannels(iview):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 i = 1
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 print "Id %-30s %-35s" % ('Name', 'Description')
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 for c in iview.channels:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 print "%2d %-30s %-35s" % (i, c.name, c.descr)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 i += 1
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 def listSeries(chan):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 i = 1
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 for s in chan.series:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 if len(s.title) > 1:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 title = s.title[1]
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 else:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 title = s.title[0]
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 print "%3d %s" % (i, title)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 i += 1
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 def getInt(f):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 ans = f.readline()
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 try:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 i = int(ans)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 except ValueError:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 return None
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 return i
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 def getSeries(iview, series, outfile = None):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 cmd = [flvstreamerpath]
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 if outfile == None:
3
34471d354603 Use os.path.join to construct the output filename, hopefully more portable.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
45 outfile = os.path.join(outpath, series.asset.replace('/', '-') + '.' + outext)
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 cmd += iview.genFetchCmd(series, outfile)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 p = subprocess.Popen(cmd)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 while True:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 res = p.poll()
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 if res == None:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 time.sleep(0.1)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 else:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 return res
2
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
54
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
55 def checkFLVStreamer():
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
56 try:
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
57 s = subprocess.Popen(flvstreamerpath, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
58 except OSError:
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
59 print "Couldn't execute FLVstreamer"
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
60 return False
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
61 while s.poll() != 1:
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
62 time.sleep(0.1)
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
63
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
64 out, err = s.communicate()
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
65 if flvpattern.match(err) == None:
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
66 print "Unrecognised output from FLVstreamer"
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
67 return False
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
68
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
69 return True
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
70
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 if __name__ == "__main__":
2
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
72 if not checkFLVStreamer():
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
73 sys.exit(1)
6edc508e0775 Try and run flvstreamer on init and exit if it fails.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
74
0
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 iview = iview.IView()
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 if iview.metered:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 print "WARNING: iview is metered"
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 print
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 slist = []
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 while True:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 chan = None
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 listChannels(iview)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 while True:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 print "Please select a channel number (0 to exit): ",
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 i = getInt(sys.stdin)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 if i != None and i >= 0 and i <= len(iview.channels):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 break
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 print "Invalid entry, try again"
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 if i == 0:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 break
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 cidx = i - 1
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 chan = iview.channels[cidx]
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 chan.getSeries()
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 listSeries(chan)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 while True:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 print "Please select which series to download (space separated numbers):"
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 res = sys.stdin.readline()
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 for r in res.split():
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 try:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 i = int(r)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 except ValueError:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 i = None
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 if i != None and i >= 0 and i <= len(chan.series):
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 slist += [chan.series[i - 1]]
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 if len(slist) > 0 or i == 0:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 break
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 print "Invalid entry, try again"
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 if len(slist) == 0:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 print "Nothing to download"
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 sys.exit(0)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 print "Downloading %d items" % (len(slist))
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 for series in slist:
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 print "Fetching " + series.asset
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 res = getSeries(iview, series)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 print "Result is " + str(res)
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 # Re-auth or the server won't love us
fa7ca67af8c9 Initial commit of a python program to fetch iview streams using flvstreamer.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 iview.getAuth()