use of cn.hutool.core.collection.CollUtil in project epic4j by huisunan.
the class BaseRunner method doStart.
public void doStart(String email, String password) {
Browser browser = null;
try {
log.info("start {} work", email);
// 用户文件路径
String userDataPath = new FileUrlResource(epicConfig.getDataPath() + File.separator + email).getFile().getAbsolutePath();
// 获取浏览器对象
browser = iStart.getBrowser(userDataPath);
// 获取默认page
Page page = iStart.getDefaultPage(browser);
// 加载cookie
if (StrUtil.isNotBlank(epicConfig.getCookiePath()) && FileUtil.exist(epicConfig.getCookiePath())) {
log.debug("load cookie");
List<CookieParam> cookies = JSONUtil.toBean(IoUtil.read(new FileReader(epicConfig.getCookiePath())), new TypeReference<List<CookieParam>>() {
}, false);
page.setCookie(cookies);
}
// 反爬虫设置
PageUtil.crawSet(page);
// 打开epic主页
page.goTo(epicConfig.getEpicUrl());
boolean needLogin = iStart.needLogin(browser);
log.debug("needLogin:{}", needLogin);
if (needLogin) {
iLogin.login(page, email, password);
}
List<Item> weekFreeItems = iStart.getWeekFreeItems(page);
// 领取游戏
List<Item> receive = iStart.receive(page, weekFreeItems);
for (INotify notify : notifies) {
if (notify.notifyReceive(receive)) {
break;
}
}
} catch (Exception e) {
if (epicConfig.getErrorScreenShoot()) {
Optional.ofNullable(browser).map(Browser::pages).filter(CollUtil::isNotEmpty).map(pages -> pages.get(0)).ifPresent(page -> {
try {
FileUrlResource errorDir = new FileUrlResource("data/error");
log.debug("create error dir {}", errorDir.getFile().mkdirs());
ScreenshotOptions options = new ScreenshotOptions();
options.setQuality(100);
options.setPath(errorDir.getFile().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg");
options.setType("jpeg");
page.screenshot(options);
} catch (IOException ioException) {
log.error("截图失败");
}
});
}
log.error("程序异常", e);
} finally {
if (browser != null) {
browser.close();
}
}
}
Aggregations