2020年11月24日

批量提交视频检测任务以及查询结果的脚本

点击数:3

#!/bin/bash

task_file=task_files.txt
fail_url=fail_url.txt

usage() {
    echo "========== Usage: 0 [submit|query] =========="
    echo "========== 【submit】提交任务,会生成{task_file}文件且不要随意删除=========="
    echo "========== 【query】查询任务结果 =========="
    echo "========== 不填写submit或query表示提交并查询 =========="

}

submit() {
    echo '==============开始提交视频=================='
    submit_url='https://xxx.xxx.cn/video/submit/v1'
    access_key="abc"
    secret_key="8d18be16df6d44fca3292ab3c5ebd0e7"
    event_id="video_m"
    # video_url="http://1500000267.vod2.myqcloud.com/6c977a17vodcq1500000267/0e5bbd4d5285890806067732732/f0.mp4"
    scenes="porn,terrorism,face,ocr"
    echo -e "\033[32m请输入视频URL文件提交任务(如 ./url.txt):\033[0m\c"
    read urls

    rm task_file &> /dev/null

    count=0
    count_fail=0

    if [[ -furls ]]
    then
        while read -r video_url
        do
            res=`curl -s --location --request POST "submit_url" \
            --header "x-access-key:access_key" \
            --header "x-secret-key: secret_key" \
            --header 'Content-Type: application/x-www-form-urlencoded' \
            --data-urlencode "event_id={event_id}" \
            --data-urlencode "url={video_url}" \
            --data-urlencode "scenes={scenes}"`
            # res='{"reason_code":200,"success":true,"task_id":"1604282130529000Y8C7B0C640028001"}'
            reason_code=(echores | jq .reason_code)
            if [[ reason_code -eq 200 ]]
            then
                r=`jq -c -n "res + {"url":"video_url"}"`
                echo  "r" >> task_file
                count=[count + 1]
            else
                echovideo_url >> fail_url
                count_fail=`exprcount_fail + 1`
            fi
        done < ${urls}
        echo '===本次共提交【${count}】个任务,其中有【${count_fail}】个任务失败===='
        echo "=== 成功任务保存在${task_file}中,失败URL保存在${fail_url}中 ===="
        echo '===============提交结束===================='
    fi

}

query() {
    echo '================查询结果==================='
    query_url='https://xxx.xxx.cn/video/query/v1'
    access_key="abc"
    secret_key="8d18be16df6d44fca3292ab3c5ebd0e7"
    event_id="video_m"
    result_file=result_file.txt

    rm $result_file &> /dev/null

    count=0
    total=0

    if [[ -f task_file ]]
    then
        while read -r task
        do
            reason_code=(echo task | jq .reason_code)
            task_id=(echo task | jq .task_id |  xargs echo )

            if [[reason_code = '200' ]]
            then
                res=`curl -s --location --request POST "query_url" \
                --header "x-access-key:access_key" \
                --header "x-secret-key: secret_key" \
                --header 'Content-Type: application/x-www-form-urlencoded' \
                --data-urlencode "task_id={task_id}"`

                res_reason_code=(echores | jq .reason_code | xargs echo )
                process=(echores | jq .process | xargs echo)
                url=(echotask | jq .url | xargs echo)
                des=(echores | jq .final_decision|xargs echo)
                # total=[total+1]
                let total++
                # let total+=1
                if [[ "res_reason_code" = "200" ]] && [[ "process" = "100" || "process" = "100.0" ]]
                then
                    printf "%s\t%s\t%s\n"task_id desurl >> result_file
                    count=((count + 1))
                else
                    printf "还未出结果:%s\t%s\n"task_id  $url
                fi
            fi

        done < $task_file
    else
        echo "任务文件不存在"
    fi
    echo "======共查询【${total}】任务,【${count}】条任务有结果======"
    echo '===============查询结束===================='
}

install_jq() {
    a=`uname  -a`

    b="Darwin"
    c="centos"
    d="ubuntu"

    which jq &> /dev/null
    if [[ ? -ne 0 ]]
    then

        if [[a =~ b ]];then
            brew install jq &> /dev/null
            if [[? -ne 0 ]]
            then
                echo 'Mac安装Json工具失败,请手工安装,安装命令:brew install jq';
                exit 1;
            fi
        elif [[ a =~c ]];then
            yum install jq &> /dev/null
            if [[ ? -ne 0 ]]
            then
              echo 'Centos安装JSON工具失败,请手工安装,安装命令:yum install jq'
              exit 1;
            fi
        elif [[a =~ d ]];then
            apt-get install jq &> /dev/null
            if [[? -ne 0 ]]
            then
                echo 'Ubuntu安装JSON工具失败,请手工安装,安装命令:apt-get install jq'
                exit 1
            fi
        else
            echo "{a}安装JSON工具失败,请手工安装"
            exit 1
        fi
    else
        echo '当前操作系统已安装JSON工具'
    fi
}

main() {
    echo@
    # usage
    install_jq
    submit
    sleep 5
    query
}

if [[ # -eq 1 ]]
then
    case "1" in
        "submit")
            submit
            ;;
        "query")
            query
            ;;
        *)
            usage
            ;;
    esac
else
    main
fi

发表评论