AntiFraudUtil.java 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
package com.cnlive.shenhe.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

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

import javax.swing.plaf.basic.BasicScrollPaneUI.HSBChangeListener;


/**
 * 数美 反欺诈工具类
 *
 * @author GuoYaqi
 */
public class AntiFraudUtil {

    //把变量配置到属性文件

    private static final String ACCESSKEY = "DU0tyaqrO5Vuiuiq4i8E"; // 商户的 accessKey
    private static final String REQUEST_URL_TEXT = "http://api.fengkongcloud.com/v2/saas/anti_fraud/text";
    private static final String REQUEST_URL_ARTICLE = "http://img-api.fengkongcloud.com/v1/saas/anti_fraud/article";
    private static final String REQUEST_URL_IMG = "http://img-api-bj.fengkongcloud.com/v2/saas/anti_fraud/imgs";
    private static final String REQUEST_URL_VIDEO = "http://video-api-bj.fengkongcloud.com/v2/saas/anti_fraud/video";
    private static final String RESPONSE_URL_VIDEO = "http://video-api-bj.fengkongcloud.com/v2/saas/anti_fraud/query_video";
    private static final String REQUEST_URL_VIDEOSTREAM = "http://video-api.fengkongcloud.com/v2/saas/anti_fraud/videostream";
    private static final String RESPONSE_URL_VIDEOSTREAM = "http://video-api.fengkongcloud.com/v2/saas/anti_fraud/query_videostream";
    private static final String FINISH_URL_VIDEOSTREAM = "http://video-api.fengkongcloud.com/v2/saas/anti_fraud/finish_videostream";
   
    
    private static final String Contents_list = "https://www.fengkongcloud.com/ApiV2/ListService/lists";
    
    public static void main(String[] args) {
//        String jsonArray = "[{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},\n" +
//                "{\"imageUrl\":\"https://wjj.ys1.cnliveimg.com/769/img/2018/0129/fitnessPhotos.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},\n" +
//                "{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"}]";
//        UUID uuid = UUID.randomUUID();
//        String btId = uuid.toString();

//        System.out.println("文本:"+AntiFraudUtil.filterText("0000001", "王八蛋", "DYNAMIC_USER") + "---");
//        System.out.println(AntiFraudUtil.filterArticle("000111","http://3g.huabian.com/api/hz0227/show/388865.html",null));
        //System.out.println("图片:"+AntiFraudUtil.filterImg("0000001", jsonArray,"DYNAMIC_BUSINESS") + "---");
        //System.out.println("视频:"+AntiFraudUtil.filterVideo("http://wjj.ys1.cnliveimg.com/testVideo/test1.mp4",btId,null) + "---");
        //queryVideoResult("02b649b7-3451-403e-abd0-592302cec2a0");

try {
//	Map<Object, Object> hashMap = new HashMap<Object, Object>();
//	hashMap.put("11", "1111");
//	String object = (String) hashMap.get("11");
//	String object2 = (String) hashMap.get("22");
//	System.out.println(object);
//	System.out.println(object2);

//	System.out.println(addBlackContents("2222"));
} catch (Exception e) {
	e.printStackTrace();
}
//System.out.println(contentsList());
//System.out.println(blackContentsList());
    }

    /**
     * 智能文本过滤
     *
     * @param text 需要过滤的文本
     * @param tokenId 建议传UID
     * @param channel COMMENT,DYNAMIC_BUSINESS,DYNAMIC_USER,不传为COMMENT
     * @return 通过:PASS  拒绝:REJECT  可疑:REVIEW  数美异常:null
     */
    public static JSONObject filterText(String tokenId, String text, String channel) {
        try {
            JSONObject result = reqTextFilter(tokenId, text, channel);
            
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            if (result != null) {
                Integer code = result.getInteger("code");
                
                if (code != null && code == 1100) {  //请求成功
                	String riskLevel = result.getString("riskLevel");
                	
                	if ("PASS".equals(riskLevel)) {
                		return result;
                	}
                	
                	if ("REJECT".equals(riskLevel)) {
                		JSONObject detail = result.getJSONObject("detail");

                		result.put("desc",
                				String.format("(%s)", detail.getString("riskTypeDesc")));
                		return result;
                	}
                	
                	if ("REVIEW".equals(riskLevel)) {
                		return result;
                	}
                }
            }
            return null;
        } catch (Exception e) {
        	return null;
        }
    }


    /**
     * 智能图文过滤
     * @param channel COMMENT,NEWS(新闻),CULTURE_SPORTS(娱乐/体育),不传为COMMENT
     * @param contents 要检测的网页内容,url链接或文本内容
     * @param tokenId 建议传UID
     * @return 通过:PASS  拒绝:REJECT  可疑:REVIEW  数美异常:null
     */
    public static JSONObject filterArticle(String tokenId, String contents ,String channel) {
        try {
            JSONObject result = reqArticleFilter(tokenId, contents ,channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            return getResult(result);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 智能图片过滤
     *
     * @param picArray    需要过滤的图片的集合
     * @param tokenId 建议传UID
     * @return 通过:PASS  拒绝:REJECT  异常:REVIEW
     */
    public static JSONObject filterImg(String tokenId, String picArray, String channel) {
        try {
            JSONArray imgArray = getJSONArray(picArray);
            JSONObject result = reqImgFilter(tokenId, imgArray, channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            if (result != null) {
                System.out.println("数美图片过滤返回结果" + result);
                JSONObject jsonObj = new JSONObject();
                StringBuffer strBuffer = new StringBuffer();
                int count = 0;
                Integer code = result.getInteger("code");
                if (code != null && code == 1100) { //请求成功
                    JSONArray jsonArray = result.getJSONArray("imgs");
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject json = jsonArray.getJSONObject(i);
                            String riskLevel = json.getString("riskLevel");

                            if ("PASS".equals(riskLevel)) {
                                continue;
                            }

                            if ("REJECT".equals(riskLevel) || "REVIEW".equals(riskLevel)) {
                                JSONObject detail = json.getJSONObject("detail");
                                for (int j = 0; j < imgArray.size(); j++) {
                                    String btId = json.getString("btId");
                                    if (imgArray.getJSONObject(j).containsValue(btId)) {
                                        strBuffer.append("(第" + (j + 1) + "张-" + detail.getString("description") + ")");
                                    }
                                }
                                jsonObj.put("desc", strBuffer);
                                count++;
                                continue;
                            }
                        }
                    }
                    if (count > 0) {
                        jsonObj.put("type", "REJECT");
                    } else {
                        jsonObj.put("type", "PASS");
                    }
                }else{//数美请求结果code为失败状态
                	jsonObj.put("type", "REJECT");
                	jsonObj.put("desc","code:"+code+",message:"+result.getString("message") );
                }
                return jsonObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 智能视频过滤
     *
     * @param url    需要过滤的视频的url
     * @param uuid    需要过滤的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject filterVideo(String url,String uuid, String channel) {
        try {
            JSONObject result = reqVideoFilter(url, uuid, channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 智能视频过滤
     *
     * @param uuid    需要查看的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject queryVideoResult(String uuid) {
        try {
            JSONObject userData = new JSONObject();
            userData.put("accessKey", ACCESSKEY);
            userData.put("btId", uuid);
            JSONObject result = HttpClientUtils.httpPost(RESPONSE_URL_VIDEO, userData,false);
            System.out.println("响应:" + result);
            return result;
        } catch (Exception e) {
            System.out.println("...................接口请求失败:");
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 智能视频流过滤(直播)
     *
     * @param url    需要过滤的视频的url
     * @param uuid    需要过滤的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject filterVideoStream(String url,String uuid,String videoName, String channel) {
    	try {
    		JSONObject result = reqVideoFilterStream(url, uuid,videoName, channel);
    		/**
    		 * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
    		 * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
    		 * 其余情况需要根据错误码进行重试或者其它异常处理
    		 */
    		return result;
    	} catch (Exception e) {
    		return null;
    	}
    }
    
    /**
     * 智能视频流过滤(直播)
     *
     * @param uuid    需要查看的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject queryVideoStreamResult() {
    	try {
    		JSONObject userData = new JSONObject();
    		userData.put("accessKey", ACCESSKEY);
    		JSONObject result = HttpClientUtils.httpPost(RESPONSE_URL_VIDEOSTREAM, userData,false);
    		System.out.println("响应:" + result);
    		return result;
    	} catch (Exception e) {
    		System.out.println("...................接口请求失败:");
    		e.printStackTrace();
    		return null;
    	}
    }
    
    /**
     * 视频流关闭(直播--关闭)
     *
     * @param url    关闭的视频流的地址
     * @param requestId    关闭的视频流的requestId
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject finishVideoStream(String url,String requestId) {
    	try {
    		JSONObject result = reqFinishVideoStream(url, requestId);
    		/**
    		 * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
    		 * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
    		 * 其余情况需要根据错误码进行重试或者其它异常处理
    		 */
    		return result;
    	} catch (Exception e) {
    		return null;
    	}
    }



    private static JSONObject reqTextFilter(String tokenId, String text, String channel) throws Exception {
        JSONObject userData = new JSONObject();

        userData.put("accessKey", ACCESSKEY);
        userData.put("type", "SOCIAL");

        JSONObject data = new JSONObject();
        data.put("tokenId", tokenId);
        data.put("text", text);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_TEXT, userData,false);
    }

    private static JSONObject reqImgFilter(String tokenId, JSONArray imgs, String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);
        userData.put("type", "AD_PORN_PERSON");

        JSONObject data = new JSONObject();
        data.put("tokenId", tokenId);

        data.put("imgs", imgs);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_IMG, userData,false);
    }

    private static JSONObject reqVideoFilter(String url,String uuid, String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);
        userData.put("btId", uuid);
        userData.put("callback","http://apiwjjtest.cnlive.com/Daren/filterVideoResult/callbackVideoResult.action");
        userData.put("type", "AD_PORN_PERSON_VIOLENCE");


        JSONObject data = new JSONObject();
        data.put("url", url);
        data.put("addition","AUDIO");
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_VIDEO, userData,false);
    }
    
    private static JSONObject reqVideoFilterStream(String url,String uuid,String videoName, String channel) throws Exception {
    	JSONObject userData = new JSONObject();
    	userData.put("accessKey", ACCESSKEY);
//    	userData.put("btId", uuid);
    	userData.put("callback","http://test.shen.cnlive.com/api/videoStream/callbackVideoStreamResult");
    	userData.put("type", "DEFAULT");
    	
    	
    	JSONObject data = new JSONObject();
    	data.put("url", url);
    	data.put("VideoName", videoName);
    	data.put("callbackLevel", true);//true表示返回所有图片的审核结果,false表示只返回非pass的审核结果
    	data.put("detectFrequency",10);//视频流截帧频率范围:0.5-60(0.5秒一帧-60秒一帧),默认为5
    	data.put("tokenId", uuid);//客户端用户账号唯一标识,用于用户行为分析,建议传入用户UID;
//    	data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
    	data.put("channel", "VIDEO_LIVE");
    	userData.put("data", data);
    	
    	return HttpClientUtils.httpPost(REQUEST_URL_VIDEOSTREAM, userData,false);
    }
  
    private static JSONObject reqFinishVideoStream(String url,String requestId) throws Exception {
    	JSONObject userData = new JSONObject();
    	userData.put("accessKey", ACCESSKEY);
    	userData.put("requestId",requestId);
    	userData.put("url", url);
    	
    	return HttpClientUtils.httpPost(FINISH_URL_VIDEOSTREAM, userData,false);
    }

    private static JSONObject reqArticleFilter(String tokenId,String contents ,String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);

        JSONObject data = new JSONObject();
        data.put("contents", contents);
        data.put("tokenId", tokenId);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "NEWS");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_ARTICLE, userData,false);
    }

    private static JSONObject getResult(JSONObject result) {
    	
        if (result != null) {
        	System.out.println("数美过滤返回结果" + result);
        	JSONObject jsonObj = new JSONObject();
            Integer code = result.getInteger("code");

            if (code != null && code == 1100) {  //请求成功
                String riskLevel = result.getString("riskLevel");

                if ("PASS".equals(riskLevel)) {
                	jsonObj.put("type", "REJECT");
                    return result;
                }

                if ("REJECT".equals(riskLevel)) {
                    JSONObject detail = result.getJSONObject("detail");

                    result.put("desc", String.format("(%s)", detail.getString("description")));
                    return result;
                }

                if ("REVIEW".equals(riskLevel)) {
                    return result;
                }
            }
        }
        return null;
    }

    private static JSONArray getJSONArray(String json){
        JSONArray picJson = JSON.parseArray(json);
        JSONArray imgs = new JSONArray();
        for (int i = 0; i < picJson.size(); i++) {
            UUID uuid = UUID.randomUUID();
            String btId = uuid.toString();
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObj = picJson.getJSONObject(i);
            String img = jsonObj.getString("imageUrl");
            jsonObject.put("img",img);
            jsonObject.put("btId",btId);
            imgs.add(jsonObject);
        }
        return imgs;
    }
    /**
     * 名单列表查询
     * @return
     */
    private static String contentsList()  {

    	String url=Contents_list+"?accessKey="+ACCESSKEY+"&type=1&serviceId=POST_TEXT";
        return HttpClientUtils.get(url);
    }
    /**
     * 名单内容列表查询(敏感词黑名单)
     * @return
     */
    private static String blackContentsList()  {
    	
    	String url="https://www.fengkongcloud.com/ApiV2/ListService/lists/0f85d5aab7f59a6070c403c57aefcc0a/contents?accessKey="+ACCESSKEY;
    	return HttpClientUtils.get(url);
    }
    
    /**
     *  新增名单内容(敏感词黑名单)
     * @param contents
     * @return
     * @throws Exception
     */
    private static JSONObject addBlackContents(String contents) throws Exception {

        JSONObject data = new JSONObject();
        String[] contents1=new String[]{contents};
        data.put("contents", contents1);

        return HttpClientUtils.httpPost("https://www.fengkongcloud.com/ApiV2/ListService/lists/0f85d5aab7f59a6070c403c57aefcc0a/contents?accessKey="+ACCESSKEY, data,false);
    }
}