CrawlBizService.java
9.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
package cn.fw.freya.service;
import cn.fw.freya.enums.AccountTypeEnum;
import cn.fw.freya.model.data.Account;
import cn.fw.freya.model.data.pool.LivePool;
import cn.fw.freya.model.data.pool.VideoPool;
import cn.fw.freya.model.dto.rpc.ReportAccountDto;
import cn.fw.freya.service.crawl.CrawlStrategy;
import cn.fw.freya.service.data.AccountService;
import cn.fw.freya.service.rpc.AccountRpcService;
import cn.fw.freya.service.rpc.ReportRpcService;
import cn.fw.freya.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Collectors;
/**
* @author kurisu
* @date 2021-11-15 14:35
* @description bizService
*/
@Slf4j
@Service
public class CrawlBizService {
private final Map<AccountTypeEnum, CrawlStrategy> crawlStrategyMap;
private final AccountService accountService;
private final AccountRpcService accountRpcService;
private final ReportRpcService reportRpcService;
@Autowired
public CrawlBizService(final List<CrawlStrategy> crawlStrategyList,
final AccountService accountService,
final AccountRpcService accountRpcService,
final ReportRpcService reportRpcService) {
this.crawlStrategyMap = crawlStrategyList.stream()
.collect(Collectors.toMap(CrawlStrategy::getType, v -> v));
this.accountService = accountService;
this.accountRpcService = accountRpcService;
this.reportRpcService = reportRpcService;
}
/**
* 登陆准备
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @return
*/
public String preLogin(String accountNo, Integer type) {
AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
AssertUtil.notNull(typeEnum, () -> "平台类型不正确");
CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
return crawlStrategy.preLogin(accountNo);
}
/**
* 登陆
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @return
*/
public boolean doLogin(String accountNo, Integer type) {
AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
AssertUtil.notNull(typeEnum, () -> "平台类型不正确");
CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
return crawlStrategy.doLogin(accountNo);
}
/**
* 退出浏览器
*
* @param accountNo 账户号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @return
*/
public boolean exitBrowser(String accountNo, Integer type) {
AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
AssertUtil.notNull(typeEnum, () -> "平台类型不正确");
CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
return crawlStrategy.exitBrowser(accountNo);
}
/**
* 执行抓取数据
*
* @param account
*/
public void crawlData(Account account) throws IOException {
Integer type = account.getType();
final String accountNo = account.getAccountNo();
AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
// 抓取数据
log.info("线程: " + Thread.currentThread().getName() + " 开始抓取数据");
final ReportAccountDto accountMsg = crawlStrategy.updateAccountMsg(accountNo);// 更新粉丝数
if (Objects.isNull(accountMsg)) {
doPushExpireAccount(account);
return;
}
final List<VideoPool> allVideoMsg = crawlStrategy.getAllVideoMsg(accountNo);// 获取所有视频信息
if (Objects.isNull(allVideoMsg)) {
doPushExpireAccount(account);
return;
}
final List<LivePool> yesterdayLiveMsg = crawlStrategy.getYesterdayLiveMsg(accountNo);// 获取昨日直播信息
if (Objects.isNull(yesterdayLiveMsg)) {
doPushExpireAccount(account);
return;
}
// 上报数据
final boolean reportAccountMsg = this.doReportAccountMsg(account, accountMsg);
final String format = String.format("上报[%s]平台, 账户号为: %s", AccountTypeEnum.getNameByValue(account.getType()), accountNo);
if (!reportAccountMsg) {
log.error(format + "的账户信息失败");
return;
}
final boolean reportVideo = this.doReportVideo(account, allVideoMsg);
if (!reportVideo) {
log.error(format + "的视频数据失败");
return;
}
final boolean reportLive = this.doReportLive(account, yesterdayLiveMsg);
if (!reportLive) {
log.error(format + "的直播数据失败");
return;
}
this.afterCrawl(accountService.findById(account.getId()));
}
/**
* 后续处理, 设置账户今日完成数据抓取
*
* @param account
*/
private void afterCrawl(Account account) {
account.setDone(Boolean.TRUE);
accountService.saveOrUpdate(account);
}
/**
* 上报失效账户
*
* @param account 账户
*/
private void doPushExpireAccount(Account account) {
boolean result = false;
int hasTryTimes = 0;
int maxTryTimes = 2;
while (!result && hasTryTimes < maxTryTimes) {
result = accountRpcService.pushExpireAccount(account.getAccountNo(), account.getType());
hasTryTimes++;
}
}
/**
* 上报账户信息
*
* @param account 账户
* @param accountMsg 账户其他信息
* @return
*/
private boolean doReportAccountMsg(Account account, ReportAccountDto accountMsg) {
boolean reportFansCnt;
int hasTryTimes = 0;
int maxTryTimes = 2;
while (hasTryTimes < maxTryTimes) {
reportFansCnt = reportRpcService.reportAccountMsg(account.getAccountNo(), account.getType(), accountMsg);
if (reportFansCnt) {
return true;
} else {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3));
}
hasTryTimes++;
}
return false;
}
/**
* 上报所有视频
*
* @param account 账户
* @param allVideoMsg 所有视频
* @return
*/
private boolean doReportVideo(Account account, List<VideoPool> allVideoMsg) {
boolean reportVideo;
int hasTryTimes = 0;
int maxTryTimes = 2;
while (hasTryTimes < maxTryTimes) {
reportVideo = reportRpcService.reportVideo(account.getAccountNo(), account.getType(), allVideoMsg);
if (reportVideo) {
return true;
} else {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3));
}
hasTryTimes++;
}
return false;
}
/**
* 上报昨日所有直播
*
* @param account 账户
* @param yesterdayLiveMsg 昨日所有直播数据
* @return
*/
private boolean doReportLive(Account account, List<LivePool> yesterdayLiveMsg) {
boolean reportLive;
int hasTryTimes = 0;
int maxTryTimes = 2;
while (hasTryTimes < maxTryTimes) {
reportLive = reportRpcService.reportLive(account.getAccountNo(), account.getType(), yesterdayLiveMsg);
if (reportLive) {
return true;
} else {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3));
}
hasTryTimes++;
}
return false;
}
/**
* 监听器, 收到消息后执行上报账户直播信息
*
* @param account 账户
* @throws IOException
*/
@Async(value = "wmyThreadPool")
@EventListener(Account.class)
public void reportLive(Account account) throws IOException {
log.info(Thread.currentThread().getName() + " spring监听器在CrawlBizService.reportLive()方法上成功收到消息: " + account);
Integer type = account.getType();
final String accountNo = account.getAccountNo();
AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
final List<LivePool> yesterdayLiveMsg = crawlStrategy.getYesterdayLiveMsg(accountNo);// 获取昨日直播信息
final boolean reportLive = this.doReportLive(account, yesterdayLiveMsg);
if (!reportLive) {
accountService.setAccountUndone(accountNo);
log.error(LocalDate.now() + " 上报账户为" + accountNo + "的" + AccountTypeEnum.getNameByValue(account.getType()) + "的直播数据失败");
}
}
}