2 # -*- coding: utf-8 -*-
11 syslog.openlog('pigebox')
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'))
19 start = '%s:%s' % (filename[9:11], filename[12:14])
21 if filename[14] == 'm': # support for seconds
23 start += ':' + filename[15:17]
26 end = '%s:%s' % (filename[15+seconds:17+seconds], filename[18+seconds:20+seconds])
28 end += ':' + filename[24:26]
33 start = datetime.datetime.strptime('%s %s' % (date, start),
35 end = datetime.datetime.strptime('%s %s' % (date, end),
38 end = '%s:%s' % (d.get('end_hour')[0], d.get('end_min')[0])
40 start = datetime.datetime.strptime('%s %s:%s' % (
41 d.get('date')[0], d.get('start_hour')[0], d.get('start_min')[0]),
43 end = datetime.datetime.strptime('%s %s:%s' % (
44 d.get('date')[0], d.get('end_hour')[0], d.get('end_min')[0]),
57 end = end + datetime.timedelta(1)
59 os.chdir('/home/alsa-record/')
62 path = start.strftime('%Y/%m-%b/%d-%a/')
63 filenames = [os.path.join(path, x) for x in os.listdir(path)]
64 if end.hour < start.hour:
65 path = end.strftime('%Y/%m-%b/%d-%a/')
66 if os.path.exists(path):
67 filenames.extend([os.path.join(path, x) for x in os.listdir(path)])
70 filenames = [x for x in filenames if
71 x >= start.strftime('%Y/%m-%b/%d-%a/%Hh%M') and
72 x < end.strftime('%Y/%m-%b/%d-%a/%Hh%M')]
76 filenames = get_filenames()
77 without_extension = [os.path.splitext(x)[0] for x in filenames]
78 for f in without_extension:
79 if without_extension.count(f) > 1:
80 # currently encoding, wait a moment and try again
81 print 'X-Currently-Encoding: please wait'
87 syslog.syslog(syslog.LOG_INFO, 'extraction (%s to %s)' % (
88 start.strftime('%Y-%m-%d %Hh%M'),
89 end.strftime('%Hh%M')))
92 print 'Content-Type: audio/x-wav'
94 print 'Content-Type: audio/ogg'
96 print 'Content-Disposition: attachment; filename=%s.%s\n' % \
97 (start.strftime('%Y-%m-%d-%Hh%M') + end.strftime('-%Hh%M'), ext)
102 command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-']
104 command = ['sox'] + filenames + ['-t', 'wav', '-']
105 os.system(' '.join(command))