admin
2020-10-10 81db7b3b070c003e6f5f0d1c757ab30b6f42c944
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
package com.yeshi.buwan.util.video;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import com.google.gson.Gson;
import com.yeshi.buwan.acFun.AcfunVideoNew;
import com.yeshi.buwan.acFun.AcfunVideoResult;
import com.yeshi.buwan.service.imp.juhe.AcfunVideoNewService;
import com.yeshi.buwan.util.BeanUtil;
import com.yeshi.buwan.util.HttpUtil;
import com.yeshi.buwan.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class AcfunApiUtil {
 
    private static String sign(Map<String, String> map) {
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = map.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            list.add(key + map.get(key));
        }
        Collections.sort(list);
        Collections.reverse(list);
 
        String str = "";
        for (String st : list) {
            str += st;
        }
        str += "BuWanAcfun++#";
 
        return StringUtil.Md5(str).toLowerCase();
    }
 
    private static String baseRequest(String url, Map<String, String> params) {
        String sign = sign(params);
        params.put("sign", sign);
        return HttpUtil.post(url, params);
    }
 
    public static AcfunVideoResult videoList(String pcursor) {
        String url = "https://www.acfun.cn/rest/pc-direct/partner/buwan/list";
        Map<String, String> params = new HashMap<String, String>();
        params.put("pcursor", pcursor);
        params.put("count", "100");
        String result = baseRequest(url, params);
        System.out.println(result);
        JSONObject resultJSON = JSONObject.fromObject(result);
        if (resultJSON.optInt("code") == 0) {
            JSONObject data = resultJSON.optJSONObject("data");
            pcursor = data.optString("pcursor");
            JSONArray array = data.optJSONArray("results");
            List<AcfunVideoNew> list = new ArrayList<>();
            Gson gson = new Gson();
            for (int i = 0; i < array.size(); i++) {
                try {
                    list.add(gson.fromJson(array.optJSONObject(i).toString(), AcfunVideoNew.class));
                } catch (Exception e) {
                    System.out.println(array.optJSONObject(i).toString());
                    e.printStackTrace();
                }
            }
            return new AcfunVideoResult(pcursor, list);
        }
        return null;
    }
 
    public static List<Boolean> listValid(List<String> ids) {
        String url = "https://www.acfun.cn/rest/pc-direct/partner/buwan/enable";
        Map<String, String> params = new HashMap<String, String>();
        String st = "";
        for (String id : ids)
            st += id + ",";
        if (st.endsWith(","))
            st = st.substring(0, st.length() - 1);
        params.put("ids", st);
        String result = baseRequest(url, params);
        JSONObject resultJson = JSONObject.fromObject(result);
        List<Boolean> validList = new ArrayList<>();
        if (resultJson.optInt("code") == 0) {
            JSONObject data = resultJson.optJSONObject("data");
            for (String id : ids)
                validList.add(data.optBoolean(id));
        }
        return validList;
    }
 
    public static void main(String[] args) {
//        AcfunVideoNewService acfunVideoNewService = BeanUtil.getBean(AcfunVideoNewService.class);
//        AcfunVideoResult result = videoList("");
//        while (result != null && result.pcursor != null && !result.pcursor.equalsIgnoreCase("no_more")) {
//            if (result.videoList != null)
//                acfunVideoNewService.save(result.videoList);
//            result = videoList(result.pcursor);
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
 
        videoList("");
 
        // String[] sts = new String[] { "5xzzqkwh73r27gq", "5xzzknzyuvvd9vc" };
        // List<Boolean> list = listValid(Arrays.asList(sts));
        // System.out.println(list);
    }
 
 
}