Subversion Repositories SmartDukaan

Rev

Rev 37420 | Rev 37424 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled;

import nu.pattern.OpenCV;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.json.JSONObject;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.Instant;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class CheckOppoWarrantyTask {

    private static final Logger LOGGER = LogManager.getLogger(CheckOppoWarrantyTask.class);

    static {
        try {
            OpenCV.loadShared();
        } catch (Throwable t) {
            LogManager.getLogger(CheckOppoWarrantyTask.class).warn("OpenCV not available on this platform: {}", t.getMessage());
        }
    }

    public Map<String, LocalDate> checkWarranty(List<String> imeis) {
        Map<String, LocalDate> dateMap = new HashMap<>();
        LOGGER.info("Initiating webdriver...");

        Map<String, Object> prefsMap = new HashMap<>();
        prefsMap.put("profile.default_content_settings.popups", 0);
        prefsMap.put("download.prompt", false);
        prefsMap.put("download.directory_upgrade", true);
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
        //I need this
        //System.setProperty("webdriver.chrome.driver", "/Users/amit/cft/chromedriver/chromedriver");
        ChromeOptions options = new ChromeOptions();
        //options.setBinary("/Users/amit/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome For Testing");
        options.setExperimentalOption("prefs", prefsMap);
        //options.setExperimentalOption("detach", true);
        options.addArguments("--headless");
        options.addArguments("--user-agent=\"Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166\"");

        options.addArguments("--no-sandbox");
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions");
        options.addArguments("--force-device-scale-factor=1");
        options.addArguments("--test-type");

        WebDriver driver = null;

        try {
            driver = new ChromeDriver(options);
            driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
            driver.manage().window().setSize(new Dimension(1600, 900));
            driver.manage().window().maximize();
            // Deleting all the cookies
            driver.manage().deleteAllCookies();
            // Specifiying pageLoadTimeout and Implicit wait
            driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);

            int mainCount = 0;
            List<String> remainingImeis = imeis;
            WebDriverWait wait10Sec = new WebDriverWait(driver, 8);
            WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
            WebElement slideButton;
            Actions actionProvider = new Actions(driver);
            do {
                driver.get("https://support.oppo.com/in/warranty-check/");
                installResponseCapture(driver);
                try {
                    driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
                } catch (Exception e) {
                }

                for (int i = 0; i < remainingImeis.size(); i++) {
                    String imei = remainingImeis.get(i);
                    try {
                        LOGGER.info("Starting fresh with new IMEI " + imei);
                        if (driver.findElement(By.className("el-input__inner")).getAttribute("value").length() > 0) {
                            LOGGER.info("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
                            driver.get("https://support.oppo.com/in/warranty-check/");
                            break;
                        }
                        driver.findElement(By.className("el-input__inner")).sendKeys(imei);

                        //driver.findElement(By.className("el-button--primary")).click();
                        driver.findElement(By.cssSelector("div.searchBtn")).click();

                        // Perform click-and-hold action on the element
                        int counter = 1;
                        boolean captchaNotBroken = true;
                        do {
                            String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
                            try {
                                LOGGER.info("Starting Do");
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
                                slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
                                //System.out.println("Slide button - " + slideButton);
                                LOGGER.info("Margin Left before- " + slideButton.getCssValue("margin-left"));
                                actionProvider.moveToElement(slideButton).clickAndHold()
                                        .moveByOffset(1, 0)//.pause(2000)
                                        .perform();

                                //actionProvider.moveByOffset(-1,0).pause(2000).perform();
                                LOGGER.info("Margin Left after - " + slideButton.getCssValue("margin-left"));
                                //Thread.sleep(5000);
                            } catch (Exception e) {
                                LOGGER.warn("Slider captcha widget (dx_captcha_basic_*) not found or not draggable"
                                        + " - reloading. Cause: " + e.getMessage());
                                driver.get("https://support.oppo.com/in/warranty-check/");
                                break;
                            }
                            double[] circles = getMatCircles2(driver, fileName);
                            if (circles != null) {
                                double firstCircleX = circles[0];
                                double secondCircleX = circles[1];
                                double distance = Math.abs(firstCircleX - secondCircleX);
                                LOGGER.info("Distance is " + distance);

                                double[] cal = calibrate(driver, actionProvider, distance);
                                int moveByOffset = cal == null ? 0 : (int) cal[0];
                                if (moveByOffset > 0) {
                                    // Move, then measure again and close the remainder BEFORE
                                    // releasing. Releasing on the first computed offset committed
                                    // the whole calibration error to the final position.
                                    actionProvider.moveByOffset(moveByOffset, 0).perform();
                                    refine(driver, actionProvider, cal[1]);
                                    actionProvider.release().perform();
                                    LOGGER.info("Move click performed (main {}px)", moveByOffset);
                                    try {
                                        try {
                                            wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
                                            LOGGER.warn("Failed = " + counter);
                                        } catch (Exception notFailedException) {
                                            LOGGER.info("Success  at attempt " + counter);

                                            // Read the JSON the page itself received rather than the
                                            // rendered text. The DOM value is built from two text nodes -- the
                                            // date plus a "(UTC+x)" suffix derived from the browser timezone --
                                            // and the old locator tested only the first, so it matched nothing.
                                            // The API carries regDate as epoch millis, which has no format or
                                            // timezone ambiguity at all.
                                            String json = capturedResponse(driver);
                                            LocalDate activationDate = regDateFrom(json);

                                            if (json == null) {
                                                LOGGER.warn("Result shown but no getDeviceInfo response captured for imei {}", imei);
                                            } else {
                                                LOGGER.info("Activation lookup for {} -> {}", imei,
                                                        activationDate != null ? activationDate : "no activation date");
                                            }
                                            // Always record the imei, even with a null date: findElement used to
                                            // throw here, so the row was never written and came back every run.
                                            dateMap.put(imei, activationDate);
                                            clearCapture(driver);

                                            if (dateMap.size() == imeis.size()) {
                                                return dateMap;
                                            }
                                            WebElement checkAgainButton = driver.findElement(By.className("c-btn"));
                                            captchaNotBroken = false;
                                            checkAgainButton.click();
                                            break;
                                        }
                                    } catch (Throwable e) {
                                        LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());
                                    }
                                }
                            }
                            LOGGER.info("After circles conditions");
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
                            slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
                            actionProvider.moveToElement(slideButton).doubleClick().perform();
                            LOGGER.info("Ending Do" + counter++);

                        } while (captchaNotBroken && counter <= 20);
                        LOGGER.warn("Counter reached upto 20");
                    } catch (Exception e) {
                        LOGGER.error("Oppo warranty check failed for imei " + imei, e);
                    }
                    driver.get("https://support.oppo.com/in/warranty-check/");
                }
                mainCount++;
                remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
                if (remainingImeis.isEmpty()) break;
            } while (mainCount < 5);
        } finally {
            if (driver != null) {
                try {
                    driver.quit(); //Ensures cleanup even on return or error
                } catch (Exception e) {
                    LOGGER.warn("Error closing driver: " + e.getMessage());
                }
            }
        }

        return dateMap;
    }


    /** Stash the getDeviceInfo response on window as the page receives it. */
    private void installResponseCapture(WebDriver driver) {
        try {
            ((JavascriptExecutor) driver).executeScript(
                "if (!window.__oppoCapInstalled) {"
              + "  window.__oppoCapInstalled = true; window.__oppoResp = null;"
              + "  var O = XMLHttpRequest.prototype.open;"
              + "  XMLHttpRequest.prototype.open = function(m,u){"
              + "    this.addEventListener('load', function(){"
              + "      try { if (String(u).indexOf('getDeviceInfo') !== -1) window.__oppoResp = this.responseText; } catch(e){}"
              + "    });"
              + "    return O.apply(this, arguments);"
              + "  };"
              + "}");
        } catch (Exception e) {
            LOGGER.warn("Could not install the response capture hook: " + e.getMessage());
        }
    }

    private String capturedResponse(WebDriver driver) {
        try {
            Object v = ((JavascriptExecutor) driver).executeScript("return window.__oppoResp || null;");
            return v == null ? null : v.toString();
        } catch (Exception e) {
            return null;
        }
    }

    private void clearCapture(WebDriver driver) {
        try {
            ((JavascriptExecutor) driver).executeScript("window.__oppoResp = null;");
        } catch (Exception ignored) {
        }
    }

    /** data.regDate is epoch millis; absent or blank means the device is not activated yet. */
    private LocalDate regDateFrom(String json) {
        if (json == null) return null;
        try {
            JSONObject data = new JSONObject(json).optJSONObject("data");
            if (data == null) return null;
            String reg = data.optString("regDate", "");
            if (reg == null || reg.trim().isEmpty() || "null".equals(reg)) return null;
            return Instant.ofEpochMilli(Long.parseLong(reg.trim()))
                    .atZone(ZoneId.of("Asia/Kolkata")).toLocalDate();
        } catch (Exception e) {
            LOGGER.warn("Could not read regDate from the Oppo response: " + e.getMessage());
            return null;
        }
    }

    /**
     * Slider pixels to nudge when working out how far the puzzle piece travels per
     * slider pixel.
     *
     * This was 15, which was the reason the captcha only solved about 9% of the time.
     * The piece moves roughly 1.7 px per slider px, so a 15 px nudge closed the gap by
     * only ~25 px -- and Hough circle detection is worth about +/-2-3 px per circle, so
     * the ratio was being derived from a measurement with ~20% noise in it. That error
     * then multiplied across the whole remaining travel: on a 150 px gap it landed ~30 px
     * out, and the target hole is only ~30 px across, so it missed entirely.
     *
     * A 60 px nudge closes ~100 px, so the same detection noise is ~5% instead of ~20%.
     */
    private static final int CALIBRATION_NUDGE = 60;

    /** Stop correcting once the piece is within this many px of the hole. */
    private static final double ALIGNED_PX = 2.0;

    /**
     * @return {sliderPxToMove, sliderPxPerCirclePx}, or null when the circles could not
     *         be read. Returned rather than stored on a field: this is a singleton and
     *         the Oppo secondary and tertiary jobs run it concurrently.
     */
    private double[] calibrate(WebDriver driver, Actions actionProvider, double distance) throws Exception {
        actionProvider.moveByOffset(CALIBRATION_NUDGE, 0).perform();

        String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
        double[] movedCircles = getMatCircles2(driver, fileName2);
        if (movedCircles == null) return null;

        double movedDistance = Math.abs(movedCircles[0] - movedCircles[1]);
        double closed = distance - movedDistance;
        LOGGER.info("Calibration: gap {} -> {} after {}px, closed {}",
                distance, movedDistance, CALIBRATION_NUDGE, closed);

        // A non-positive value means the piece did not move, or moved away from the
        // hole -- either way the ratio is meaningless and guessing would waste the try.
        if (closed <= 0) return null;

        double sliderPerCircle = CALIBRATION_NUDGE / closed;
        int moveBy = (int) Math.round(movedDistance * sliderPerCircle);
        return new double[]{moveBy, sliderPerCircle};
    }

    /**
     * Measure again after the main move and close whatever is left. The old code
     * committed its single computed offset blind, so any calibration error went
     * straight into the final position with nothing to catch it.
     */
    private int refine(WebDriver driver, Actions actionProvider, double sliderPerCircle) throws Exception {
        String fileName = "/tmp/" + Thread.currentThread().getName() + "-refine.png";
        double[] circles = getMatCircles2(driver, fileName);
        if (circles == null) return 0;

        double remaining = Math.abs(circles[0] - circles[1]);
        if (remaining <= ALIGNED_PX) return 0;

        int correction = (int) Math.round(remaining * sliderPerCircle);
        if (correction == 0) return 0;
        LOGGER.info("Refine: {}px of gap left -> nudging {}px", remaining, correction);
        actionProvider.moveByOffset(correction, 0).perform();
        return correction;
    }

    private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {

        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        BufferedImage fullImg = ImageIO.read(screenshot);

        // Get the location of element on the page
        WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
        Point point = imageElement.getLocation();

        // Get width and height of the element
        int eleWidth = imageElement.getSize().getWidth();
        int eleHeight = imageElement.getSize().getHeight();

        // Crop the entire page screenshot to get only element screenshot
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
                eleWidth, eleHeight);
        ImageIO.write(eleScreenshot, "png", screenshot);

        // Copy the element screenshot to disk
        File screenshotLocation = new File(fileName);
        FileUtils.copyFile(screenshot, screenshotLocation);

        Imgcodecs imageCodecs = new Imgcodecs();
        Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
        Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
        Mat medianBlur = new Mat();
        Imgproc.medianBlur(grayMatrix, medianBlur, 5);
        Mat circles = new Mat();
        Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
        return circles;
    }

    private double[] getMatCircles2(WebDriver driver, String fileName) throws Exception {
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        BufferedImage fullImg = ImageIO.read(screenshot);

        // Get the location of element on the page
        WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
        Point point = imageElement.getLocation();

        // Get width and height of the element
        int eleWidth = imageElement.getSize().getWidth();
        int eleHeight = imageElement.getSize().getHeight();

        // Crop the entire page screenshot to get only element screenshot
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
                eleWidth, eleHeight);
        ImageIO.write(eleScreenshot, "png", screenshot);

        // Copy the element screenshot to disk
        File screenshotLocation = new File(fileName);
        FileUtils.copyFile(screenshot, screenshotLocation);

        Mat src = Imgcodecs.imread(fileName);
        if (src.empty()) {
            System.err.println("Cannot read image: " + fileName);
            new File(fileName).delete();
            return null;
        }

        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);

        Mat circles = new Mat();
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
                20, // min distance between centers
                100, 20, // param1 (Canny high threshold), param2 (accumulator threshold)
                10, 50); // minRadius, maxRadius (adjust as needed)

        if (circles.cols() < 2) {
            new File(fileName).delete();
            return null;
        }

        for (int i = 0; i < circles.cols(); i++) {
            double[] c1 = circles.get(0, i);
            for (int j = i + 1; j < circles.cols(); j++) {
                double[] c2 = circles.get(0, j);
                if (Math.abs(c1[2] - c2[2]) < 3) { // check similar radius
                    new File(fileName).delete();
                    return new double[] {
                            c1[0], c2[0] // x-coordinates
                    };
                }
            }
        }

        new File(fileName).delete();
        return null;
    }


}