-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess.py
132 lines (114 loc) · 5.03 KB
/
postprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import asyncio
import configparser
import json
import re
import shlex
import shutil
import subprocess
import tempfile
from pprint import pprint
from urllib import parse
import requests
import os.path
import sys
import logging
from Auphonic import AuphonicProduction
if len(sys.argv) <= 1:
print ('usage: %s [inputfile] [inputfile] ...')
config = configparser.RawConfigParser()
config.read([os.path.join(os.environ.get('AST_CONFIG_DIR', '/etc/asterisk'), 'audio-postpro.cfg'),
os.path.expanduser('~/.audio-postpro.cfg'),
os.environ.get('FAX2MAIL_CONFIG', 'audio-postpro.cfg')],
encoding='utf-8')
auphonic_accesstoken = config.get('auphonic', 'accesstoken')
numeric_level = getattr(logging, config.get('process', 'loglevel').upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % config.get('process', 'loglevel'))
logging.basicConfig(level=numeric_level, filename=config.get('process', 'logfile', fallback=None))
if not os.path.isdir(config.get('paths', 'spooldir')):
raise Exception('spooldir (%s) not found' % config.get('paths', 'spooldir'))
spooldir = {}
for folder in ['tmp', 'state', 'original', 'processed']:
spooldir[folder] = os.path.abspath(config.get('paths', folder,
fallback=os.path.join(config.get('paths', 'spooldir'), folder)))
os.makedirs(spooldir[folder], exist_ok=True)
productions = []
for inputfile in sys.argv[1:]:
if not os.path.isfile(inputfile):
logging.error('skipping %s as it is not a regular file' % inputfile)
continue
logging.info('Processing %s' % inputfile)
backupfile = os.path.join(spooldir['original'], os.path.basename(inputfile))
logging.debug('Backing up original to %s' % backupfile)
try:
shutil.copy2(inputfile, backupfile)
except OSError as e:
logging.exception('Failed to backup up original file to %s: %s' % (backupfile, e))
exit(1)
logging.debug('creating auphonic production')
try:
production = AuphonicProduction.new(auphonic_accesstoken, preset=config.get('auphonic', 'preset'),
webhook=config.get('auphonic', 'webhook', fallback=None))
except RuntimeError as e:
logging.exception('Failed to create auphonic production: %s' % e)
exit (1)
productiondir = os.path.join(spooldir['tmp'], production.uuid)
statefile = os.path.join(spooldir['state'], '%s.json' % production.uuid)
logging.debug('created auphonic production with uuid %s, creating tmp dir %s and statefile %s'
% (production.uuid, productiondir, statefile))
try:
os.mkdir(productiondir)
except OSError as e:
logging.exception('Failed to create productiondir %s: %s' % (productiondir, e))
exit(1)
try:
statefh = open(statefile, 'w')
except OSError as e:
logging.exception('Failed to create statefile %s: %s', (statefile, e))
exit(1)
uploadfile = backupfile
fileformat = inputfile.split('.')[-1]
soxinputargs = config.get('sox', 'fileformat_%s' % fileformat, fallback=None )
soxoutputfile = None
if soxinputargs is not None:
soxoutputfile = '%s.%s' % (os.path.basename(inputfile).rsplit('.', maxsplit=1)[0],
config.get('sox', 'outputfiletype', fallback='flac'))
soxoutputfile = os.path.join(productiondir, soxoutputfile)
logging.debug('sox outputfile: %s' % soxoutputfile)
soxargs = [config.get('sox', 'sox', fallback='sox')]
soxargs += shlex.split(soxinputargs)
soxargs.append(uploadfile)
soxargs += shlex.split(config.get('sox', 'output', fallback='-t %s "%s"')
% (soxoutputfile.split('.')[-1], shlex.quote(soxoutputfile)))
logging.debug('running %s' % ' '.join(soxargs))
try:
sox = subprocess.run(soxargs, check=True)
except subprocess.CalledProcessError as e:
logging.exception('Failed to run sox, cmdline: %s, stderr: %s, stdout:%s' %
(e.cmd, (e.stderr if not None else ''), (e.output if not None else '')))
exit(1)
uploadfile = soxoutputfile
logging.debug('Uploading %s' % uploadfile)
try:
production.upload(uploadfile)
except RuntimeError as e:
logging.exception('failed to upload %s to auphonic production %s: %s' % (uploadfile, production.uuid, e))
exit(1)
logging.debug('starting auphonic production')
try:
production.start()
except RuntimeError as e:
logging.exception('failed to start auphonic production %s: %s' % (production.uuid, e))
exit(1)
state = {
'uuid': production.uuid,
'inputfile': inputfile,
'backupfile': backupfile,
'sox': (soxinputargs is not None),
'soxinputargs': soxinputargs,
'soxoutputfile': soxoutputfile,
}
json.dump(state, statefh)
statefh.close()
productions.append(production)
print(' '.join(p.uuid for p in productions))