use of com.ruiyun.jvppeteer.core.browser.Browser in project epic4j by huisunan.
the class CrawTest method test.
@Test
@SneakyThrows
public void test() {
BrowserFetcher.downloadIfNotExist("938248");
String dataPath = new FileUrlResource("./data/chrome/tests").getFile().getAbsolutePath();
LaunchOptions options = new LaunchOptionsBuilder().withArgs(Arrays.asList("--blink-settings=imagesEnabled=false", "--no-first-run", "--disable-gpu", "--no-default-browser-check", "--no-sandbox")).withHeadless(false).withUserDataDir(dataPath).withExecutablePath("").withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).build();
Browser browser = Puppeteer.launch(options);
Page page = browser.pages().get(0);
// PageUtil.crawSet(page);
// page.goTo("http://localhost:9999/");
page.goTo("http://www.baidu.com/");
TimeUnit.SECONDS.sleep(10);
FileUrlResource errorDir = new FileUrlResource("data/test");
File file = errorDir.getFile();
log.debug("mkdir {}", file.mkdirs());
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
screenshotOptions.setQuality(100);
screenshotOptions.setPath(file.getAbsolutePath() + File.separator + "report.jpg");
screenshotOptions.setType("jpeg");
page.screenshot(screenshotOptions);
FileUtil.writeUtf8String(page.content(), new File(file.getAbsolutePath() + File.separator + "report.html"));
log.info("success");
}
use of com.ruiyun.jvppeteer.core.browser.Browser 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();
}
}
}
use of com.ruiyun.jvppeteer.core.browser.Browser in project epic4j by huisunan.
the class MainStart method getWeekFreeItems.
/**
* 获取周免游戏
*/
@Retry(message = "获取周末游戏失败")
@Override
public List<Item> getWeekFreeItems(Page page) {
Browser browser = page.browser();
String userCountry = "CN";
String locate = "zh-CN";
String country = PageUtil.getJsonValue(browser, epicConfig.getUserInfoUrl(), "userInfo.country.value", String.class);
if (StrUtil.isNotEmpty(country)) {
userCountry = country;
locate = locate + "-" + userCountry;
}
String formatUrl = StrUtil.format(epicConfig.getFreeGameUrl(), locate, userCountry, userCountry);
log.debug(formatUrl);
JSONObject json = PageUtil.getJson(formatUrl, browser);
List<Item> list = new ArrayList<>();
DateTime now = DateUtil.date();
for (JSONObject element : json.getByPath("data.Catalog.searchStore.elements", JSONArray.class).jsonIter()) {
if (!"ACTIVE".equals(element.getStr("status"))) {
continue;
}
if (StreamSupport.stream(element.getJSONArray("categories").jsonIter().spliterator(), false).anyMatch(item -> "freegames".equals(item.getStr("path")))) {
JSONObject promotions = element.getJSONObject("promotions");
if (promotions == null) {
continue;
}
JSONArray promotionalOffers = promotions.getJSONArray("promotionalOffers");
if (CollUtil.isNotEmpty(promotionalOffers)) {
if (StreamSupport.stream(promotionalOffers.jsonIter().spliterator(), false).flatMap(offerItem -> StreamSupport.stream(offerItem.getJSONArray("promotionalOffers").jsonIter().spliterator(), false)).anyMatch(offerItem -> {
DateTime startDate = DateUtil.parse(offerItem.getStr("startDate")).setTimeZone(TimeZone.getDefault());
DateTime endDate = DateUtil.parse(offerItem.getStr("endDate")).setTimeZone(TimeZone.getDefault());
JSONObject discountSetting = offerItem.getJSONObject("discountSetting");
return DateUtil.isIn(now, startDate, endDate) && "PERCENTAGE".equals(discountSetting.getStr("discountType")) && discountSetting.getInt("discountPercentage") == 0;
})) {
list.add(element.toBean(Item.class));
}
}
}
}
return list;
}
Aggregations