This repository has been archived by the owner on Jan 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
war.py
283 lines (203 loc) · 8.48 KB
/
war.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from flask import Flask, render_template, request, abort, json
from flask_httpauth import HTTPBasicAuth
from flask_misaka import Misaka
from urllib.parse import quote_plus
from werkzeug.exceptions import HTTPException
import gauges
import os
# -----------------------------------------------------------
# Boot
app = Flask(__name__, static_url_path='')
app.config.from_pyfile('config.py')
Misaka(app)
gauges.TOKEN = app.config['GAUGES']['API_TOKEN']
if not app.config['DEBUG']:
from bugsnag.handlers import BugsnagHandler
from bugsnag.flask import handle_exceptions
import bugsnag
import bugsnag_client
bugsnag_client.API_KEY = app.config['BUGSNAG']['ORG_API_KEY']
bugsnag.configure(api_key=app.config['BUGSNAG']['NOTIFIER_API_KEY'])
handle_exceptions(app)
app.logger.addHandler(BugsnagHandler())
else:
import logging
from logging.handlers import RotatingFileHandler
formatter = logging.Formatter("[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
handler = RotatingFileHandler(os.path.join(app.config['LOGS_PATH'], 'app.log'), maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
app.logger.addHandler(handler)
auth = HTTPBasicAuth()
import sample_store
from utils import *
# -----------------------------------------------------------
# CLI commands
@app.cli.command()
def samples_recognize():
"""Samples recognization worker."""
queue = get_queue()
queue.watch('war-samples-recognize')
push = get_push()
db = get_database()
app.logger.info('Initialized')
# For each beanstalk jobs
while True:
job = queue.reserve()
if not job:
app.logger.error('Unable to reserve job')
continue
try:
job_data = json.loads(job.body)
except Exception as e:
job.delete()
app.logger.error(e)
continue
sample_id = job_data['sample_id']
# Get the sample in the database
sample = get_one_sample_by_id(db, sample_id)
if sample is None:
job.delete()
app.logger.error('The sample {} does not exists in the database'.format(sample_id))
continue
# Pusher channel where updates will be pushed
push_channel = 'results-{}'.format(sample_id)
sample_file_path = sample_store.get_local_path(sample_id)
# Upload the sample to the object storage if it's not already done
if sample['file_url'] is None and os.path.exists(sample_file_path):
try:
with open(sample_file_path, 'rb') as sample_file:
sample_store.save_remotely(sample_file, sample_id)
sample_file_url = sample_store.get_remote_path(sample_id)
update_one_sample(db, sample_id, {'$set': {'file_url': sample_file_url}})
sample['file_url'] = sample_file_url
push.trigger(push_channel, 'can-play', {'file_url': sample_file_url})
except Exception as e:
app.logger.error(e)
elif not os.path.exists(sample_file_path) and sample['file_url']:
try:
sample_store.download_from_remote(sample_id)
except Exception as e:
job.delete()
app.logger.error(e)
continue
elif os.path.exists(sample_file_path) and sample['file_url']:
# Do nothing
pass
else:
job.delete()
app.logger.error('The sample {} doesn\'t have file to recognize at all'.format(sample_id))
continue
audio_databases = get_enabled_audio_databases(db)
there_were_errors = False
# For each enabled audio database
for audio_database_id, audio_database_instance in audio_databases.items():
if audio_database_id not in sample:
continue
# Launch recognization process
try:
recognization = audio_database_instance.recognize(sample_id)
update_one_sample(db, sample_id, {'$set': {audio_database_id: recognization}})
sample[audio_database_id] = recognization
if recognization['status'] == 'success': # The audio database have found a matching
db.stats.update_one({'audio_database': audio_database_id}, {'$inc': {'successes': 1}})
search_terms = []
if 'artist' in recognization['data']:
search_terms.append(recognization['data']['artist'])
if 'title' in recognization['data']:
search_terms.append(recognization['data']['title'])
recognization['data']['search_terms'] = quote_plus(' '.join(search_terms))
elif recognization['status'] == 'failure': # The audio database doesn't found anything
db.stats.update_one({'audio_database': audio_database_id}, {'$inc': {'failures': 1}})
elif recognization['status'] == 'error': # There were an error with the audio database
there_were_errors = True
app.logger.error(recognization['data']['message'])
except Exception as e:
there_were_errors = True
update_one_sample(db, sample_id, {
'$set': {audio_database_id: {
'status': 'error',
'data': {'message': str(e)}
}}
})
sample[audio_database_id] = {
'status': 'error',
'data': {'message': str(e)}
}
app.logger.error(e)
# TODO
# Here we'll normally make some fuzzy string matching between all results to set the final
# recognized result. But as we only have one audio database to search in, we'll force the final
# result to be the one returned by our only audio database.
update_one_sample(db, sample_id, {'$set': {'done': True, 'final_result': 'ACRCloud'}})
sample['done'] = True
sample['final_result'] = 'ACRCloud'
if sample[sample['final_result']]['status'] == 'success':
push.trigger(push_channel, 'success', sample[sample['final_result']]['data'])
elif sample[sample['final_result']]['status'] == 'failure':
push.trigger(push_channel, 'failure', {})
elif sample[sample['final_result']]['status'] == 'error':
push.trigger(push_channel, 'error', sample[sample['final_result']]['data'])
# End TODO
job.delete()
if not there_were_errors:
sample_store.delete_locally(sample_id)
else:
app.logger.info('There were errors')
# -----------------------------------------------------------
# Hooks
@app.before_request
def define_globals():
g.INCLUDE_WEB_ANALYTICS = not app.config['DEBUG']
g.NO_INDEX = False
g.SHOW_MANAGE_BAR = False
@app.before_request
def check_under_maintenance():
if os.path.exists('maintenance') and not request.path.startswith('/manage'):
abort(503)
@app.before_request
def manage_area():
if request.path.startswith('/manage'):
g.INCLUDE_WEB_ANALYTICS = False
g.NO_INDEX = True
@app.before_request
def show_manage_bar():
if auth.username() != '' and auth.username() != None:
g.SHOW_MANAGE_BAR = True
@app.teardown_appcontext
def close_database(error):
if hasattr(g, 'database'):
g.database.client.close()
@app.teardown_appcontext
def close_queue(error):
if hasattr(g, 'queue'):
g.queue.close()
@auth.get_password
def get_password(username):
if username in app.config['MONITORING_USERS']:
return app.config['MONITORING_USERS'].get(username)
return None
@auth.error_handler
def auth_error():
return http_error_handler(403, without_code=True)
# -----------------------------------------------------------
# HTTP errors handler
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(500)
@app.errorhandler(503)
def http_error_handler(error, without_code=False):
if isinstance(error, HTTPException):
error = error.code
else:
error = 500
g.INCLUDE_WEB_ANALYTICS = False
g.NO_INDEX = True
ret = (render_template('errors/{}.html'.format(error)),)
if not without_code:
ret = ret + (error,)
return ret
# -----------------------------------------------------------
# Routes
import routes