package com.examples;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.util.HashMap;
import java.util.Map;

public class ResourcesCensor {
    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static final String ACCESS_KEY = "填写你们自己的ak";
    private static final String SECRET_KEY = "填写你们自己的sk";
    private final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    private final Client client = new Client();

    public static void main(String args[]) {
        ResourcesCensor resourcesCensor = new ResourcesCensor();
        String result;
        try {
            result = resourcesCensor.ImageCensor();
            //result = resourcesCensor.VideoCensor();
            System.out.printf(result);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }

    //参考api文档 https://developer.qiniu.com/dora/manual/4252/image-review
    //图片审核
    public String ImageCensor() throws QiniuException {
        // 构造post请求body
        Gson gson = new Gson();

        Map<String, Object> uri = new HashMap<>();
        uri.put("uri", "http://oayjpradp.bkt.clouddn.com/Audrey_Hepburn.jpg");

        Map<String, Object> scenes = new HashMap<>();
        //pulp 黄  terror 恐  politician 敏感人物
        String[] types = {"pulp", "terror", "politician", "ads"};
        scenes.put("scenes", types);

        Map params = new HashMap();
        params.put("data", uri);
        params.put("params", scenes);

        String paraR = gson.toJson(params);
        byte[] bodyByte = paraR.getBytes();

        // 接口请求地址
        String url = "http://ai.qiniuapi.com/v3/image/censor";

        return post(url, bodyByte);
    }

    //参考api文档 https://developer.qiniu.com/dora/manual/4258/video-pulp
    //视频审核
    public String VideoCensor() throws QiniuException {
        // 构造post请求body
        Gson gson = new Gson();

        Map bodyData = new HashMap();

        Map<String, Object> uri = new HashMap<>();
        uri.put("uri", "https://mars-assets.qnssl.com/scene.mp4");

        Map<String, Object> params = new HashMap<>();

        Map<String, Object> scenes = new HashMap<>();
        //pulp 黄  terror 恐  politician 敏感人物
        String[] types = {"pulp", "terror", "politician"};

        Map<String, Object> cut_param = new HashMap<>();
        cut_param.put("interval_msecs", 500);

        params.put("scenes", types);
        params.put("cut_param", cut_param);

        bodyData.put("data", uri);
        bodyData.put("params", params);
        String paraR = gson.toJson(bodyData);
        byte[] bodyByte = paraR.getBytes();

        // 接口请求地址
        String url = "http://ai.qiniuapi.com/v3/video/censor";
        return post(url, bodyByte);
    }

    private String post(String url, byte[] body) throws QiniuException {
        String accessToken = (String) auth.authorizationV2(url, "POST", body, "application/json")
                .get("Authorization");

        StringMap headers = new StringMap();
        headers.put("Authorization", accessToken);
        
        com.qiniu.http.Response resp = client.post(url, body, headers, Client.JsonMime);
        return resp.bodyString();
    }

}

@@ -682,16 +682,18 @@
         
+//
 result =
@@ -728,35 +728,359 @@
 ();%0A            
-//r
+result = resourcesCensor.VideoCensor();%0A            System.out.println(result);%0A%0A            /* %E5%8F%AA%E6%9C%89%E8%A7%86%E9%A2%91%E5%AE%A1%E6%A0%B8%E6%89%8D%E4%BC%9A%E8%BF%94%E5%9B%9EjobID */%0A            Gson gson = new Gson();%0A            Map%3CString, String%3E jobMap = new HashMap();%0A            String jobID = (String) gson.fromJson(result, jobMap.getClass()).get(%22job%22);%0A            String videoCensorR
 esult = resource
@@ -1079,32 +1079,35 @@
 resourcesCensor.
+get
 VideoCensor();%0A 
@@ -1093,33 +1093,51 @@
 r.getVideoCensor
-(
+ResultByJobID(jobID
 );%0A            S
@@ -1155,11 +1155,23 @@
 rint
-f(r
+ln(videoCensorR
 esul
@@ -3149,24 +3149,625 @@
 te);%0A    %7D%0A%0A
+    /**%0A     *%0A     * @param ID : %E8%A7%86%E9%A2%91%E5%AE%A1%E6%A0%B8%E8%BF%94%E5%9B%9E%E7%9A%84 job ID%0A     *%0A     */%0A    public String getVideoCensorResultByJobID(String ID)%7B%0A        String url = %22http://ai.qiniuapi.com/v3/jobs/video/%22.concat(ID);%0A        String accessToken = (String) auth.authorizationV2(url).get(%22Authorization%22);%0A        StringMap headers = new StringMap();%0A        headers.put(%22Authorization%22, accessToken);%0A%0A        try %7B%0A            com.qiniu.http.Response resp = client.get(url,headers);%0A            return resp.bodyString();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A    %7D%0A%0A
     private 
@@ -3819,32 +3819,32 @@
 iniuException %7B%0A
-
         String a
@@ -4061,24 +4061,16 @@
 Token);%0A
-        
 %0A       

add new example for query videCensorResult after videoCesnor [ci skip]