| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package parser;
- import data.GuiMysql;
- import objects.SoccerMatch;
- import objects.SoccerMatchAnalysis;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.openqa.selenium.remote.RemoteWebDriver;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.*;
- public class Svenskaspel {
- String matchesXPATH = "//div[@class = 'pg_coupon_static__event_container']";
- String date;
- Map<String, String> nameReplace = Map.ofEntries(
- new AbstractMap.SimpleEntry<String, String>("manchester united", "Manchester Utd"),
- new AbstractMap.SimpleEntry<String, String>("queens park rangers", "QPR"),
- new AbstractMap.SimpleEntry<String, String>("west bromwich", "West Brom"),
- new AbstractMap.SimpleEntry<String, String>("blackburn rovers", "Blackburn"),
- new AbstractMap.SimpleEntry<String, String>("nottingham forest", "Nottingham"),
- new AbstractMap.SimpleEntry<String, String>("wolverhampton", "Wolves"));
- public List<SoccerMatchAnalysis> GetStryktipsetSelenium(String date) throws MalformedURLException {
- this.date = date;
- String url = "https://spela.svenskaspel.se/stryktipset/resultat/" + date + "/statistik";
- // RemoteWebDriver seleniumDriver = getSeleniumDriver();
- // seleniumDriver.get(url);
- // WebDriverManager.chromedriver().driverVersion("124.0.6367.60").setup();
- // ChromeDriver driver = new ChromeDriver(getChromeOptions());
- ChromeOptions options = new ChromeOptions();
- // Fixing 255 Error crashes
- // options.addArguments("--no-sandbox");
- options.addArguments("--disable-dev-shm-usage");
- // Options to trick bot detection
- // Removing webdriver property
- options.addArguments("--disable-blink-features=AutomationControlled");
- options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
- options.setExperimentalOption("useAutomationExtension", null);
- // Changing the user agent / browser fingerprint
- options.addArguments("window-size=1920,1080");
- options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
- "Gecko) " + "Chrome/74.0.3729.169 Safari/537.36");
- // Other
- options.addArguments("disable-infobars");
- System.out.println("Getting page");
- RemoteWebDriver driver = new RemoteWebDriver(new URL("http://nordh.xyz:14444/wd/hub"), options);
- driver.get(url);
- CloseCookiePopup(driver);
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- List<SoccerMatchAnalysis> matches = FindMatches(driver);
- driver.close();
- driver.quit();
- return matches;
- }
- private List<SoccerMatchAnalysis> FindMatches(WebDriver driver) {
- List<SoccerMatch> soccerMatches = new ArrayList<>();
- List<SoccerMatchAnalysis> matchAnalyses = new ArrayList<>();
- List<WebElement> matches = driver.findElements(By.xpath(matchesXPATH));
- for (WebElement match : matches) {
- List<String> teamNames = GetNames(match);
- String homeTeamName = teamNames.get(0);
- String awayTeamName = teamNames.get(1);
- System.out.println("Match: " + homeTeamName + " " + awayTeamName);
- if (nameReplace.containsKey(homeTeamName.toLowerCase())) {
- homeTeamName = nameReplace.get(homeTeamName.toLowerCase());
- }
- if (nameReplace.containsKey(awayTeamName.toLowerCase())) {
- awayTeamName = nameReplace.get(awayTeamName.toLowerCase());
- }
- soccerMatches.add(GuiMysql.getInstance().getMatch(date,
- homeTeamName.trim().replace(" ", "%") + "%",
- awayTeamName.trim().replace(" ", "%") + "%"));
- }
- for (SoccerMatch match : soccerMatches) {
- try {
- matchAnalyses.add(new SoccerMatchAnalysis(match));
- } catch (NullPointerException e) {
- System.out.println(
- "Failed to get team info between " + match.getHomeTeamName() + "-" + match.getAwayTeamName());
- }
- }
- return matchAnalyses;
- }
- private List<String> GetNames(WebElement match) {
- List<String> result = new ArrayList<>();
- WebElement hometeamWebElement = match.findElement(By.xpath("./a/div[2]/span[1]"));
- WebElement awayteamWebElement = match.findElement(By.xpath("./a/div[2]/span[3]"));
- if (hometeamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
- String teamName = hometeamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
- result.add(teamName);
- } else {
- result.add(hometeamWebElement.findElement(By.xpath("./span")).getText());
- }
- if (awayteamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
- String teamName = awayteamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
- result.add(teamName);
- } else {
- result.add(awayteamWebElement.findElement(By.xpath("./span")).getText());
- }
- return result;
- }
- private void CloseCookiePopup(WebDriver driver) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- WebElement acceptButton = driver.findElement(By.xpath("//button[@id='onetrust-accept-btn-handler']"));
- acceptButton.click();
- }
- private ChromeOptions getChromeOptions() {
- ChromeOptions options = new ChromeOptions();
- // Fixing 255 Error crashes
- options.addArguments("--no-sandbox");
- options.addArguments("--disable-dev-shm-usage");
- // Options to trick bot detection
- // Removing webdriver property
- options.addArguments("--disable-blink-features=AutomationControlled");
- options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
- options.setExperimentalOption("useAutomationExtension", null);
- // Changing the user agent / browser fingerprint
- options.addArguments("window-size=1920,1080");
- /*
- * options.addArguments(
- * "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
- * );
- */
- // Other
- options.addArguments("disable-infobars");
- return options;
- }
- }
|