This commit is contained in:
@ -1,13 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
import json
|
||||
from xeger import Xeger
|
||||
from flask_restful import Resource
|
||||
from flask import request
|
||||
from flask import request, jsonify
|
||||
import requests
|
||||
import filetype
|
||||
from db import db
|
||||
from models import SampleMetadata
|
||||
from rabbitmqqueue import rabbitmq_channel
|
||||
from schemas import SampleSchema
|
||||
from config import *
|
||||
|
||||
"""
|
||||
@ -38,39 +39,47 @@ class SampleResource(Resource):
|
||||
else:
|
||||
soundfile = request.files['file']
|
||||
|
||||
if 'date' not in request.form:
|
||||
return {"status": "error", "message": "no date found"}, 470
|
||||
if 'description' not in request.form:
|
||||
return {"status": "error", "message": "no description found"}, 470
|
||||
else:
|
||||
date = request.form.get("date")
|
||||
description = request.form.get("description")
|
||||
|
||||
if 'device_id' not in request.form:
|
||||
return {"status": "error", "message": "no device_id found"}, 471
|
||||
else:
|
||||
device_id = request.form.get("device_id")
|
||||
if soundfile.content_type != 'audio/wave':
|
||||
LOGGER.info(
|
||||
f"Input file was not WAV.")
|
||||
return {'status': 'error', 'message': 'Input file not a wave file.'}, 415
|
||||
|
||||
kind = filetype.guess(soundfile)
|
||||
if kind is None or kind.mime != 'wav':
|
||||
try:
|
||||
desc = json.loads(description)
|
||||
except Exception as e:
|
||||
LOGGER.exception(e)
|
||||
return {'status': 'error',
|
||||
'message': 'Input JSON could not be parsed'}, 400
|
||||
|
||||
validate_errors = SampleSchema().validate(desc)
|
||||
if validate_errors:
|
||||
LOGGER.error(
|
||||
f"Input file was not WAV. Recieved metadata: device_id: {device_id}")
|
||||
return {'status': 'error', 'message': 'Input file not WAV.'}, 415
|
||||
"Input JSON did not conform to schema. It was: {}", desc)
|
||||
return {'status': 'error',
|
||||
'message': 'Input JSON schema invalid'}, 417
|
||||
|
||||
xeger = Xeger(limit=32)
|
||||
xeger = Xeger(limit=30)
|
||||
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')
|
||||
|
||||
record = SampleMetadata(
|
||||
device_id=device_id,
|
||||
device_date=date,
|
||||
device_id=desc['device_id'],
|
||||
device_date=desc['date'],
|
||||
tag=generated_tag)
|
||||
try:
|
||||
db.session.add(record)
|
||||
requests.post(
|
||||
f"http://{STORAGE_HOSTNAME}/v1/storage/object",
|
||||
f"http://{STORAGE_HOSTNAME}/object",
|
||||
files={
|
||||
'tag': (None, generated_tag),
|
||||
'file': (
|
||||
'wave.wav',
|
||||
soundfile,
|
||||
kind.mime)})
|
||||
soundfile.content_type)})
|
||||
rabbitmq_channel.basic_publish(
|
||||
exchange=RABBITMQ_EXCHANGE,
|
||||
routing_key='feature',
|
||||
@ -90,7 +99,7 @@ class SampleResource(Resource):
|
||||
:return:
|
||||
"""
|
||||
samples = SampleMetadata.query.all()
|
||||
return {"status": "ok", "message": samples}, 200
|
||||
return {"status": "ok", "message": jsonify(json_list=samples)}, 200
|
||||
|
||||
|
||||
class SampleParameterResource(Resource):
|
||||
@ -105,4 +114,4 @@ class SampleParameterResource(Resource):
|
||||
:return:
|
||||
"""
|
||||
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
|
||||
return {"status": "ok", "message": sample}, 200
|
||||
return {"status": "ok", "message": jsonify(sample)}, 200
|
||||
|
Reference in New Issue
Block a user