2 # -*- coding: utf-8 -*-
12 from config import BASE_DIR, PATH_LAYOUT, FILENAME_LAYOUT
14 BASE_DIR = '/home/alsa-record/'
15 PATH_LAYOUT = '%Y/%m-%b/%d-%a/'
16 FILENAME_LAYOUT = '%Hh%M'
18 if isinstance(BASE_DIR, basestring):
21 for i, dirname in enumerate(BASE_DIR):
22 if not dirname.endswith('/'):
23 BASE_DIR[i] = dirname + '/'
25 syslog.openlog('pigebox')
27 cgi_params = cgi.parse()
29 if os.environ.get('PATH_INFO'):
30 # filename has to be of date-start-end.extension
31 # - date must be %Y%m%d
32 # - start and end must be one of %Hh%Mm%S.%f, %Hh%Mm%S, %Hh%Mm, %Hh%M
33 # - extension must be one of wav, ogg, flac
35 # ex: 20160515-13h45-14h00m05.flac
36 filename = os.path.basename(os.environ.get('PATH_INFO'))
37 basename, ext = filename.rsplit('.', 1)
38 date_str, start_str, end_str = basename.split('-')
40 date = datetime.datetime.strptime(date_str, '%Y%m%d')
44 for time_format in ('%Hh%Mm%S.%f', '%Hh%Mm%S', '%Hh%Mm', '%Hh%M'):
45 if start_time is None:
47 start_time = datetime.datetime.strptime(start_str, time_format)
52 end_time = datetime.datetime.strptime(end_str, time_format)
55 start = datetime.datetime(date.year, date.month, date.day,
56 start_time.hour, start_time.minute, start_time.second,
57 start_time.microsecond)
58 end = datetime.datetime(date.year, date.month, date.day,
59 end_time.hour, end_time.minute, end_time.second,
62 start = datetime.datetime.strptime('%s %s:%s' % (
63 cgi_params.get('date')[0], cgi_params.get('start_hour')[0], cgi_params.get('start_min')[0]),
65 end = datetime.datetime.strptime('%s %s:%s' % (
66 cgi_params.get('date')[0], cgi_params.get('end_hour')[0], cgi_params.get('end_min')[0]),
68 if cgi_params.get('wav'):
72 filename = '%s-%s.%s' % (
73 start.strftime('%Y-%m-%d-%Hh%M'), end.strftime('%Hh%M'), ext)
81 end = end + datetime.timedelta(1)
84 if floor_start.second or floor_start.microsecond:
85 floor_start = floor_start.replace(second=0, microsecond=0)
86 floor_start = floor_start.replace(minute=floor_start.minute/15*15)
89 if ceil_end.second or ceil_end.microsecond:
90 ceil_end = ceil_end.replace(minute=ceil_end.minute+1, second=0, microsecond=0)
91 if ceil_end.minute%15:
92 if ceil_end.minute / 15 == 3:
93 ceil_end = ceil_end.replace(minute=0)
94 ceil_end += datetime.timedelta(seconds=3600)
96 ceil_end = ceil_end.replace(minute=(1+(ceil_end.minute/15))*15)
98 def get_filenames(base_dir):
99 path = os.path.join(base_dir, start.strftime(PATH_LAYOUT))
101 filenames = [x for x in os.listdir('.')]
103 filenames = [os.path.join(path, x) for x in os.listdir(path)]
104 if end.hour < start.hour:
105 path = os.path.join(base_dir, end.strftime(PATH_LAYOUT))
106 if os.path.exists(path):
107 filenames.extend([os.path.join(path, x) for x in os.listdir(path)])
109 filenames = [x for x in filenames if
110 x[len(base_dir):] >= floor_start.strftime(PATH_LAYOUT + FILENAME_LAYOUT) and
111 x[len(base_dir):] < ceil_end.strftime(PATH_LAYOUT + FILENAME_LAYOUT)]
116 for base_dir in BASE_DIR:
117 filenames.extend(get_filenames(base_dir))
120 without_extension = [os.path.splitext(x)[0] for x in filenames]
121 for f in without_extension:
122 if without_extension.count(f) > 1:
123 # currently encoding, wait a moment and try again
124 print 'X-Currently-Encoding: please wait'
130 syslog.syslog(syslog.LOG_INFO, 'extraction (%s to %s)' % (
131 start.strftime('%Y-%m-%d %Hh%M'),
132 end.strftime('%Hh%M')))
135 print 'Content-Type: audio/x-wav'
137 print 'Content-Type: audio/ogg'
139 print 'Content-Disposition: attachment; filename=%s\n' % filename
144 command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-']
146 command = ['sox'] + filenames + ['-t', 'flac', '-']
148 command = ['sox'] + filenames + ['-t', 'wav', '-']
151 command.append('trim')
152 command.append('=%s.%06d' % ((start - floor_start).seconds, (start - floor_start).microseconds))
153 command.append('=%s.%06d' % ((end - floor_start).seconds, (end - floor_start).microseconds))
155 if cgi_params.get('fade'): # fade duration, ex: 0.2
156 command.append('fade')
157 command.append(cgi_params.get('fade')[0])
158 command.append('0') # apply fade to both start and end
160 if cgi_params.get('norm'): # normalize
161 command.append('gain')
163 command.append(cgi_params.get('norm')[0])
165 if '--test' in sys.argv:
166 print ' '.join(command)
168 os.system(' '.join(command))