webargs 参数嵌套

复杂参数

{'data': {'errCode': 0,
        'fileId': '528589078613',
          'message': '',
          'metaData': {'audioDuration': 0,
                       'audioStreamList': [{'bitrate': 0,
                                            'codec': 'opus',
                                            'samplingRate': 48000}],
                       'bitrate': 0,
                       'container': 'matroa,webm',
                       'duration': 0,
                       'floatDuration': 0,
                       'height': 480,
                       'md5': '',
                       'rotate': 0,
                       'size': 30541764,
                       'totalSize': 30541764,
                       'videoDuration': 0,
                       'videoStreamList': [{'bitrate': 0,
                                            'codec': 'vp8',
                                            'fps': 1000,
                                            'height': 480,
                                            'width': 640}],
                       'width': 640},
          'name': 'no-duration',
          'processTaskList': [{'errCode': 0,           
                               'input': {'definition': 30223, 'watermark': 0},
                               'message': 'SUCCESS',
                               'output': {'audioStreamList': [{'bitrate': 48016,
                               
                                                               'codec': 'aac',
                                                               'samplingRate': 44100}],
                                          'bitrate': 247810,
                                          'container': 'mov,mp4,m4a,3gp,3g2,mj2',
                                          'definition': 30223,
                                          'duration': 329,
                                          'floatDuration': 329.9200134277344,
                                          'height': 480,
                                          'md5': '563dbf3574aa1ff7035e63e70a',
                                          'size': 10605994,
                                          'totalSize': 10605994,
                                          'url': 'http://125569333.mp4', 
                                          'videoStreamList': [{'bitrate': 199794,
                                                               'codec': 'h264',
                                                               'fps': 25,
                                                               'height': 480,
                                                               'width': 640}],
                                          'width': 640},
                               'status': 'SUCCESS',
                               'taskType': 'Transcode'}],
          'sessionContext': '',
          'status': 'FINISH',
          'tasksNotifyMode': '',
          'tasksPriority': 0,
          'url': 'http://12556933f0.mkv',
          'vodTaskId': '12556937-Proce-092fded8a3774cdf2t0'},
 'eventType': 'ProcedureStateChanged',
 'version': '4.0'
}

目标参数:
status fileid duration url taskType eventType version

from marshmallow import fields, validate, Schema
from webargs import fields as web_fields


TENCENT_TRANSCODE_ARGS = {
    "data": web_fields.Nested(
        {
            "fileId": fields.Str(required=True),
            "processTaskList": fields.List(web_fields.Nested(
                {
                    "taskType": fields.Str(required=True),
                    "status": fields.Str(required=True),
                    "output": web_fields.Nested({"url": fields.Str(required=True), "duration": fields.Int()},
                                                required=True)
                }, required=True
            ), required=True)
        }
    ),
    "eventType": fields.Str(required=True),
    "version": fields.Str(required=True)
}

@app.route('', methods=["get", 'post'])
@use_args(TENCENT_TRANSCODE_ARGS, locations=('querystring', 'json', 'form'))
def transcode_callback(args):
    # 成功
    _data = args.get("data")
    processTaskList = _data.get("processTaskList")
    fileId = _data.get("fileId")
    status = ""
    duration = 0
    url = ""
      for process in processTaskList:
        status = process.get("status", "")
        taskType = process.get("taskType", "")
        output = process.get("output", "")
        if taskType == "Transcode" and status in ["SUCCESS", "FAIL"] and output:
            url = output.get("url", "")
            duration = output.get("duration")
            break
    data = {
        "status": status,
        'fileid': fileId,
        'duration': duration,
        'url': url,
    }

你可能感兴趣的:(python)