Search in sources :

Example 1 with SelectItem

use of com.hsn.epic4j.bean.SelectItem 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("all free items:{}", weekFreeItems.stream().map(Item::getTitle).collect(Collectors.joining(",")));
    }
    List<Item> receiveItem = new ArrayList<>();
    for (Item item : weekFreeItems) {
        String url = "";
        if (Item.BASE_GAME.equals(item.getOfferType())) {
            url = item.getProductSlug();
        } else if (Item.DLC.equals(item.getOfferType())) {
            url = item.getUrlSlug();
        }
        String itemUrl = StrUtil.format(epicConfig.getStoreUrl(), url);
        log.debug("item url:{}", itemUrl);
        page.goTo(itemUrl);
        // age limit
        PageUtil.tryClick(page, "div[data-component=PDPAgeGate] Button", itemUrl, 8, 1000);
        PageUtil.waitForTextChange(page, "div[data-component=DesktopSticky] button[data-testid=purchase-cta-button]", "Loading");
        if (isInLibrary(page)) {
            log.debug("{} had in library", item.getTitle());
            continue;
        }
        page.waitForSelector("div[data-component=WithClickTracking] button").click();
        // epic user licence check
        log.debug("user licence check");
        PageUtil.tryClick(page, "div[data-component=makePlatformUnsupportedWarningStep] button[data-component=BaseButton", itemUrl);
        PageUtil.tryClick(page, "#agree", itemUrl);
        PageUtil.tryClick(page, "div[data-component=EulaModalActions] button[data-component=BaseButton]", itemUrl);
        String purchaseUrl = PageUtil.getStrProperty(page, "#webPurchaseContainer iframe", "src");
        log.debug("purchase url :{}", purchaseUrl);
        page.goTo(purchaseUrl);
        PageUtil.tryClick(page, "#purchase-app button[class*=confirm]:not([disabled])", page.mainFrame().url(), 20, 500);
        PageUtil.tryClick(page, "#purchaseAppContainer div.payment-overlay button.payment-btn--primary", page.mainFrame().url());
        PageUtil.findSelectors(page, 10_000, true, () -> {
            throw new TimeException("time out");
        }, 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);
            }
        }), new SelectItem("#talon_frame_checkout_free_prod[style*=visible]", () -> {
            // 需要验证码
            throw new PermissionException("CAPTCHA is required for unknown reasons when claiming");
        }), 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");
            if (!isInLibrary(page)) {
                throw new ItemException("An item was mistakenly considered to have been claimed");
            }
            receiveItem.add(item);
            return SelectItem.SelectCallBack.END;
        }));
    }
    if (receiveItem.isEmpty()) {
        log.info("all free week games in your library:{}", weekFreeItems.stream().map(Item::getTitle).collect(Collectors.joining(",")));
    }
    return receiveItem;
}
Also used : PermissionException(com.hsn.epic4j.exception.PermissionException) DateUtil(cn.hutool.core.date.DateUtil) Item(com.hsn.epic4j.bean.Item) java.util(java.util) SneakyThrows(lombok.SneakyThrows) Constant(com.ruiyun.jvppeteer.core.Constant) FileUtil(com.ruiyun.jvppeteer.util.FileUtil) Viewport(com.ruiyun.jvppeteer.options.Viewport) Autowired(org.springframework.beans.factory.annotation.Autowired) DateTime(cn.hutool.core.date.DateTime) StreamSupport(java.util.stream.StreamSupport) TimeException(com.hsn.epic4j.exception.TimeException) Browser(com.ruiyun.jvppeteer.core.browser.Browser) EpicConfig(com.hsn.epic4j.config.EpicConfig) Puppeteer(com.ruiyun.jvppeteer.core.Puppeteer) JSONObject(cn.hutool.json.JSONObject) LaunchOptions(com.ruiyun.jvppeteer.options.LaunchOptions) Retry(com.hsn.epic4j.aop.Retry) SelectItem(com.hsn.epic4j.bean.SelectItem) PermissionException(com.hsn.epic4j.exception.PermissionException) LaunchOptionsBuilder(com.ruiyun.jvppeteer.options.LaunchOptionsBuilder) Collectors(java.util.stream.Collectors) Page(com.ruiyun.jvppeteer.core.page.Page) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) BrowserFetcher(com.ruiyun.jvppeteer.core.browser.BrowserFetcher) JSONArray(cn.hutool.json.JSONArray) PageUtil(com.hsn.epic4j.util.PageUtil) ItemException(com.hsn.epic4j.exception.ItemException) Item(com.hsn.epic4j.bean.Item) SelectItem(com.hsn.epic4j.bean.SelectItem) ItemException(com.hsn.epic4j.exception.ItemException) TimeException(com.hsn.epic4j.exception.TimeException) SelectItem(com.hsn.epic4j.bean.SelectItem) SneakyThrows(lombok.SneakyThrows) Retry(com.hsn.epic4j.aop.Retry)

Example 2 with SelectItem

use of com.hsn.epic4j.bean.SelectItem 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("login start");
    PageUtil.click(page, "div.menu-icon");
    PageUtil.click(page, "div.mobile-buttons a[href='/login']");
    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("Check login result timeout.");
    }, new SelectItem("#talon_frame_login_prod[style*=visible]", () -> {
        throw new PermissionException("CAPTCHA is required for unknown reasons when logging in");
    }), 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("From Epic Games: " + jsonValue);
    }), new SelectItem("input[name=code-input-0]", () -> {
        throw new PermissionException("From Epic Games need code");
    }), new SelectItem(".signed-in", () -> {
        log.info("login success");
        return SelectItem.SelectCallBack.END;
    }));
    log.debug("login end");
}
Also used : PermissionException(com.hsn.epic4j.exception.PermissionException) TimeException(com.hsn.epic4j.exception.TimeException) CheckException(com.hsn.epic4j.exception.CheckException) SelectItem(com.hsn.epic4j.bean.SelectItem) SneakyThrows(lombok.SneakyThrows)

Example 3 with SelectItem

use of com.hsn.epic4j.bean.SelectItem in project epic4j by huisunan.

the class PageUtil method findSelectors.

/**
 * 在指定范围时间内,遍历查找
 *
 * @param page        page
 * @param timeout     超时时间
 * @param ignore      true忽略异常
 * @param timeoutBack 超时后回调
 * @param selectItems 要查询的元素
 */
@SneakyThrows
public void findSelectors(Page page, Integer timeout, Boolean ignore, SelectItem.SelectCallBack timeoutBack, SelectItem... selectItems) {
    WaitForSelectorOptions options = new WaitForSelectorOptions();
    options.setTimeout(timeout);
    // 100ms
    int interval = 100;
    for (int i = 0; i < timeout; i += interval) {
        for (SelectItem selectItem : selectItems) {
            boolean flag = false;
            try {
                flag = selectItem.getPagePredicate().test(page, selectItem);
            } catch (Exception e) {
                if (ignore)
                    log.debug("ignore exception", e);
                else
                    throw e;
            }
            if (flag) {
                boolean isContinue = selectItem.getCallback().run();
                if (!isContinue) {
                    return;
                }
            }
        }
        TimeUnit.MILLISECONDS.sleep(interval);
    }
    if (timeoutBack != null) {
        timeoutBack.run();
    }
}
Also used : SelectItem(com.hsn.epic4j.bean.SelectItem) WaitForSelectorOptions(com.ruiyun.jvppeteer.options.WaitForSelectorOptions) TimeException(com.hsn.epic4j.exception.TimeException) SneakyThrows(lombok.SneakyThrows)

Aggregations

SelectItem (com.hsn.epic4j.bean.SelectItem)3 TimeException (com.hsn.epic4j.exception.TimeException)3 SneakyThrows (lombok.SneakyThrows)3 PermissionException (com.hsn.epic4j.exception.PermissionException)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 JSONArray (cn.hutool.json.JSONArray)1 JSONObject (cn.hutool.json.JSONObject)1 Retry (com.hsn.epic4j.aop.Retry)1 Item (com.hsn.epic4j.bean.Item)1 EpicConfig (com.hsn.epic4j.config.EpicConfig)1 CheckException (com.hsn.epic4j.exception.CheckException)1 ItemException (com.hsn.epic4j.exception.ItemException)1 PageUtil (com.hsn.epic4j.util.PageUtil)1 Constant (com.ruiyun.jvppeteer.core.Constant)1 Puppeteer (com.ruiyun.jvppeteer.core.Puppeteer)1 Browser (com.ruiyun.jvppeteer.core.browser.Browser)1 BrowserFetcher (com.ruiyun.jvppeteer.core.browser.BrowserFetcher)1