Rev 37446 | 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 com.smartdukaan.cron.monitored.ImeiActivationGauges;import org.springframework.beans.factory.annotation.Autowired;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.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;@Componentpublic class CheckRealmeWarrantyTask {@Autowiredprivate ImeiActivationGauges gauges;private static final String BRAND = "Realme";private static final Logger LOGGER = LogManager.getLogger(CheckRealmeWarrantyTask.class);static {try {OpenCV.loadShared();} catch (Throwable t) {LogManager.getLogger(CheckRealmeWarrantyTask.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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\"");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");// Headless needs no GPU, yet chrome still forks a gpu-process per browser (6 were// alive across the fleet). --disable-gpu removes them; --disable-dev-shm-usage// keeps chrome off the small /dev/shm on this box. Both are pure overhead removal,// no behaviour change. This box co-hosts a 9.4GB tomcat with ~3GB available and// has been OOM-killed twice this month, and tomcat was the victim both times.options.addArguments("--disable-gpu");options.addArguments("--disable-dev-shm-usage");options.addArguments("--disable-software-rasterizer");options.addArguments("--disable-blink-features=AutomationControlled","--disable-infobars","--disable-popup-blocking","--disable-notifications","--disable-extensions");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 cookiesdriver.manage().deleteAllCookies();// Specifiying pageLoadTimeout and Implicit waitdriver.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, 15);WebDriverWait wait5Sec = new WebDriverWait(driver, 4);WebElement slideButton;Actions actionProvider = new Actions(driver);do {driver.get("https://www.realme.com/in/support/phonecheck");installResponseCapture(driver);/*try {driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();} catch (Exception e) {try {LOGGER.info("Cookie not found");} catch (Exception e1) {}}*/for (int i = 0; i < remainingImeis.size(); i++) {String imei = remainingImeis.get(i);try {LOGGER.info("Starting fresh with new IMEI " + imei);// realme rebuilt this page: the field is no longer .sn-input and the// submit is no longer div.check-btn but an icon inside the same box.if (driver.findElement(By.cssSelector(IMEI_INPUT)).getAttribute("value").length() > 0) {//System.out.println("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));driver.get("https://www.realme.com/in/support/phonecheck");installResponseCapture(driver);break;}driver.findElement(By.cssSelector(IMEI_INPUT)).sendKeys(imei);//driver.findElement(By.className("el-button--primary")).click();driver.findElement(By.cssSelector(SUBMIT_ICON)).click();// Perform click-and-hold action on the elementint counter = 1;boolean captchaNotBroken = true;// release() is only reached on the moveByOffset > 0 path, so an attempt// that fails detection or calibration leaves the button DOWN -- measured// at 77% of realme presses in production. Actions is one instance per// driver, created outside the imei loop, so the stuck press leaks into the// next attempt and the next imei: the following clickAndHold lands on an// already-held button and the widget never repositions the handle.boolean pressed = false;do {String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";try {LOGGER.info("Starting Do");//Thread.sleep(5000);wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("canvas")));wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]")));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"));gauges.captchaShown(BRAND);actionProvider.moveToElement(slideButton).clickAndHold().moveByOffset(1, 0)//.pause(2000).perform();pressed = true;//actionProvider.moveByOffset(-1,0).pause(2000).perform();LOGGER.info("Margin Left after - " + slideButton.getCssValue("margin-left"));//Thread.sleep(5000);} catch (Exception e) {LOGGER.info(e.getMessage());driver.get("https://www.realme.com/in/support/phonecheck");installResponseCapture(driver);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, measure again, close the remainder, THEN release.actionProvider.moveByOffset(moveByOffset, 0).perform();refine(driver, actionProvider, cal[1]);actionProvider.release().perform();pressed = false;LOGGER.info("Move click performed (main {}px)", moveByOffset);try {// Success used to mean "the dx_captcha_basic_bar-inform error element// did not appear". realme rebuilt this page, so that element may no// longer exist -- in which case the wait always times out and EVERY// attempt reports success while the captcha was never solved. That is// exactly what happened: 27 successes, 27 missed responses, 0 rows.//// The lookup response arriving is the only signal that actually proves// the captcha passed, so wait for that instead.String json = awaitResponse(driver, 8000);if (json == null) {LOGGER.warn("Failed = {} (captcha said: {}) urls={}",counter, captchaSignal(driver), seenUrls(driver));} else {LOGGER.info("Success at attempt " + counter);gauges.captchaSolved(BRAND);//List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));//WebElement activationTimeElement = driver.findElement(By.xpath("//*[contains(text(), 'UTC+5.5') or contains(text(), 'Non-Activate')]"));// Read the JSON the page already received. The old locator// keyed off a label that no longer exists, and its "| //h1" branch// would silently grab a page heading and try to parse it as a date.// The API is explicit: expiryDate (yyyy.MM.dd) plus an isActivation// flag, so nothing has to be inferred from message text.LocalDate activationDate = activationFrom(json);LOGGER.info("Activation lookup for {} -> {}", imei,activationDate != null ? activationDate : "not activated");// Always record, even with a null date. findElement used to THROW// when the element was missing, so nothing was written and the imei// came back on every run indefinitely.dateMap.put(imei, activationDate);gauges.answered(BRAND, activationDate != null);clearCapture(driver);if (dateMap.size() == imeis.size()) {return dateMap;}WebElement checkAgainButton = driver.findElement(By.className("recheck-btn"));captchaNotBroken = false;checkAgainButton.click();break;}} catch (Throwable e) {LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());}}}// Any path that got here without releasing (null circles, unusable// calibration) must let go before the next attempt re-presses.if (pressed) {try {actionProvider.release().perform();} catch (Exception releaseFailed) {LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());}pressed = false;}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 <= MAX_CAPTCHA_ATTEMPTS);// The widget-not-found path breaks out mid-iteration; do not carry a held// button into the next imei.if (pressed) {try {actionProvider.release().perform();} catch (Exception releaseFailed) {LOGGER.warn("Could not release the slider: " + releaseFailed.getMessage());}}LOGGER.warn("Gave up after {} captcha attempts - imei stays pending for the next tick", MAX_CAPTCHA_ATTEMPTS);} catch (Exception e) {LOGGER.info("Caught unknown - for imei " + imei);LOGGER.error("Realme warranty check failed for imei " + imei, e);}driver.get("https://www.realme.com/in/support/phonecheck");installResponseCapture(driver);}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(); // Always shuts everything down} catch (Exception e) {LOGGER.warn("Error closing driver: " + e.getMessage());}}}return dateMap;}/** realme rebuilt the page; the old .sn-input / div.check-btn no longer render. */private static final String IMEI_INPUT = ".imei-input input";private static final String SUBMIT_ICON = ".imei-input .search-icon";/*** Calibration nudge, in slider px, sized from the gap rather than fixed.** A fixed 15px was far too small: it closed only ~25px, so the ratio came from a* measurement ~20% noise and the aim landed ~30px out. A fixed 60px then proved too* large in the other direction -- production data showed 85% of attempts with a gap* under 60px ended up UNUSABLE (the piece shot past the hole and the measured gap* grew), and 45% overall.** Aim to close about half the gap: the piece travels roughly ASSUMED_RATIO px per* slider px, so nudge ~= gap / (2 * ratio), clamped. The real ratio is still derived* from the measurement; this only sizes the probe.*//** REALME's tuning values, deliberately independent of oppo's -- do not sync the two.* Realme currently converts ~60% of completed drags against oppo's ~12% on the same* widget, so it is the one that must not be disturbed to chase oppo.** Left at 1.7 on purpose. Realme's measured ratio over 1,683 production calibrations* is 0.70, so this is wrong here too and the probe is smaller than intended -- but it* is wrong while converting 60%, and oppo is carrying the corrected value first. If* oppo improves, bring 0.70 over here and measure it on its own.*/private static final double ASSUMED_RATIO = 1.7;private static final int MIN_NUDGE = 10;private static final int MAX_NUDGE = 60;/*** Captcha attempts per imei per tick, before giving up and letting the next tick* retry with a FRESH page and session. The imei is never abandoned -- it stays* pending and returns in ~5 minutes.** 12, NOT oppo's 7. Realme has not had the glide change, so its successes still* spread late. Measured over 399 successes and 610 exhausted imeis:* cap 7 -> only 83.5% of successes kept (16.5% lost), 59.2% of work saved* cap 12 -> 96.0% kept, 35.7% saved <- here* cap 14 -> 97.7% kept, 26.7% saved* Realme burns more than twice oppo's captcha work, so even this gentler cap saves* more in absolute terms (4,937 attempts vs oppo's 3,648).** If glide is ever ported here, expect the curve to shift left as oppo's did* (33% -> 2% of successes from attempts 8-20) and this can come down to ~7.*/private static final int MAX_CAPTCHA_ATTEMPTS = 12;private static final double ALIGNED_PX = 2.0;private static int nudgeFor(double gap) {int n = (int) Math.round(gap / (2 * ASSUMED_RATIO));return Math.max(MIN_NUDGE, Math.min(MAX_NUDGE, n));}/** Stash the active/check response on window as the page receives it. */private void installResponseCapture(WebDriver driver) {try {((JavascriptExecutor) driver).executeScript("if (!window.__rmCapInstalled) {"+ " window.__rmCapInstalled = true; window.__rmResp = null; window.__rmCaptcha = null; window.__rmSeen = [];"// Three transports, because realme's page uses all of them and only the// prototype-level patches are reliable.//// The lookup goes through Nuxt's $fetch (ofetch), which grabs its// reference to globalThis.fetch when its bundle is evaluated -- long// before driver.get() returns and we can wrap window.fetch. So wrapping// window.fetch NEVER sees /active/check: verified on the live page, the// same $fetch call is invisible to a window.fetch wrapper and visible to// a Response.prototype one. That is why 526 solved captchas produced 0// rows: the answer arrived and we could not see it.//// Response.prototype.json / .text are resolved at call time, so patching// them catches any client no matter when it took its fetch reference.+ " var O = XMLHttpRequest.prototype.open;"+ " XMLHttpRequest.prototype.open = function(m,u){"+ " this.addEventListener('load', function(){"+ " try { var s=String(u);"+ " if (s.indexOf('active/check') !== -1 || s.indexOf('customer-api') !== -1) window.__rmResp = this.responseText;"+ " else if (s.indexOf('captcha') !== -1) window.__rmCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200);"+ " else if (window.__rmSeen.length < 12) window.__rmSeen.push(s.slice(0,80));"+ " } catch(e){}"+ " });"+ " return O.apply(this, arguments);"+ " };"+ " var F = window.fetch;"+ " if (F) { window.fetch = function(){"+ " var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"+ " return F.apply(this, arguments).then(function(r){"+ " try { if (u.indexOf('active/check') !== -1 || u.indexOf('customer-api') !== -1) { r.clone().text().then(function(t){ window.__rmResp = t; }); }"+ " else if (window.__rmSeen.length < 12) window.__rmSeen.push('F ' + u.slice(0,80)); } catch(e){}"+ " return r;"+ " });"+ " }; }"// Read the body off the Response itself. Must use the ORIGINAL reader on// the clone -- calling the patched one recurses forever (it hangs the page).+ " ['json','text'].forEach(function(name){"+ " var orig = Response.prototype[name];"+ " if (!orig) return;"+ " Response.prototype[name] = function(){"+ " try { var u = this.url || '';"+ " if ((u.indexOf('active/check') !== -1 || u.indexOf('customer-api') !== -1) && !this.__rmSeenBody) {"+ " var c = this.clone(); c.__rmSeenBody = 1;"+ " orig.call(c).then(function(t){ window.__rmResp = (typeof t === 'string') ? t : JSON.stringify(t); });"+ " } else if (u && window.__rmSeen.length < 12 && u.indexOf('captcha') === -1) {"+ " window.__rmSeen.push('R ' + u.slice(0,80));"+ " } } catch(e){}"+ " return orig.apply(this, arguments);"+ " };"+ " });"+ "}");} catch (Exception e) {LOGGER.warn("Could not install the response capture hook: " + e.getMessage());}}/*** Wait for the lookup response to arrive, which is the only reliable proof that* the captcha was actually solved. Polls rather than blocking so a solved captcha* is picked up as soon as the request completes.*/private String awaitResponse(WebDriver driver, long timeoutMs) {long deadline = System.currentTimeMillis() + timeoutMs;while (System.currentTimeMillis() < deadline) {String json = capturedResponse(driver);if (json != null && !json.trim().isEmpty()) return json;try {Thread.sleep(300);} catch (InterruptedException ie) {Thread.currentThread().interrupt();return null;}}return null;}/** Last response from the captcha widget itself -- says WHY a drag was refused. */private String captchaSignal(WebDriver driver) {try {Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmCaptcha || null;");return v == null ? null : v.toString();} catch (Exception e) {return null;}}/** Non-captcha URLs the page called -- shows what to match on when the lookup is missed. */private String seenUrls(WebDriver driver) {try {Object v = ((JavascriptExecutor) driver).executeScript("return (window.__rmSeen||[]).join(' | ');");return v == null ? "" : v.toString();} catch (Exception e) {return "";}}private String capturedResponse(WebDriver driver) {try {Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmResp || null;");return v == null ? null : v.toString();} catch (Exception e) {return null;}}private void clearCapture(WebDriver driver) {try {((JavascriptExecutor) driver).executeScript("window.__rmResp = null;");} catch (Exception ignored) {}}/*** realme returns the WARRANTY EXPIRY, not the activation date, so a year is* subtracted -- warranty is 12 months as standard. Verified against our own* records: 41,267 of 41,289 stored Realme activation dates precede the date we* checked them, averaging 13 days before, which is only consistent with the* subtraction being applied. isActivation false means the device is not* activated yet and there is no date to derive.*/private LocalDate activationFrom(String json) {if (json == null) return null;try {JSONObject root = new JSONObject(json);JSONObject data = root.optJSONObject("data");if (data == null) return null;if (!data.optBoolean("isActivation", false)) return null;String expiry = data.optString("expiryDate", "");if (expiry == null || expiry.trim().isEmpty() || "null".equals(expiry)) return null;return LocalDate.parse(expiry.trim().split(" ")[0],DateTimeFormatter.ofPattern("yyyy.MM.dd")).minusYears(1);} catch (Exception e) {LOGGER.warn("Could not read expiryDate from the realme response: " + e.getMessage());return null;}}/** @return {sliderPxToMove, sliderPxPerCirclePx}, or null if the circles were unreadable. */private double[] calibrate(WebDriver driver, Actions actionProvider, double distance) throws Exception {int nudge = nudgeFor(distance);actionProvider.moveByOffset(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, nudge, closed);if (closed <= 0) return null;double sliderPerCircle = nudge / closed;return new double[]{Math.round(movedDistance * sliderPerCircle), sliderPerCircle};}/** Measure again after the main move and close the remainder before releasing. */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 pageWebElement imageElement = driver.findElement(By.id("dx_captcha_basic_bg_1"));Point point = imageElement.getLocation();// Get width and height of the elementint eleWidth = imageElement.getSize().getWidth();int eleHeight = imageElement.getSize().getHeight();// Crop the entire page screenshot to get only element screenshotBufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),eleWidth, eleHeight);ImageIO.write(eleScreenshot, "png", screenshot);// Copy the element screenshot to diskFile 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 pageWebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));Point point = imageElement.getLocation();// Get width and height of the elementint eleWidth = imageElement.getSize().getWidth();int eleHeight = imageElement.getSize().getHeight();// Crop the entire page screenshot to get only element screenshotBufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),eleWidth, eleHeight);ImageIO.write(eleScreenshot, "png", screenshot);// Copy the element screenshot to diskFile screenshotLocation = new File(fileName);FileUtils.copyFile(screenshot, screenshotLocation);Mat src = Imgcodecs.imread(fileName);if (src.empty()) {LOGGER.warn("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 centers100, 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 radiusnew File(fileName).delete();return new double[] {c1[0], c2[0] // x-coordinates};}}}new File(fileName).delete();return null;}}