2021-11-24 21:25:55 +01:00
|
|
|
import logging
|
2021-11-24 20:50:26 +01:00
|
|
|
from flask import Flask, request, abort, Response
|
|
|
|
import json
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2021-11-24 21:25:55 +01:00
|
|
|
app.logger.setLevel(logging.DEBUG)
|
2021-11-24 20:50:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['POST'])
|
|
|
|
def record():
|
|
|
|
if 'file' not in request.files:
|
|
|
|
return abort(400, "no file found")
|
|
|
|
else:
|
|
|
|
soundfile = request.files['file']
|
|
|
|
|
|
|
|
if 'description' not in request.form:
|
|
|
|
return abort(400, "no description found")
|
|
|
|
else:
|
|
|
|
description_raw = request.form.get("description")
|
|
|
|
|
|
|
|
if soundfile.content_type != 'audio/wave':
|
|
|
|
return abort(415, 'Input file not a wave file.')
|
|
|
|
try:
|
|
|
|
desc = json.loads(description_raw)
|
|
|
|
except:
|
|
|
|
return abort(417, 'Input JSON schema invalid')
|
|
|
|
|
|
|
|
with open("arrived.txt", "at") as f:
|
|
|
|
f.write(f"{desc['device_id']}\n")
|
|
|
|
|
2021-11-24 21:25:55 +01:00
|
|
|
app.logger.info(f"Arrived: {desc['device_id']}")
|
|
|
|
|
2021-11-24 20:50:26 +01:00
|
|
|
return Response(status=200)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0')
|