All pastes #470257 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-04 05:07 UTC
rendered paste body
#! /usr/bin/env python

#
# copy_recordings.py
#

from mx import DateTime
import os, os.path
import re

RE = re.compile(r'from-(?P<from>[0-9]*)_to-(?P<to>[0-9]*)')

ALREADY_MOVED = []

AST_RECORDINGS_DIR = '/var/spool/asterisk/monitor'
RECORDINGS_DIR = '/mnt/recordings'

if os.popen('/bin/mount').read().find('recordings') < 0:
    raise Exception('recordings not mounted')
for root, dirs, files in os.walk(RECORDINGS_DIR):
    ALREADY_MOVED += files


for root, dirs, files in os.walk(AST_RECORDINGS_DIR):
    for file in files:
        if file in ALREADY_MOVED: continue
        if not file.startswith('MON'): continue
        if file.find('in') >= 0 or file.find('out') >= 0: continue

        # This is a sample filename for some of the recordings
        # MON_1172866490.3082_from-7238_to-6665551212.WAV
        ticks = float(file.split('_')[1])
        frm,to = RE.findall(file)[0]
        dt = DateTime.DateTimeFromTicks(ticks)
        datedirname = dt.Format('%Y-%m-%d')
        exten = ''
        if len(frm) == 4:
            exten = frm
        elif len(to) == 4:
            exten = to

        destdir = os.path.join(RECORDINGS_DIR,datedirname,exten)

        if not os.path.exists(destdir):
            os.makedirs(destdir)

        infilename = os.path.join(AST_RECORDINGS_DIR,file)
        outfilename = os.path.join(destdir,file)

        infile = open(infilename,'r')
        outfile = open(outfilename,'w')

        print 'Copy: %s to %s' % (infilename, outfilename)
        outfile.write(infile.read())

        infile.close()
        outfile.close()

        ALREADY_MOVED.append(file)




# vim: set ai si et sw=4 :