OtherController.java
6.38 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
package cn.fw.freya.controller;
import cn.fw.freya.common.ResponseMessage;
import cn.fw.freya.model.data.Account;
import cn.fw.freya.model.data.pool.LivePool;
import cn.fw.freya.model.dto.other.ReportLiveDto;
import cn.fw.freya.model.dto.other.ReportVideoDto;
import cn.fw.freya.model.dto.rpc.ReportAccountDto;
import cn.fw.freya.service.CommonBizService;
import cn.fw.freya.service.crawl.impl.Common;
import cn.fw.freya.service.rpc.AccountRpcService;
import cn.fw.freya.service.rpc.ReportRpcService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author kurisu
*/
@Slf4j
@Validated
@RestController
@RequestMapping("/other")
@RequiredArgsConstructor
public class OtherController {
private final Common common;
private final CommonBizService commonBizService;
private final AccountRpcService accountRpcService;
private final ReportRpcService reportRpcService;
private final ApplicationEventPublisher publisher;
/**
* 清理 driverMap
*/
@GetMapping("/clearMap")
public ResponseMessage<Boolean> clearMap() {
return ResponseMessage.success(commonBizService.clearMap());
}
/**
* 初始化项目
*/
@GetMapping("/init")
public ResponseMessage<Boolean> init() {
return ResponseMessage.success(commonBizService.init());
}
/**
* 从服务器同步账号到H2数据库
*
* @return
*/
@GetMapping("/asyncAccount")
public ResponseMessage<Boolean> asyncAccount() {
return ResponseMessage.success(commonBizService.doAsyncAccount());
}
/**
* 上报失效账号
*
* @param account 账号
* @param type 账号类型
* @return
*/
@PostMapping("/pushExpireAccount")
public ResponseMessage<Boolean> pushExpireAccount(@NotBlank(message = "账号不能为空") String account, @NotNull(message = "粉丝数不能为空") @NotNull(message = "账户类型不能为空") Integer type) {
return ResponseMessage.success(accountRpcService.pushExpireAccount(account, type));
}
/**
* 上报账户信息
*
* @param account 账号
* @param type 账号类型
* @param accountMsg 账户信息
* @return
*/
@PostMapping("/reportAccountMsg")
public ResponseMessage<Boolean> reportAccountMsg(@NotBlank(message = "账号不能为空") String account, @NotNull(message = "账户类型不能为空") Integer type, @NotNull(message = "账户信息不能为空") ReportAccountDto accountMsg) {
return ResponseMessage.success(reportRpcService.reportAccountMsg(account, type, accountMsg));
}
/**
* 上报账户所有视频数
*
* @param dto 所有视频
* @return
*/
@PostMapping("/reportVideo")
public ResponseMessage<Boolean> reportVideo(@RequestBody @Valid ReportVideoDto dto) {
return ResponseMessage.success(reportRpcService.reportVideo(dto.getAccount(), dto.getType(), dto.getVideoPoolList()));
}
/**
* 上报账户所有直播数
*
* @param dto 所有直播
* @return
*/
@PostMapping("/reportLive")
public ResponseMessage<Boolean> reportLive(@RequestBody @Validated ReportLiveDto dto) {
return ResponseMessage.success(reportRpcService.reportLive(dto.getAccount(), dto.getType(), dto.getLivePoolList()));
}
/**
* 测试spring事件发布
*/
@GetMapping("/publishEvent")
public void publishEvent() {
for (int i = 0; i < 32; i++) {
publisher.publishEvent(LocalDate.now() + " -> 发布事件 thread->" + i);// 事件发布成功后会等待监听程序执行完所有逻辑后
System.out.println("事件发布成功");
}
}
/**
* 处理JSON数据中时间戳对象
*/
@PostMapping("/formatTimestamp")
public ResponseMessage<List<JSONObject>> formatJsonTimestamp(@RequestBody @Validated JsonFormatParam param) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return ResponseMessage.success(JSON.parseArray(param.getJsonString())
.stream()
.map(item -> {
JSONObject obj = (JSONObject) item;
obj.put("duration", BigDecimal.valueOf(obj.getLong("duration")).divide(BigDecimal.valueOf(60), 2, RoundingMode.HALF_UP));
param.getFields().forEach(item1 -> obj.put(item1, sdf.format(new Date(obj.getLong(item1)))));
return obj;
}).collect(Collectors.toList())
);
}
@Data
static class JsonFormatParam {
@NotBlank(message = "JSON字符串不能为空")
String jsonString;
@NotEmpty(message = "必须指定哪些字段需要被格式化")
Set<String> fields;
}
/**
* 获取随机账户
*
* @param type 账户类型
* @return
*/
@GetMapping("/getRandomUser")
public Account getRandomUser(@NotNull(message = "账户类型不能为空") Integer type) {
return common.getRandomUserByType(type);
}
/**
* 获取直播无回放信息的直播
*
* @param type 账户类型
* @param durationThreshold 时长阈值
* @return
*/
@GetMapping("/getWithoutPlaybackLive")
public List<LivePool> getWithoutPlaybackLive(@NotNull(message = "账户类型不能为空") Integer type, @NotNull(message = "时长阈值不能为空") Double durationThreshold) {
return common.getWithoutPlaybackLive(type, durationThreshold);
}
/**
* 获取未完成抓取的账号分布
*
* @return 未完成抓取的账号分布
*/
@GetMapping("/getUndoneAccount")
public Map<String, List<String>> getUndoneAccount() {
return common.getUndoneAccount();
}
}