ReportRpcService.java
7.57 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
package cn.fw.freya.service.rpc;
import cn.fw.freya.common.PathConstant;
import cn.fw.freya.model.data.pool.LivePool;
import cn.fw.freya.model.data.pool.VideoPool;
import cn.fw.freya.model.dto.rpc.LivePoolDto;
import cn.fw.freya.model.dto.rpc.ReportAccountDto;
import cn.fw.freya.model.dto.rpc.VideoPoolDto;
import cn.fw.freya.utils.RequestUtil;
import cn.fw.freya.utils.http.HttpConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author kurisu
* @date 2021-11-11 18:11
* @description 数据上报 接口
*/
@Slf4j
@Service
public class ReportRpcService {
@Getter
@Value("${freya.url}")
private String baseUrl;
/**
* 账户信息上报
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @param accountMsg 账户信息
* @return
*/
public boolean reportAccountMsg(String accountNo, Integer type, ReportAccountDto accountMsg) {
Map<String, Object> params = new HashMap<>();
params.put("account", accountNo);
params.put("type", type);
params.put("fans", accountMsg.getFansCnt());
params.put("accountName", accountMsg.getAccountName());
HttpConfig config = HttpConfig.custom()
.encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
.url(getBaseUrl() + PathConstant.REPORT_FANS)
.map(params);
String res = RequestUtil.put(config);
if (!StringUtils.hasText(res)) {
return false;
}
JSONObject resObj = JSONObject.parseObject(res);
Boolean result = resObj.getBoolean("success");
if (Boolean.FALSE.equals(result)) {
Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
String message = Optional.ofNullable(resObj.getString("message")).orElse("账号粉丝数上报失败");
log.error(LocalDate.now() + "上报账户 " + accountNo + (Objects.equals(type, 1) ? " 快手" : " 抖音") + "粉丝数据异常", message);
return false;
}
return true;
}
/**
* 视频数据上报
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @param videoList 视频数据集合
* @return
*/
public boolean reportVideo(String accountNo, Integer type, List<VideoPool> videoList) {
VideoPool videoPool;
if (videoList.size() > 0) {
videoPool = videoList.get(0);
} else {
return true;
}
if (Objects.isNull(videoPool.getVideoId()) || Objects.isNull(videoPool.getVideoUrl())) {
log.info(LocalDate.now() + (Objects.equals(type, 1) ? " '快手'" : " '抖音'") + " 账户号为: " + accountNo + "的账号, 暂无视频数据");
return true;
}
VideoPoolDto dto = new VideoPoolDto();
dto.setAccount(accountNo);
dto.setType(type);
dto.setVideoList(videoList.stream().map(item -> VideoPoolDto.VideoDTO
.builder()
.videoId(item.getVideoId())
.title(item.getTitle())
.preview(item.getPreview())
.uv(item.getPlayCount())
.fullUv(item.getFullPlayCount())
.likeNum(item.getLikeCount())
.shareNum(item.getShareCount())
.commentNum(item.getCommentCount())
.newFanNum(item.getNewFansUserCnt())
.duration(item.getDuration())
.publishTime(item.getPublishTime())
.playUrl(item.getVideoUrl())
.build()
).collect(Collectors.toList()));
HttpConfig config = HttpConfig.custom()
.encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
.url(getBaseUrl() + PathConstant.REPORT_VIDEO)
.json(JSON.toJSONString(dto));
String res = RequestUtil.post(config);
if (!StringUtils.hasText(res)) {
return false;
}
JSONObject resObj = JSONObject.parseObject(res);
Boolean result = resObj.getBoolean("success");
if (Boolean.FALSE.equals(result)) {
Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
String message = Optional.ofNullable(resObj.getString("message")).orElse("账号视频数据上报失败");
log.error(LocalDate.now() + " 上报账户 " + accountNo + (Objects.equals(type, 1) ? " 快手" : " 抖音") + "视频数据异常", message);
return false;
}
return true;
}
/**
* 直播数据上报
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @param liveList 直播数据集合
* @return
*/
public boolean reportLive(String accountNo, Integer type, List<LivePool> liveList) {
LivePool livePool;
if (liveList.size() > 0) {
livePool = liveList.get(0);
} else {
return true;
}
if (Objects.isNull(livePool.getOpenTime()) || Objects.isNull(livePool.getRoomId())) {
log.info(LocalDate.now() + (Objects.equals(type, 1) ? " '快手'" : " '抖音'") + " 账户号为: " + accountNo + "的账号, 暂无直播数据");
return true;
}
LivePoolDto dto = new LivePoolDto();
dto.setAccount(accountNo);
dto.setType(type);
dto.setLiveList(liveList.stream().map(item -> LivePoolDto.LiveDTO
.builder()
.roomNo(item.getRoomId())
.userNick(item.getUserNick())
.title(item.getRoomName())
.cover(item.getRoomCoverImage())
.playbackUrl(item.getPlaybackUrl())
.uv(item.getWatchUserCnt())
.uvPeak(item.getWatchPeakUserCnt())
.likeNum(item.getLikeCnt())
.shareNum(item.getShareCnt())
.commentNum(item.getCommentUserCnt())
.newFanNum(item.getNewFansUserCnt())
.receiveNum(item.getScore())
.receiveAmount(item.getIncome())
.liveDuration(item.getDuration())
.liveStartTime(item.getOpenTime())
.liveEndTime(item.getLiveEndTime())
.build()
).collect(Collectors.toList()));
HttpConfig config = HttpConfig.custom()
.encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
.url(getBaseUrl() + PathConstant.REPORT_LIVE)
.json(JSON.toJSONString(dto));
String res = RequestUtil.post(config);
if (!StringUtils.hasText(res)) {
return false;
}
JSONObject resObj = JSONObject.parseObject(res);
Boolean result = resObj.getBoolean("success");
if (Boolean.FALSE.equals(result)) {
Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
String message = Optional.ofNullable(resObj.getString("message")).orElse("账号直播数据上报失败");
log.error(LocalDate.now() + " 上报账户 " + accountNo + (Objects.equals(type, 1) ? " 快手" : " 抖音") + "直播数据异常", message);
return false;
}
return true;
}
}