#! /usr/bin/env python # -*- coding: utf-8 -*- import time import cgi import os import sys import datetime import syslog syslog.openlog('pigebox') d = cgi.parse() if not d: print 'Location: .' print '' print 'Hop' sys.exit(0) end = '%s:%s' % (d.get('end_hour')[0], d.get('end_min')[0]) start = datetime.datetime.strptime('%s %s:%s' % ( d.get('date')[0], d.get('start_hour')[0], d.get('start_min')[0]), '%d/%m/%Y %H:%M') end = datetime.datetime.strptime('%s %s:%s' % ( d.get('date')[0], d.get('end_hour')[0], d.get('end_min')[0]), '%d/%m/%Y %H:%M') if end < start: end = end + datetime.timedelta(1) os.chdir('/home/alsa-record/') def get_filenames(): path = start.strftime('%Y/%m-%b/%d-%a/') filenames = [os.path.join(path, x) for x in os.listdir(path)] if end.hour < start.hour: path = end.strftime('%Y/%m-%b/%d-%a/') if os.path.exists(path): filenames.extend([os.path.join(path, x) for x in os.listdir(path)]) filenames.sort() filenames = [x for x in filenames if x >= start.strftime('%Y/%m-%b/%d-%a/%Hh%M') and x < end.strftime('%Y/%m-%b/%d-%a/%Hh%M')] return filenames while True: filenames = get_filenames() without_extension = [os.path.splitext(x)[0] for x in filenames] for f in without_extension: if without_extension.count(f) > 1: # currently encoding, wait a moment and try again print 'X-Currently-Encoding: please wait' time.sleep(5) break else: break syslog.syslog(syslog.LOG_INFO, 'extraction (%s to %s)' % ( start.strftime('%Y-%m-%d %Hh%M'), end.strftime('%Hh%M'))) if d.get('wav'): ext = 'wav' print 'Content-Type: audio/x-wav' else: ext = 'ogg' print 'Content-Type: audio/ogg' print 'Content-Disposition: attachment; filename=%s.%s\n' % \ (start.strftime('%Y-%m-%d-%Hh%M') + end.strftime('-%Hh%M'), ext) sys.stdout.flush() if ext == 'ogg': command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-'] else: command = ['sox'] + filenames + ['-t', 'wav', '-'] os.system(' '.join(command))