Svenskaspel.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package parser;
  2. import data.GuiMysql;
  3. import objects.SoccerMatch;
  4. import objects.SoccerMatchAnalysis;
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.chrome.ChromeOptions;
  9. import org.openqa.selenium.remote.RemoteWebDriver;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.*;
  13. public class Svenskaspel {
  14. String matchesXPATH = "//div[@class = 'pg_coupon_static__event_container']";
  15. String date;
  16. Map<String, String> nameReplace = Map.ofEntries(
  17. new AbstractMap.SimpleEntry<String, String>("manchester united", "Manchester Utd"),
  18. new AbstractMap.SimpleEntry<String, String>("queens park rangers", "QPR"),
  19. new AbstractMap.SimpleEntry<String, String>("west bromwich", "West Brom"),
  20. new AbstractMap.SimpleEntry<String, String>("blackburn rovers", "Blackburn"),
  21. new AbstractMap.SimpleEntry<String, String>("nottingham forest", "Nottingham"),
  22. new AbstractMap.SimpleEntry<String, String>("wolverhampton", "Wolves"));
  23. public List<SoccerMatchAnalysis> GetStryktipsetSelenium(String date) throws MalformedURLException {
  24. this.date = date;
  25. String url = "https://spela.svenskaspel.se/stryktipset/resultat/" + date + "/statistik";
  26. // RemoteWebDriver seleniumDriver = getSeleniumDriver();
  27. // seleniumDriver.get(url);
  28. // WebDriverManager.chromedriver().driverVersion("124.0.6367.60").setup();
  29. // ChromeDriver driver = new ChromeDriver(getChromeOptions());
  30. ChromeOptions options = new ChromeOptions();
  31. // Fixing 255 Error crashes
  32. // options.addArguments("--no-sandbox");
  33. options.addArguments("--disable-dev-shm-usage");
  34. // Options to trick bot detection
  35. // Removing webdriver property
  36. options.addArguments("--disable-blink-features=AutomationControlled");
  37. options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
  38. options.setExperimentalOption("useAutomationExtension", null);
  39. // Changing the user agent / browser fingerprint
  40. options.addArguments("window-size=1920,1080");
  41. options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
  42. "Gecko) " + "Chrome/74.0.3729.169 Safari/537.36");
  43. // Other
  44. options.addArguments("disable-infobars");
  45. System.out.println("Getting page");
  46. RemoteWebDriver driver = new RemoteWebDriver(new URL("http://nordh.xyz:14444/wd/hub"), options);
  47. driver.get(url);
  48. CloseCookiePopup(driver);
  49. try {
  50. Thread.sleep(3000);
  51. } catch (InterruptedException e) {
  52. throw new RuntimeException(e);
  53. }
  54. List<SoccerMatchAnalysis> matches = FindMatches(driver);
  55. driver.close();
  56. driver.quit();
  57. return matches;
  58. }
  59. private List<SoccerMatchAnalysis> FindMatches(WebDriver driver) {
  60. List<SoccerMatch> soccerMatches = new ArrayList<>();
  61. List<SoccerMatchAnalysis> matchAnalyses = new ArrayList<>();
  62. List<WebElement> matches = driver.findElements(By.xpath(matchesXPATH));
  63. for (WebElement match : matches) {
  64. List<String> teamNames = GetNames(match);
  65. String homeTeamName = teamNames.get(0);
  66. String awayTeamName = teamNames.get(1);
  67. System.out.println("Match: " + homeTeamName + " " + awayTeamName);
  68. if (nameReplace.containsKey(homeTeamName.toLowerCase())) {
  69. homeTeamName = nameReplace.get(homeTeamName.toLowerCase());
  70. }
  71. if (nameReplace.containsKey(awayTeamName.toLowerCase())) {
  72. awayTeamName = nameReplace.get(awayTeamName.toLowerCase());
  73. }
  74. soccerMatches.add(GuiMysql.getInstance().getMatch(date,
  75. homeTeamName.trim().replace(" ", "%") + "%",
  76. awayTeamName.trim().replace(" ", "%") + "%"));
  77. }
  78. for (SoccerMatch match : soccerMatches) {
  79. try {
  80. matchAnalyses.add(new SoccerMatchAnalysis(match));
  81. } catch (NullPointerException e) {
  82. System.out.println(
  83. "Failed to get team info between " + match.getHomeTeamName() + "-" + match.getAwayTeamName());
  84. }
  85. }
  86. return matchAnalyses;
  87. }
  88. private List<String> GetNames(WebElement match) {
  89. List<String> result = new ArrayList<>();
  90. WebElement hometeamWebElement = match.findElement(By.xpath("./a/div[2]/span[1]"));
  91. WebElement awayteamWebElement = match.findElement(By.xpath("./a/div[2]/span[3]"));
  92. if (hometeamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
  93. String teamName = hometeamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
  94. result.add(teamName);
  95. } else {
  96. result.add(hometeamWebElement.findElement(By.xpath("./span")).getText());
  97. }
  98. if (awayteamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
  99. String teamName = awayteamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
  100. result.add(teamName);
  101. } else {
  102. result.add(awayteamWebElement.findElement(By.xpath("./span")).getText());
  103. }
  104. return result;
  105. }
  106. private void CloseCookiePopup(WebDriver driver) {
  107. try {
  108. Thread.sleep(1000);
  109. } catch (InterruptedException e) {
  110. throw new RuntimeException(e);
  111. }
  112. WebElement acceptButton = driver.findElement(By.xpath("//button[@id='onetrust-accept-btn-handler']"));
  113. acceptButton.click();
  114. }
  115. private ChromeOptions getChromeOptions() {
  116. ChromeOptions options = new ChromeOptions();
  117. // Fixing 255 Error crashes
  118. options.addArguments("--no-sandbox");
  119. options.addArguments("--disable-dev-shm-usage");
  120. // Options to trick bot detection
  121. // Removing webdriver property
  122. options.addArguments("--disable-blink-features=AutomationControlled");
  123. options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
  124. options.setExperimentalOption("useAutomationExtension", null);
  125. // Changing the user agent / browser fingerprint
  126. options.addArguments("window-size=1920,1080");
  127. /*
  128. * options.addArguments(
  129. * "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"
  130. * );
  131. */
  132. // Other
  133. options.addArguments("disable-infobars");
  134. return options;
  135. }
  136. }