]> git.0d.be Git - pige-extractor.git/blob - download.cgi
Fixed redirect on no data
[pige-extractor.git] / download.cgi
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import time
5 import cgi
6 import os
7 import sys
8 import datetime
9 import syslog
10
11 syslog.openlog('pigebox')
12
13 d = cgi.parse()
14
15 if os.environ.get('PATH_INFO'):
16     # filename has to be of the following form: %Y%m%d-%Hh%M-%Hh%M.%ext
17     filename = os.path.basename(os.environ.get('PATH_INFO'))
18     date = filename[:8]
19     start = '%s:%s' % (filename[9:11], filename[12:14])
20     end = '%s:%s' % (filename[15:17], filename[18:20])
21     ext = filename[-3:]
22
23     start = datetime.datetime.strptime('%s %s' % (date, start),
24            '%Y%m%d %H:%M')
25     end = datetime.datetime.strptime('%s %s' % (date, end),
26            '%Y%m%d %H:%M')
27 elif d:
28     end = '%s:%s' % (d.get('end_hour')[0], d.get('end_min')[0])
29
30     start = datetime.datetime.strptime('%s %s:%s' % (
31            d.get('date')[0], d.get('start_hour')[0], d.get('start_min')[0]),
32            '%d/%m/%Y %H:%M')
33     end = datetime.datetime.strptime('%s %s:%s' % (
34            d.get('date')[0], d.get('end_hour')[0], d.get('end_min')[0]),
35            '%d/%m/%Y %H:%M')
36     if d.get('wav'):
37         ext = 'wav'
38     else:
39         ext = 'ogg'
40 else:
41     print 'Location: .'
42     print ''
43     print 'Hop'
44     sys.exit(0)
45
46 if end < start:
47     end = end + datetime.timedelta(1)
48
49 os.chdir('/home/alsa-record/')
50
51 def get_filenames():
52     path = start.strftime('%Y/%m-%b/%d-%a/')
53     filenames = [os.path.join(path, x) for x in os.listdir(path)]
54     if end < start:
55         path = end.strftime('%Y/%m-%b/%d-%a/')
56         if os.path.exists(path):
57             filenames.extend([os.path.join(path, x) for x in os.listdir(path)])
58     filenames.sort()
59
60     filenames = [x for x in filenames if
61                 x >= start.strftime('%Y/%m-%b/%d-%a/%Hh%M') and 
62                 x < end.strftime('%Y/%m-%b/%d-%a/%Hh%M')]
63     return filenames
64
65 while True:
66     filenames = get_filenames()
67     without_extension = [os.path.splitext(x)[0] for x in filenames]
68     for f in without_extension:
69         if without_extension.count(f) > 1:
70             # currently encoding, wait a moment and try again
71             print 'X-Currently-Encoding: please wait'
72             time.sleep(5)
73             break
74     else:
75         break
76
77 syslog.syslog(syslog.LOG_INFO, 'extraction (%s to %s)' % (
78                         start.strftime('%Y-%m-%d %Hh%M'),
79                         end.strftime('%Hh%M')))
80
81 if ext == 'wav':
82     print 'Content-Type: audio/x-wav'
83 else:
84     print 'Content-Type: audio/ogg'
85
86 print 'Content-Disposition: attachment; filename=%s.%s\n' % \
87                 (start.strftime('%Y-%m-%d-%Hh%M') + end.strftime('-%Hh%M'), ext)
88
89 sys.stdout.flush()
90
91 if ext == 'ogg':
92     command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-']
93 else:
94     command = ['sox'] + filenames + ['-t', 'wav', '-']
95 os.system(' '.join(command))
96