#! /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, FILENAME_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.rsplit('.')[-1] 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' filename = '%s-%s.ext' % ( start.strftime('%Y-%m-%d-%Hh%M'), end.strftime('%Hh%M'), ext) 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) if type(BASE_DIR) is str: os.chdir(BASE_DIR) def get_filenames(): path = start.strftime(PATH_LAYOUT) if not path: filenames = [x for x in os.listdir('.')] else: 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: if type(BASE_DIR) is list: filenames = [] for base_dir in BASE_DIR: os.chdir(base_dir) filenames.extend([os.path.join(base_dir, x) for x in get_filenames()]) filenames.sort() else: 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\n' % filename sys.stdout.flush() if ext == 'ogg': command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-'] elif ext == 'flac': command = ['sox'] + filenames + ['-t', 'flac', '-'] else: command = ['sox'] + filenames + ['-t', 'wav', '-'] # trim command.append('trim') command.append('=%s' % (start - floor_start).seconds) command.append('=%s' % (end - floor_start).seconds) if '--test' in sys.argv: print ' '.join(command) else: os.system(' '.join(command))