Search in sources :

Example 1 with PermissionException

use of com.hsn.epic4j.core.exception.PermissionException in project epic4j by huisunan.

the class MainStart method receive.

@Override
@SneakyThrows
@Retry(message = "领取失败")
public List<Item> receive(Page page, List<Item> weekFreeItems) {
    if (log.isDebugEnabled()) {
        log.debug("所有免费的游戏:{}", weekFreeItems.stream().map(Item::getTitle).collect(Collectors.joining(",")));
    }
    List<Item> receiveItem = new ArrayList<>();
    for (Item item : weekFreeItems) {
        String url = getItemUrl(item);
        String itemUrl = StrUtil.format(UrlConstants.storeUrl, url);
        log.info("游戏url:{}", itemUrl);
        page.goTo(itemUrl);
        log.info("18+检测");
        PageUtil.tryClick(page, itemUrl, 8, 1000, "div[data-component=PDPAgeGate] Button");
        PageUtil.waitForTextChange(page, "div[data-component=DesktopSticky] button[data-testid=purchase-cta-button]", LOADING_TEXT);
        if (isInLibrary(page)) {
            log.debug("游戏[{}]已经在库里", item.getTitle());
            continue;
        }
        page.waitForSelector("div[data-component=DesktopSticky] button[data-testid=purchase-cta-button]").click();
        // page.waitForSelector("div[data-component=WithClickTracking] button").click();
        // epic user licence check
        log.info("首次领取游戏检测||设备检测");
        PageUtil.tryClick(page, itemUrl, 30, 100, Arrays.asList(userLicenceCheck(), platformCheck()));
        String purchaseUrl = PageUtil.getStrProperty(page, "#webPurchaseContainer iframe", "src");
        log.debug("订单链接 :{}", purchaseUrl);
        page.goTo(purchaseUrl);
        PageUtil.tryClick(page, page.mainFrame().url(), 20, 500, "#purchase-app button[class*=confirm]:not([disabled])");
        PageUtil.tryClick(page, page.mainFrame().url(), "#purchaseAppContainer div.payment-overlay button.payment-btn--primary");
        PageUtil.findSelectors(page, 30_000, true, () -> {
            throw new TimeException("订单状态检测超时");
        }, new SelectItem("#purchase-app div[class*=alert]", () -> {
            if (item.isDLC()) {
                // DLC情况下,在没有本体的情况下也也可以领取
                return SelectItem.SelectCallBack.CONTINUE;
            } else {
                String message = PageUtil.getStrProperty(page, "#purchase-app div[class*=alert]:not([disabled])", "textContent");
                throw new PermissionException(message);
            }
        }), // talon_frame_checkout_free_prod
        new SelectItem("#talon_container_checkout_free_prod[style*=visible]", () -> {
            // 需要验证码
            throw new PermissionException("检测到需要验证码");
        }), new SelectItem("#purchase-app > div", (p, i) -> p.$(i.getSelectors()) == null, () -> {
            // 当订单完成刷新时,该元素不存在,是订单完成后刷新到新页面
            page.goTo(itemUrl);
            PageUtil.waitForTextChange(page, "div[data-component=DesktopSticky] button[data-testid=purchase-cta-button]", LOADING_TEXT);
            if (!isInLibrary(page)) {
                throw new ItemException("该游戏被误认为已经认领");
            }
            log.info("游戏领取成功:{}", item.getTitle());
            receiveItem.add(item);
            return SelectItem.SelectCallBack.END;
        }));
    }
    return receiveItem;
}
Also used : PermissionException(com.hsn.epic4j.core.exception.PermissionException) DateUtil(cn.hutool.core.date.DateUtil) java.util(java.util) PageNavigateOptions(com.ruiyun.jvppeteer.options.PageNavigateOptions) SneakyThrows(lombok.SneakyThrows) Constant(com.ruiyun.jvppeteer.core.Constant) FileUtil(com.ruiyun.jvppeteer.util.FileUtil) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Viewport(com.ruiyun.jvppeteer.options.Viewport) JSONUtil(cn.hutool.json.JSONUtil) Item(com.hsn.epic4j.core.bean.Item) HttpUtil(cn.hutool.http.HttpUtil) DateTime(cn.hutool.core.date.DateTime) StreamSupport(java.util.stream.StreamSupport) PageSlug(com.hsn.epic4j.core.bean.PageSlug) EpicConfig(com.hsn.epic4j.core.config.EpicConfig) PageUtil(com.hsn.epic4j.core.util.PageUtil) TimeException(com.hsn.epic4j.core.exception.TimeException) Browser(com.ruiyun.jvppeteer.core.browser.Browser) Puppeteer(com.ruiyun.jvppeteer.core.Puppeteer) SelectItem(com.hsn.epic4j.core.bean.SelectItem) JSONObject(cn.hutool.json.JSONObject) LaunchOptions(com.ruiyun.jvppeteer.options.LaunchOptions) LaunchOptionsBuilder(com.ruiyun.jvppeteer.options.LaunchOptionsBuilder) Collectors(java.util.stream.Collectors) ItemException(com.hsn.epic4j.core.exception.ItemException) Page(com.ruiyun.jvppeteer.core.page.Page) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) BrowserFetcher(com.ruiyun.jvppeteer.core.browser.BrowserFetcher) JSONArray(cn.hutool.json.JSONArray) PermissionException(com.hsn.epic4j.core.exception.PermissionException) Item(com.hsn.epic4j.core.bean.Item) SelectItem(com.hsn.epic4j.core.bean.SelectItem) ItemException(com.hsn.epic4j.core.exception.ItemException) TimeException(com.hsn.epic4j.core.exception.TimeException) SelectItem(com.hsn.epic4j.core.bean.SelectItem) SneakyThrows(lombok.SneakyThrows)

Example 2 with PermissionException

use of com.hsn.epic4j.core.exception.PermissionException in project epic4j by huisunan.

the class PasswordLogin method login.

@Override
@SneakyThrows
public void login(Page page, String email, String password) {
    if (StrUtil.isEmpty(email)) {
        throw new CheckException("账号不能为空");
    }
    if (StrUtil.isEmpty(password)) {
        throw new CheckException(email + " 密码不能为空");
    }
    log.debug("开始登录");
    String originUrl = page.mainFrame().url();
    PageUtil.click(page, "div.menu-icon");
    PageUtil.click(page, "div.mobile-buttons a[href='/login']");
    PageUtil.waitUrlChange(page, originUrl);
    PageUtil.click(page, "#login-with-epic");
    PageUtil.type(page, "#email", email);
    PageUtil.type(page, "#password", password);
    PageUtil.click(page, "#sign-in[tabindex='0']");
    PageUtil.findSelectors(page, 30000, true, () -> {
        throw new TimeException("登录超时");
    }, new SelectItem("#talon_frame_login_prod[style*=visible]", () -> {
        throw new PermissionException("未知情况下需要验证码");
    }), new SelectItem("div.MuiPaper-root[role=alert] h6[class*=subtitle1]", () -> {
        Object jsonValue = page.waitForSelector("div.MuiPaper-root[role=alert] h6[class*=subtitle1]").getProperty("textContent").jsonValue();
        throw new PermissionException("来自epic的错误消息: " + jsonValue);
    }), new SelectItem("input[name=code-input-0]", () -> {
        throw new PermissionException("需要校验码");
    }), new SelectItem(".signed-in", () -> {
        log.info("登录成功");
        return SelectItem.SelectCallBack.END;
    }));
    log.debug("登录结束");
}
Also used : PermissionException(com.hsn.epic4j.core.exception.PermissionException) TimeException(com.hsn.epic4j.core.exception.TimeException) CheckException(com.hsn.epic4j.core.exception.CheckException) SelectItem(com.hsn.epic4j.core.bean.SelectItem) SneakyThrows(lombok.SneakyThrows)

Aggregations

SelectItem (com.hsn.epic4j.core.bean.SelectItem)2 PermissionException (com.hsn.epic4j.core.exception.PermissionException)2 TimeException (com.hsn.epic4j.core.exception.TimeException)2 SneakyThrows (lombok.SneakyThrows)2 CollUtil (cn.hutool.core.collection.CollUtil)1 DateTime (cn.hutool.core.date.DateTime)1 DateUtil (cn.hutool.core.date.DateUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 HttpUtil (cn.hutool.http.HttpUtil)1 JSONArray (cn.hutool.json.JSONArray)1 JSONObject (cn.hutool.json.JSONObject)1 JSONUtil (cn.hutool.json.JSONUtil)1 Item (com.hsn.epic4j.core.bean.Item)1 PageSlug (com.hsn.epic4j.core.bean.PageSlug)1 EpicConfig (com.hsn.epic4j.core.config.EpicConfig)1 CheckException (com.hsn.epic4j.core.exception.CheckException)1 ItemException (com.hsn.epic4j.core.exception.ItemException)1 PageUtil (com.hsn.epic4j.core.util.PageUtil)1 Constant (com.ruiyun.jvppeteer.core.Constant)1 Puppeteer (com.ruiyun.jvppeteer.core.Puppeteer)1