Commit 31609a68e28c9fccd6dbefdaaf390ee8e9862c47
1 parent
573ef45b
2023年9月12日14:49:11 修复特殊类型快手账号无法登录问题
Showing
2 changed files
with
61 additions
and
12 deletions
src/main/java/cn/fw/freya/service/crawl/impl/KuaiShouCrawl.java
... | ... | @@ -5,6 +5,7 @@ import cn.fw.freya.dao.LivePoolDao; |
5 | 5 | import cn.fw.freya.dao.VideoPoolDao; |
6 | 6 | import cn.fw.freya.domain.data.Account; |
7 | 7 | import cn.fw.freya.domain.data.FwCookie; |
8 | +import cn.fw.freya.domain.data.ResponseReceived; | |
8 | 9 | import cn.fw.freya.domain.data.pool.LivePool; |
9 | 10 | import cn.fw.freya.domain.data.pool.VideoPool; |
10 | 11 | import cn.fw.freya.domain.dto.rpc.ReportAccountDto; |
... | ... | @@ -28,8 +29,10 @@ import com.alibaba.fastjson.TypeReference; |
28 | 29 | import com.alibaba.fastjson.parser.Feature; |
29 | 30 | import lombok.RequiredArgsConstructor; |
30 | 31 | import lombok.extern.slf4j.Slf4j; |
32 | +import org.apache.http.HttpResponse; | |
31 | 33 | import org.apache.http.client.CookieStore; |
32 | 34 | import org.apache.http.impl.client.BasicCookieStore; |
35 | +import org.apache.http.util.EntityUtils; | |
33 | 36 | import org.apache.logging.log4j.util.PropertiesUtil; |
34 | 37 | import org.openqa.selenium.By; |
35 | 38 | import org.openqa.selenium.Cookie; |
... | ... | @@ -174,23 +177,35 @@ public class KuaiShouCrawl implements CrawlStrategy, SmartLifecycle { |
174 | 177 | if (Objects.isNull(driver)) { |
175 | 178 | throw new BusinessException("登陆校验失败,请重新尝试"); |
176 | 179 | } |
177 | - WebElement element; | |
180 | + WebElement element = null; | |
181 | + JSONObject ksAccountMsg = new JSONObject(); | |
182 | + boolean specialAccount = false;// 某些账号是认证过的账号, 首页右上角无账号信息元素 | |
178 | 183 | try { |
179 | 184 | element = new WebDriverWait(driver, 10, 300).until(driver1 -> |
180 | 185 | driver1.findElement(By.xpath("//div[contains(text(),'快手号:') or contains(text(),'用户 ID:')]"))); |
181 | 186 | } catch (Exception e) { |
182 | - this.exitBrowser(accountNo, null); | |
183 | - throw new BusinessException("网络异常,或该人员暂未扫码登录"); | |
187 | + ksAccountMsg = this.getKSAccountMsg(driver);// 尝试读取http日志获取账号信息 | |
188 | + if (!StringUtils.hasText(ksAccountMsg.getString("userId")) && | |
189 | + !StringUtils.hasText(ksAccountMsg.getString("accountNo"))) { | |
190 | + this.exitBrowser(accountNo, null); | |
191 | + throw new BusinessException("网络异常,或该人员暂未扫码登录"); | |
192 | + } else { | |
193 | + specialAccount = true; | |
194 | + } | |
184 | 195 | } |
185 | - if (Objects.nonNull(element)) { | |
196 | + if (Objects.nonNull(element) || specialAccount) { | |
186 | 197 | int subLength = 0; |
187 | - String accountText = element.getText(); | |
188 | - if (accountText.startsWith("用户")) { | |
189 | - subLength = 6; | |
190 | - } else if (accountText.startsWith("快手号")) { | |
191 | - subLength = 4; | |
198 | + String accountText = ""; | |
199 | + if (!specialAccount) { | |
200 | + accountText = element.getText(); | |
201 | + if (accountText.startsWith("用户")) | |
202 | + subLength = 6; | |
203 | + else if (accountText.startsWith("快手号")) | |
204 | + subLength = 4; | |
192 | 205 | } |
193 | - if (accountNo.equals(accountText.substring(subLength))) { | |
206 | + if (Objects.equals(accountNo, accountText.substring(subLength)) || | |
207 | + Objects.equals(accountNo, ksAccountMsg.getString("userId")) || | |
208 | + Objects.equals(accountNo, ksAccountMsg.getString("accountNo"))) { | |
194 | 209 | Integer type = this.getType().getValue(); |
195 | 210 | driver.get("https://cp.kuaishou.com/statistics/works");// 跳转到'视频数据'页面 |
196 | 211 | WebElement lockFlag = null;// 未解锁标志 |
... | ... | @@ -201,7 +216,7 @@ public class KuaiShouCrawl implements CrawlStrategy, SmartLifecycle { |
201 | 216 | try { |
202 | 217 | lockFlag = new WebDriverWait(driver, 3, 300).until(driver1 -> |
203 | 218 | driver1.findElement(By.xpath("//span[text()='立即开通']")));// '立即开通'元素 |
204 | - } catch (Exception ex){ | |
219 | + } catch (Exception ex) { | |
205 | 220 | this.exitBrowser(accountNo, null); |
206 | 221 | throw new BusinessException("如果已解锁数据授权, 请明日再来尝试登录"); |
207 | 222 | } |
... | ... | @@ -230,6 +245,40 @@ public class KuaiShouCrawl implements CrawlStrategy, SmartLifecycle { |
230 | 245 | } |
231 | 246 | |
232 | 247 | /** |
248 | + * 读取http日志, 获取账号信息 | |
249 | + * | |
250 | + * @param driver 驱动 | |
251 | + * @return 账号信息 | |
252 | + */ | |
253 | + private JSONObject getKSAccountMsg(WebDriver driver) { | |
254 | + JSONObject obj = new JSONObject(); | |
255 | + String dataUrl = "https://cp.kuaishou.com/rest/v2/creator/pc/authority/account/current"; | |
256 | + List<ResponseReceived> responseReceivedEvents = common.processHttpTransferData(driver); | |
257 | + responseReceivedEvents.forEach(item -> { | |
258 | + if (Objects.isNull(common.getDataUrl(item, dataUrl))) | |
259 | + return; | |
260 | + JSONObject data; | |
261 | + try { | |
262 | + HttpResponse response = common.getHttpResponse(driver, item, dataUrl); | |
263 | + if (Objects.nonNull(response)) { | |
264 | + data = JSON.parseObject(EntityUtils.toString(response.getEntity())) | |
265 | + .getJSONObject("value") | |
266 | + .getJSONObject("body") | |
267 | + .getJSONObject("data") | |
268 | + ; | |
269 | + if (StringUtils.hasText(data.getString("userId"))) | |
270 | + obj.put("userId", data.getString("userId")); | |
271 | + if (StringUtils.hasText(data.getString("userKwaiId"))) | |
272 | + obj.put("accountNo", data.getString("userKwaiId")); | |
273 | + } | |
274 | + } catch (Exception e) { | |
275 | + log.error("获取快手账号信息发生异常, 异常信息: {}", e.getMessage()); | |
276 | + } | |
277 | + }); | |
278 | + return obj; | |
279 | + } | |
280 | + | |
281 | + /** | |
233 | 282 | * 分页获取快手视频数据 |
234 | 283 | * |
235 | 284 | * @param accountNo 账号 | ... | ... |
src/main/java/cn/fw/freya/utils/CronCheckUtil.java
... | ... | @@ -40,7 +40,7 @@ public class CronCheckUtil { |
40 | 40 | * 测试cron表达式以后的执行时间 |
41 | 41 | */ |
42 | 42 | public static void main(String[] args) { |
43 | - String cronExpression = "0 0 0/1 * * ?";// 待校验的cron | |
43 | + String cronExpression = "0 0/5 * * * ?";// 待校验的cron | |
44 | 44 | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
45 | 45 | final List<LocalDateTime> runtimeList = CronCheckUtil.getRuntimeList(cronExpression, LocalDateTime.now(), 100); |
46 | 46 | runtimeList.forEach(item -> System.out.println(dateTimeFormatter.format(item))); | ... | ... |