package com.zhiqiyun.open.core.spiders;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.PlainText;

import java.util.logging.Level;

@Slf4j
@Component("phantomjs.downloader")
public class PhantomjsDownloader implements Downloader {

    @Value("${spider.phantomjs_executable_path_property}")
    private String phantomjsPath;

    @Override
    public Page download(Request request, Task task) {
        try {
            log.info(request.getUrl());
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjsPath);
            caps.setJavascriptEnabled(true); // enabled by default
            caps.setCapability("phantomjs.page.settings.loadImages", false);
            caps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36");
            PhantomJSDriver driver = new PhantomJSDriver(caps);

            driver.setLogLevel(Level.OFF);
            driver.get(request.getUrl());

            String rawText = driver.getPageSource();

            Page page = new Page();
            page.setBytes(rawText.getBytes());
            page.setRawText(rawText);
            page.setUrl(new PlainText(request.getUrl()));
            page.setRequest(request);
            page.setDownloadSuccess(true);
            return page;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public void setThread(int i) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>{}", i);
    }

}
