admin
2021-02-19 58577bae968f2a10232bc8b3c04910b93ea3c69a
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
package com.yeshi.buwan.acFun;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.service.imp.AcTypeEqVideoTypeService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.imp.juhe.AcfunVideoNewService;
import com.yeshi.buwan.util.StringUtil;
 
@Component
public class AcFunUtil {
 
    public final static int RESOURCE_ID = 21;
    public final static String RESOURCE_NAME = "AcFun";
 
    @Resource
    private AcTypeEqVideoTypeService acTypeEqVideoTypeService;
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private AcfunVideoNewService acfunVideoNewService;
 
    /**
     * acFun添加视频
     */
    public void addVideo(AcfunVideoNew acfunVideo) {
        if (acfunVideoNewService.isAddToVideo(acfunVideo.getId()))
            return;
        try {
            VideoInfo videoInfo = convert(acfunVideo);
            saveVideoInfo(videoInfo, acfunVideo.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void saveVideoInfo(VideoInfo videoInfo, String id) {
        if (videoInfo == null)
            return;
        VideoInfo vi = videoInfoService.getVideoInfo(videoInfo.getName(), videoInfo.getVideoType());
        if (vi == null) {
            List<VideoResource> resourList = new ArrayList<VideoResource>();
            resourList.add(new VideoResource("21"));
            videoInfo.setResourceList(resourList);
            videoInfoService.save(videoInfo);
            acfunVideoNewService.addAcfunVideoMap(id, Long.parseLong(videoInfo.getId()));
        }
    }
 
    private VideoInfo convert(AcfunVideoNew acFunVideo) {
        VideoInfo vi = new VideoInfo();
        String playUrl = acFunVideo.getPlayUrl();
        Long updateTime = acFunVideo.getUpdateTime();
        String videoDesc = acFunVideo.getDesc();
        Integer videoNum = 1;
        String videoTitle = acFunVideo.getTitle();
        String thumbImg = acFunVideo.getCover();
        Map<Integer, Long> typeMap = getAcfunVideoTypeMap();
        Long type = typeMap.get(acFunVideo.getChannelIds().get(0));
        if (type == null) {
            return null;
        }
 
        VideoType vt = new VideoType(type);
 
        int acfunType = acFunVideo.getChannelIds().get(0);
 
        // acfun地域添加
        if (acfunType == 195) {
            vi.setArea("日本");
        } else if (acfunType == 120) {
            vi.setArea("内地");
        } else if (acfunType == 67 || acfunType == 180) {
            vi.setArea("日本");
        }
 
        vi.setName(videoTitle);
        vi.setPicture(thumbImg);
        vi.setUpdatetime(updateTime + "");
        vi.setVideoType(vt);
        vi.setIntroduction(videoDesc);
        vi.setBaseurl(playUrl);
        vi.setShow(1 + "");
        vi.setHpicture(thumbImg);
        vi.setVpicture(thumbImg);
        vi.setLatestHpicture(thumbImg);
        vi.setLatestVpicture(thumbImg);
        vi.setCreatetime(System.currentTimeMillis());
        vi.setVideocount(1);
        vi.setContentType(2);
        vi.setTag("");
        vi.setWatchCount(0 + "");
        vi.setOrderby(1 + "");
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(updateTime);
        vi.setYear(instance.get(Calendar.YEAR) + "");
        vi.setDirector("");
        return vi;
    }
 
    static Map<Integer, Long> typeMap = null;
 
    public static Map<Integer, Long> getAcfunVideoTypeMap() {
        if (typeMap != null)
            return typeMap;
        typeMap = new HashMap<>();
        Scanner scanner = new Scanner(AcFunUtil.class.getClassLoader().getResourceAsStream("acfun_class_map.txt"));
        while (scanner.hasNextLine()) {
            String content = scanner.nextLine();
            if (content != null && content.indexOf("[") > -1 && content.indexOf("]") > -1) {
                Long videoType = Long.parseLong(content.substring(0, content.indexOf("[")).trim());
                String acfunTypeStr = content.substring(content.indexOf("[") + 1, content.indexOf("]"));
                String[] sts = acfunTypeStr.split(" ");
                for (String st : sts) {
                    if (!StringUtil.isNullOrEmpty(st)) {
                        int acfunType = Integer.parseInt(st.trim());
                        typeMap.put(acfunType, videoType);
                    }
                }
            }
        }
        scanner.close();
        return typeMap;
    }
}
 
class MyType extends TypeToken<List<AcFunVideo>> {
 
}