#! /usr/bin/env python # -*- coding: utf-8 -*- import time import cgi import os import sys import datetime import syslog try: from config import BASE_DIR, PATH_LAYOUT except ImportError: BASE_DIR = '/home/alsa-record/' PATH_LAYOUT = '%Y/%m-%b/%d-%a/' FILENAME_LAYOUT = '%Hh%M' syslog.openlog('pigebox') d = cgi.parse() if os.environ.get('PATH_INFO'): # filename has to be of the following form: %Y%m%d-%Hh%M-%Hh%M.%ext filename = os.path.basename(os.environ.get('PATH_INFO')) date = filename[:8] start = '%s:%s' % (filename[9:11], filename[12:14]) seconds = 0 if filename[14] == 'm': # support for seconds seconds += 3 start += ':' + filename[15:17] else: start += ':00' end = '%s:%s' % (filename[15+seconds:17+seconds], filename[18+seconds:20+seconds]) if seconds: end += ':' + filename[24:26] else: end += ':00' ext = filename[-3:] start = datetime.datetime.strptime('%s %s' % (date, start), '%Y%m%d %H:%M:%S') end = datetime.datetime.strptime('%s %s' % (date, end), '%Y%m%d %H:%M:%S') elif d: 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 d.get('wav'): ext = 'wav' else: ext = 'ogg' else: print 'Location: .' print '' print 'Hop' sys.exit(0) if end < start: end = end + datetime.timedelta(1) floor_start = start if floor_start.second: floor_start = floor_start.replace(second=0) floor_start = floor_start.replace(minute=floor_start.minute/15*15) ceil_end = end if ceil_end.second: ceil_end = ceil_end.replace(minute=ceil_end.minute+1, second=0) if ceil_end.minute%15: if ceil_end.minute / 15 == 3: ceil_end = ceil_end.replace(minute=0) ceil_end += datetime.timedelta(seconds=3600) else: ceil_end = ceil_end.replace(minute=(1+(ceil_end.minute/15))*15) os.chdir(BASE_DIR) def get_filenames(): path = start.strftime(PATH_LAYOUT) filenames = [os.path.join(path, x) for x in os.listdir(path)] if end.hour < start.hour: path = end.strftime(PATH_LAYOUT) 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 >= floor_start.strftime(PATH_LAYOUT + FILENAME_LAYOUT) and x < ceil_end.strftime(PATH_LAYOUT + FILENAME_LAYOUT)] 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 ext == 'wav': print 'Content-Type: audio/x-wav' else: 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', '-'] if filenames[0].endswith('.wav'): # files get compressed after a while, and they lose their original creation # time creation_time = time.localtime(os.stat(filenames[0])[-3]) daytime_as_filename = '%02dh%02d' % creation_time[3:5] if not (daytime_as_filename in filenames[0] and creation_time[5] == 0): floor_start = datetime.datetime(*creation_time[:6]) if start != floor_start or end != ceil_end: trim_parts = ['trim'] if start != floor_start: trim_start = (start - floor_start).seconds trim_parts.append(str(trim_start)) else: trim_start = 0 if end != ceil_end: if trim_start: duration = (end - start).seconds trim_parts.append(str(duration)) else: trim_end = (ceil_end-end).seconds trim_parts.append(str(trim_end)) command.extend(trim_parts) if '--test' in sys.argv: print ' '.join(command) else: os.system(' '.join(command))