Przeglądaj źródła

Reformat to use waitDriver

Axel Nordh 3 lat temu
rodzic
commit
4613ef458e
2 zmienionych plików z 74 dodań i 90 usunięć
  1. 69 90
      Odds/src/parser/OddsPortal.java
  2. 5 0
      Odds/src/parser/ParserBase.java

+ 69 - 90
Odds/src/parser/OddsPortal.java

@@ -2,6 +2,7 @@ package parser;
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.time.Duration;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -12,11 +13,11 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.openqa.selenium.By;
-import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
-import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
 import org.openqa.selenium.remote.RemoteWebDriver;
+import org.openqa.selenium.support.ui.ExpectedConditions;
+import org.openqa.selenium.support.ui.WebDriverWait;
 
 import com.google.common.base.Strings;
 
@@ -39,7 +40,7 @@ public class OddsPortal extends ParserBase implements ParserJoinedFunctions {
     private List<ResultDTO> resultsToInsert = new ArrayList<>();
     private String currentSeason;
 
-//    Behöver göras om till selenium
+//    Beh�ver g�ras om till selenium
 //    public void getHistoricMatches(String sport, String country, String league, String year) {
 //        final String url = "https://www.oddsportal.com/";
 //        final String resultsPage = "/results";
@@ -209,125 +210,103 @@ public class OddsPortal extends ParserBase implements ParserJoinedFunctions {
         // Other
         options.addArguments("disable-infobars");
 
-        WebDriver driver = new RemoteWebDriver(new URL("http://nordh.xyz:4444/wd/hub"), options);
+        RemoteWebDriver driver = new RemoteWebDriver(new URL("http://nordh.xyz:4444/wd/hub"), options);
         sportId = Mysql.getInstance().getSportId("soccer");
         driver.get(soccerUrl);
 
-        try {
-            Thread.sleep(1500);
+        WebDriverWait wait = getWaitDriver(driver, 90);
 
-            if (!checkIfElementExists(driver, "//p[text()='EU Odds']")) {
-                WebElement oddsFormat = driver
-                        .findElement(By.xpath("//p[@class='self-center text-xs text-orange-main'][1]"));
+        wait.until(ExpectedConditions
+                .numberOfElementsToBeMoreThan(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV), 1));
 
-                oddsFormat.click();
+        if (!checkIfElementExists(driver, "//p[text()='EU Odds']")) {
+            WebElement oddsFormat = driver
+                    .findElement(By.xpath("//p[@class='self-center text-xs text-orange-main'][1]"));
 
-                driver.findElement(By.xpath("//a[text()='EU Odds']")).click();
-                Thread.sleep(2000);
-            }
+            oddsFormat.click();
 
-            List<WebElement> divs = driver
-                    .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
+            driver.findElement(By.xpath("//a[text()='EU Odds']")).click();
+            wait.withTimeout(Duration.ofSeconds(30));
+        }
 
-            for (int i = 0; i < divs.size(); i++) {
-                WebElement element = divs.get(i);
-                scrollElementIntoView(driver, element);
-                ResultDTO result = new ResultDTO(SOCCER_RESULTS_TABLE_NAME);
+        List<WebElement> divs = driver
+                .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
 
-                boolean somethingWrong = false;
-                List<WebElement> subDivs = element.findElements(By.xpath("./div"));
-                if (subDivs.size() == 3) {
-                    WebElement competitionsDiv = subDivs.get(0);
-                    List<WebElement> links = competitionsDiv.findElements(By.xpath(".//a"));
+        for (int i = 0; i < divs.size(); i++) {
+            WebElement element = divs.get(i);
+            scrollElementIntoView(driver, element);
+            ResultDTO result = new ResultDTO(SOCCER_RESULTS_TABLE_NAME);
 
-                    countryId = getCountryId(links);
-                    leagueId = getLeagueId(links);
-                    result.setLeagueId(leagueId);
-                    result.setCountryId(countryId);
+            boolean somethingWrong = false;
+            List<WebElement> subDivs = element.findElements(By.xpath("./div"));
+            if (subDivs.size() == 3) {
+                WebElement competitionsDiv = subDivs.get(0);
+                List<WebElement> links = competitionsDiv.findElements(By.xpath(".//a"));
 
-                    WebElement firstResultsDiv = subDivs.get(2);
+                countryId = getCountryId(links);
+                leagueId = getLeagueId(links);
+                result.setLeagueId(leagueId);
+                result.setCountryId(countryId);
 
-                    setGameDate(result, firstResultsDiv);
+                WebElement firstResultsDiv = subDivs.get(2);
 
-                    setTeamsInfo(result, firstResultsDiv);
+                setGameDate(result, firstResultsDiv);
 
-                    setScoring(result, firstResultsDiv);
+                setTeamsInfo(result, firstResultsDiv);
 
-                    setOdds(result, firstResultsDiv);
+                setScoring(result, firstResultsDiv);
 
-                } else if (subDivs.size() == 1) {
+                setOdds(result, firstResultsDiv);
 
-                    result.setLeagueId(leagueId);
-                    result.setCountryId(countryId);
-                    WebElement firstResultsDiv = subDivs.get(0);
+            } else if (subDivs.size() == 1) {
 
-                    setGameDate(result, firstResultsDiv);
-                    setTeamsInfo(result, firstResultsDiv);
+                result.setLeagueId(leagueId);
+                result.setCountryId(countryId);
+                WebElement firstResultsDiv = subDivs.get(0);
 
-                    setScoring(result, firstResultsDiv);
+                setGameDate(result, firstResultsDiv);
+                setTeamsInfo(result, firstResultsDiv);
 
-                    setOdds(result, firstResultsDiv);
-                } else {
-                    somethingWrong = true;
-                    String message = "Subdiv size = " + subDivs.size();
-                    Logger.getGlobal().log(Level.WARNING, message);
-                }
+                setScoring(result, firstResultsDiv);
 
-                if (!somethingWrong) {
-                    final Mysql mysql = Mysql.getInstance();
+                setOdds(result, firstResultsDiv);
+            } else {
+                somethingWrong = true;
+                String message = "Subdiv size = " + subDivs.size();
+                Logger.getGlobal().log(Level.WARNING, message);
+            }
 
-                    result.setCountryId(countryId);
-                    result.setLeagueId(leagueId);
-                    result.setSeason(getLastSeason(leagueId, countryId));
+            if (!somethingWrong) {
+                final Mysql mysql = Mysql.getInstance();
 
-                    resultsToInsert.add(result);
+                result.setCountryId(countryId);
+                result.setLeagueId(leagueId);
+                result.setSeason(getLastSeason(leagueId, countryId));
 
-                    if (resultsToInsert.size() > 100) {
-                        Logger.getGlobal().log(Level.INFO, "INSERTING 100 results");
-                        mysql.addResults(resultsToInsert);
-                        resultsToInsert.clear();
-                    }
+                resultsToInsert.add(result);
 
+                if (resultsToInsert.size() > 100) {
+                    Logger.getGlobal().log(Level.INFO, "INSERTING 100 results");
+                    mysql.addResults(resultsToInsert);
+                    resultsToInsert.clear();
                 }
-                divs = driver
-                        .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
+
             }
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-            Thread.currentThread().interrupt();
+            divs = driver
+                    .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
         }
 
-        String message = "INSERTING LAST " + resultsToInsert.size() + " results";
+        String message = "INSERTING LAST " + resultsToInsert.size()
+                + " results";
         Logger.getGlobal().log(Level.INFO, message);
         Mysql.getInstance().addResults(resultsToInsert);
         resultsToInsert.clear();
-//        driver.close();
+        driver.close();
         driver.quit();
     }
 
-    private List<WebElement> scrollAndGetAllMatchDivs(ChromeDriver driver, List<WebElement> divs, int divsCount)
-            throws InterruptedException {
-        if (divsCount > 0) {
-            scrollElementIntoViewCenter(driver, divs.get(divsCount - 1));
-            Thread.sleep(2000);
-            divs = driver
-                    .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
-        }
-
-        while (divsCount < divs.size()) {
-            if (divsCount > 0) {
-                divsCount = divs.size();
-                scrollElementIntoViewCenter(driver, divs.get(divsCount - 1));
-                Thread.sleep(2000);
-                divs = driver
-                        .findElements(By.xpath(DIV_CONTAINS_CLASS_TABS_DIV_NOT_CLASS_DIV_1_DIV));
-            }
-        }
-        return divs;
-    }
-
     /*
-     * Behöver göras om till Selenium
+     * Beh�ver g�ras om till Selenium
      * private void parseTournamentTable(int sportId, int countryId, int leagueId,
      * String season,
      * HtmlTable tournamentTable, LocalDateTime gameDate,
@@ -455,9 +434,9 @@ public class OddsPortal extends ParserBase implements ParserJoinedFunctions {
         List<WebElement> oddsDivs = firstResultsDiv.findElements(By.xpath("./div/div"));
 
         try {
-            result.setOdds1(Float.parseFloat(oddsDivs.get(0).getText().toString()));
-            result.setOddsX(Float.parseFloat(oddsDivs.get(1).getText().toString()));
-            result.setOdds2(Float.parseFloat(oddsDivs.get(2).getText().toString()));
+            result.setOdds1(Float.parseFloat(oddsDivs.get(0).getText()));
+            result.setOddsX(Float.parseFloat(oddsDivs.get(1).getText()));
+            result.setOdds2(Float.parseFloat(oddsDivs.get(2).getText()));
         } catch (NumberFormatException e) {
             System.out.println(
                     "failed to parse odds " + oddsDivs.get(0).getText() + ", " + oddsDivs.get(1).getText() + ", "
@@ -475,7 +454,7 @@ public class OddsPortal extends ParserBase implements ParserJoinedFunctions {
                 result.setHomeScore(Integer.parseInt(scoringDivs.get(0).getText()));
                 result.setAwayScore(Integer.parseInt(scoringDivs.get(2).getText()));
             } catch (NumberFormatException e) {
-
+                // There is no scoring yet continue processing
             }
         }
 

+ 5 - 0
Odds/src/parser/ParserBase.java

@@ -9,6 +9,7 @@ import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
+import org.openqa.selenium.remote.RemoteWebDriver;
 import org.openqa.selenium.support.ui.WebDriverWait;
 
 public class ParserBase {
@@ -69,6 +70,10 @@ public class ParserBase {
         return new WebDriverWait(driver, Duration.ofSeconds(30));
     }
 
+    protected WebDriverWait getWaitDriver(RemoteWebDriver driver, int duration) {
+        return new WebDriverWait(driver, Duration.ofSeconds(duration));
+    }
+
     protected void scrollElementIntoView(WebDriver driver, WebElement element) {
         ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
     }