diff --git a/jenkinsfiles/SupercupUpdater/Jenkinsfile b/jenkinsfiles/SupercupUpdater/Jenkinsfile index 7fcbca3..6d7e62e 100644 --- a/jenkinsfiles/SupercupUpdater/Jenkinsfile +++ b/jenkinsfiles/SupercupUpdater/Jenkinsfile @@ -11,12 +11,19 @@ pipeline { steps { script { String jdkPath = tool name: 'OpenJDK', type: 'jdk' - withCredentials([usernamePassword(credentialsId: 'aws', passwordVariable: 'AWS_SECRET_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) { - withEnv(["JAVA_HOME=${jdkPath}/jdk-19.0.2","AWS_SECRET_KEY=${AWS_SECRET_KEY}", "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}"]) { + withCredentials([ + usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_KEY'), + usernamePassword(credentialsId: 'forum_database', usernameVariable: 'TLW_DATABASE_USERNAME', passwordVariable: 'TLW_DATABASE_PASSWORD'), + usernamePassword(credentialsId: 'forum_user', usernameVariable: 'FORUM_USERNAME', passwordVariable: 'FORUM_PASSWORD') + ]) { + withEnv([ + "JAVA_HOME=${jdkPath}/jdk-19.0.2" + ]) { try { sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesUpdaterFootball --season ${season} --league 45 --configFile Supercup 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchdaysUpdater --season ${season} --league 45 --configFile Supercup 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode TeamsUpdater --season ${season} --league 45 --configFile Supercup 2>&1 >> log.txt" + sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesResultsUpdater --season ${season} --league 45 --configFile Supercup 2>&1 >> log.txt" } catch (Exception e) { telegramSendManual("TLW-Database-Tool crashed!") } diff --git a/jenkinsfiles/TippligaUpdater/Jenkinsfile b/jenkinsfiles/TippligaUpdater/Jenkinsfile index 17dfb87..16c84d0 100644 --- a/jenkinsfiles/TippligaUpdater/Jenkinsfile +++ b/jenkinsfiles/TippligaUpdater/Jenkinsfile @@ -11,16 +11,24 @@ pipeline { steps { script { String jdkPath = tool name: 'OpenJDK19', type: 'jdk' - withCredentials([usernamePassword(credentialsId: 'aws', passwordVariable: 'AWS_SECRET_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) { - withEnv(["JAVA_HOME=${jdkPath}/jdk-19.0.2", "AWS_SECRET_KEY=${AWS_SECRET_KEY}", "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}"]) { + withCredentials([ + usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_KEY'), + usernamePassword(credentialsId: 'forum_database', usernameVariable: 'TLW_DATABASE_USERNAME', passwordVariable: 'TLW_DATABASE_PASSWORD'), + usernamePassword(credentialsId: 'forum_user', usernameVariable: 'FORUM_USERNAME', passwordVariable: 'FORUM_PASSWORD') + ]) { + withEnv([ + "JAVA_HOME=${jdkPath}/jdk-19.0.2" + ]) { try { sh "ls build/libs" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesUpdaterFootball --season ${season} --league 1 --configFile Tippliga 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchdaysUpdater --season ${season} --league 1 --configFile Tippliga 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode TeamsUpdater --season ${season} --league 1 --configFile Tippliga 2>&1 >> log.txt" + sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesResultsUpdater --season ${season} --league 1 --configFile Tippliga 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesUpdaterFootball --season ${season} --league 2 --configFile Tippliga 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchdaysUpdater --season ${season} --league 2 --configFile Tippliga 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode TeamsUpdater --season ${season} --league 2 --configFile Tippliga 2>&1 >> log.txt" + sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesResultsUpdater --season ${season} --league 2 --configFile Tippliga 2>&1 >> log.txt" } catch (Exception e) { telegramSendManual("TLW-Database-Tool crashed!") } diff --git a/jenkinsfiles/WTLPokalUpdater/Jenkinsfile b/jenkinsfiles/WTLPokalUpdater/Jenkinsfile index a77f1f6..33a9e44 100644 --- a/jenkinsfiles/WTLPokalUpdater/Jenkinsfile +++ b/jenkinsfiles/WTLPokalUpdater/Jenkinsfile @@ -11,12 +11,19 @@ pipeline { steps { script { String jdkPath = tool name: 'OpenJDK19', type: 'jdk' - withCredentials([usernamePassword(credentialsId: 'aws', passwordVariable: 'AWS_SECRET_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) { - withEnv(["JAVA_HOME=${jdkPath}/jdk-19.0.2","AWS_SECRET_KEY=${AWS_SECRET_KEY}", "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}"]) { + withCredentials([ + usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_KEY'), + usernamePassword(credentialsId: 'forum_database', usernameVariable: 'TLW_DATABASE_USERNAME', passwordVariable: 'TLW_DATABASE_PASSWORD'), + usernamePassword(credentialsId: 'forum_user', usernameVariable: 'FORUM_USERNAME', passwordVariable: 'FORUM_PASSWORD') + ]) { + withEnv([ + "JAVA_HOME=${jdkPath}/jdk-19.0.2" + ]) { try { sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesUpdaterFootball --season ${season} --league 48 --configFile WTL-Pokal 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchdaysUpdater --season ${season} --league 48 --configFile WTL-Pokal 2>&1 >> log.txt" sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode TeamsUpdater --season ${season} --league 48 --configFile WTL-Pokal 2>&1 >> log.txt" + sh "java -jar build/libs/tlw-database-tool-1.0.jar --mode MatchesResultsUpdater --season ${season} --league 48 --configFile WTL-Pokal 2>&1 >> log.txt" } catch (Exception e) { telegramSendManual("TLW-Database-Tool crashed!") } diff --git a/src/main/java/de/jeyp91/App.java b/src/main/java/de/jeyp91/App.java index 01170e0..5dfa7b5 100644 --- a/src/main/java/de/jeyp91/App.java +++ b/src/main/java/de/jeyp91/App.java @@ -59,6 +59,10 @@ public class App { sql = tlwMatchesUpdaterFootball.getUpdateSQL(); beautifulInfo = tlwMatchesUpdaterFootball.getBeautifulInfo(); break; + case "MatchesResultsUpdater": + TLWMatchesResultsUpdater tlwMatchesResultsUpdater = new TLWMatchesResultsUpdater(season, league, configFile); + beautifulInfo = tlwMatchesResultsUpdater.getBeautifulInfo(); + break; case "TeamsUpdater": TLWTeamsUpdater teamsUpdater = new TLWTeamsUpdater(season, league, configFile); sql = teamsUpdater.getInsertSQL(); @@ -94,7 +98,7 @@ public class App { parser.addArgument("-m", "--mode") .dest("mode") - .choices("MatchdaysUpdater", "MatchesCreatorFootball", "MatchesUpdaterFootball", "TeamsUpdater", "APIFootballUpdater", "MatchesListGistUpdater", "PostChecksum") + .choices("MatchdaysUpdater", "MatchesCreatorFootball", "MatchesUpdaterFootball", "MatchesResultsUpdater", "TeamsUpdater", "APIFootballUpdater", "MatchesListGistUpdater", "PostChecksum") .help("") .required(true) .type(String.class); diff --git a/src/main/java/de/jeyp91/BaseMatch.java b/src/main/java/de/jeyp91/BaseMatch.java index bbdef5c..b5e466d 100644 --- a/src/main/java/de/jeyp91/BaseMatch.java +++ b/src/main/java/de/jeyp91/BaseMatch.java @@ -2,10 +2,10 @@ package de.jeyp91; public abstract class BaseMatch { - public final Integer COMPARISON_IDENTICAL = 0; - public final Integer COMPARISON_DIFFERENT = 1; - public final Integer COMPARISON_DIFFERENT_DATETIME = 2; - public final Integer COMPARISON_DIFFERENT_RESULT = 3; + public static final Integer COMPARISON_IDENTICAL = 0; + public static final Integer COMPARISON_DIFFERENT = 1; + public static final Integer COMPARISON_DIFFERENT_DATETIME = 2; + public static final Integer COMPARISON_DIFFERENT_RESULT = 3; protected Integer teamIdHome; protected Integer teamIdGuest; @@ -18,6 +18,8 @@ public abstract class BaseMatch { protected Integer matchday = null; protected String matchDatetime = null; + protected Integer updateStatus = COMPARISON_IDENTICAL; + public Integer getMatchday() { return this.matchday; } @@ -57,4 +59,10 @@ public abstract class BaseMatch { public String getTeamNameGuest() { return this.teamNameGuest; } + + public void setUpdateStatus(Integer newStatus) { this.updateStatus = newStatus; } + + public Integer getUpdateStatus() { + return this.updateStatus; + } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatch.java b/src/main/java/de/jeyp91/tippliga/TLWMatch.java index bb14d4e..665b2de 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatch.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatch.java @@ -14,10 +14,11 @@ import org.apache.logging.log4j.Logger; */ public class TLWMatch extends BaseMatch{ - public final Integer STATUS_NOTSTARTED = 0; - public final Integer STATUS_STARTED = 1; - public final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2; - public final Integer STATUS_FINISHED = 3; + public static final Integer STATUS_NOTSTARTED = 0; + public static final Integer STATUS_STARTED = 1; + public static final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2; + public static final Integer STATUS_FINISHED = 3; + public static final Integer STATUS_NOT_EVALUATED = 4; private Integer season = null; private Integer league = null; diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java index 68a8c4b..2ee5c6a 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java @@ -1,6 +1,7 @@ package de.jeyp91.tippliga; import de.jeyp91.apifootball.APIFootballMatch; +import de.jeyp91.teamidmatcher.TeamIDMatcher; import de.jeyp91.tippligaforum.TippligaConfigProvider; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -100,4 +101,28 @@ public class TLWMatchesManagerBase { date2 = date2.toLocalDate().atTime(0, 0, 0); return (int) date1.until(date2, ChronoUnit.DAYS); } + + protected TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList tlwMatches) throws NullPointerException{ + int foundMatches = 0; + TLWMatch matchingMatch = null; + for(TLWMatch match : tlwMatches) { + Integer apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME); + Integer apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST); + int tlwTeamIdHome = match.getTeamIdHome(); + int tlwTeamIdGuest = match.getTeamIdGuest(); + + if(apiTeamIdHome == null + || apiTeamIdGuest == null) { + throw new NullPointerException(); + } + if( + apiTeamIdHome.equals(tlwTeamIdHome) + && apiTeamIdGuest.equals(tlwTeamIdGuest) + ) { + foundMatches++; + matchingMatch = match; + } + } + return matchingMatch; + } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java new file mode 100644 index 0000000..3e4b5b9 --- /dev/null +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java @@ -0,0 +1,140 @@ +package de.jeyp91.tippliga; + +import de.jeyp91.StatusHolder; +import de.jeyp91.apifootball.APIFootballMatch; +import de.jeyp91.apifootball.APIFootballMatchesProvider; +import de.jeyp91.teamidmatcher.TeamIDMatcher; +import de.jeyp91.tippligaforum.TippligaSQLConnector; +import de.jeyp91.tippligaforum.TippligaWebsiteConnector; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class TLWMatchesResultsUpdater extends TLWMatchesManagerBase { + private static final Logger logger = LogManager.getLogger(TLWMatchesResultsUpdater.class); + + + ArrayList> tlwMatchesUpdated = new ArrayList<>(); + + private String beautifulInfo = ""; + + public TLWMatchesResultsUpdater(int season, int league, String configFileName) { + this.season = season; + this.league = league; + + super.initConfigParamsFromFile(configFileName); + initUpdates(); + applyUpdates(); + } + + private void initUpdates() { + + APIFootballMatchesProvider apiFootballMatchesProvider = new APIFootballMatchesProvider(this.season); + TippligaSQLConnector tippligaSQLConnector = TippligaSQLConnector.getInstance(); + for (Object singleMatchdayConfig : this.matchdayConfig) { + int tlwMatchday = ((Long) ((JSONObject) singleMatchdayConfig).get("TLWMatchday")).intValue(); + JSONArray matchesConfig = (JSONArray) ((JSONObject) singleMatchdayConfig).get("matchesConfig"); + + ArrayList apiFootballMatches = apiFootballMatchesProvider.getAPIFootballMatchesFromConfig(matchesConfig); + + ArrayList tlwMatchesOriginalMatchday = tippligaSQLConnector.getMatches(String.valueOf(this.season), String.valueOf(this.league), String.valueOf(tlwMatchday)); + ArrayList tlwMatchesUpdatedMatchday = new ArrayList<>(); + for (TLWMatch match : tlwMatchesOriginalMatchday) { + tlwMatchesUpdatedMatchday.add(new TLWMatch(match)); + } + + if (apiFootballMatches.size() > tlwMatchesOriginalMatchday.size()) { + logger.error("Not matching config!"); + } + + if(apiFootballMatches.size() > 0) { + updateResults(tlwMatchesUpdatedMatchday, apiFootballMatches); + } + + updateBeautifulInfo(tlwMatchesOriginalMatchday, tlwMatchesUpdatedMatchday); + tlwMatchesUpdatedMatchday.removeIf(tlwMatch -> tlwMatch.getUpdateStatus() != TLWMatch.COMPARISON_DIFFERENT_RESULT); + if(tlwMatchesUpdatedMatchday.size() > 0) this.tlwMatchesUpdated.add(tlwMatchesUpdatedMatchday); + } + } + + public void applyUpdates() { + TippligaWebsiteConnector con; + try { + con = new TippligaWebsiteConnector(); + } catch (IOException e) { + throw new RuntimeException(e); + } + this.tlwMatchesUpdated.forEach(tlwMatches -> { + con.updateResultsAdmin(this.season, this.league, tlwMatches.get(0).getMatchday(), tlwMatches); + }); + } + + private void updateResults( + ArrayList tlwMatchesUpdated, + ArrayList apiFootballMatches) + { + ArrayList tlwMatchesCopy = new ArrayList<>(tlwMatchesUpdated); + for(APIFootballMatch apiFootballMatch : apiFootballMatches) { + try { + TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy); + tlwMatchesCopy.remove(tlwMatch); + if(Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_STARTED) + || Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_PROVISIONAL_RESULT_AVAILABLE) + || Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_FINISHED)) { + if(!Objects.equals(tlwMatch.getGoalsHome(), apiFootballMatch.getGoalsHome()) + || !Objects.equals(tlwMatch.getGoalsGuest(), apiFootballMatch.getGoalsGuest()) + ) { + tlwMatch.setGoalsHome(apiFootballMatch.getGoalsHome()); + tlwMatch.setGoalsGuest(apiFootballMatch.getGoalsGuest()); + tlwMatch.setUpdateStatus(TLWMatch.COMPARISON_DIFFERENT_RESULT); + } + if(tlwMatch.getKoMatch() == 1 && + (!Objects.equals(tlwMatch.getGoalsOvertimeHome(), apiFootballMatch.getGoalsOvertimeHome()) + || !Objects.equals(tlwMatch.getGoalsOvertimeGuest(), apiFootballMatch.getGoalsOvertimeGuest())) + ) { + tlwMatch.setGoalsOvertimeHome(apiFootballMatch.getGoalsOvertimeHome()); + tlwMatch.setGoalsOvertimeGuest(apiFootballMatch.getGoalsOvertimeGuest()); + tlwMatch.setUpdateStatus(TLWMatch.COMPARISON_DIFFERENT_RESULT); + } + } + } catch (NullPointerException ignored) { + + } + } + } + + private void updateBeautifulInfo(ArrayList matchesOriginal, ArrayList matchesUpdated) { + for (int i = 0; i < matchesOriginal.size(); i++) { + updateBeautifulInfo(matchesOriginal.get(i), matchesUpdated.get(i)); + } + } + + private void updateBeautifulInfo(TLWMatch matchOriginal, TLWMatch matchUpdated) { + String beautifulInfoStart = "Aktualisiere Saison " + matchOriginal.getSeason() + ", " + + "Liga " + matchOriginal.getLeague() + ", " + + "Spieltag " + matchOriginal.getMatchday() + ", " + + "Spielnummer " + matchOriginal.getMatchNo() + ", " + + "Spiel '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdHome()) + "' - '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdGuest()) + "', "; + + if(!Objects.equals(matchOriginal.getGoalsHome(), matchUpdated.getGoalsHome()) || !Objects.equals(matchOriginal.getGoalsGuest(), matchUpdated.getGoalsGuest())) { + this.beautifulInfo += beautifulInfoStart + + "Ergebnis zu '" + matchUpdated.getGoalsHome() + ":" + matchUpdated.getGoalsGuest() + "'.\n"; + } + + if(matchOriginal.getKoMatch() == 1 && (!Objects.equals(matchOriginal.getGoalsOvertimeHome(), matchUpdated.getGoalsOvertimeHome()) || !Objects.equals(matchOriginal.getGoalsOvertimeGuest(), matchUpdated.getGoalsOvertimeGuest()))) { + this.beautifulInfo += beautifulInfoStart + + "Ergebnis Nachspielzeit zu '" + matchUpdated.getGoalsOvertimeHome() + ":" + matchUpdated.getGoalsOvertimeGuest() + "'.\n"; + } + } + + public String getBeautifulInfo() { + return this.beautifulInfo; + } +} diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java index 0773442..aa4c6c5 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java @@ -220,30 +220,6 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { } } - private TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList tlwMatches) throws NullPointerException{ - int foundMatches = 0; - TLWMatch matchingMatch = null; - for(TLWMatch match : tlwMatches) { - Integer apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME); - Integer apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST); - int tlwTeamIdHome = match.getTeamIdHome(); - int tlwTeamIdGuest = match.getTeamIdGuest(); - - if(apiTeamIdHome == null - || apiTeamIdGuest == null) { - throw new NullPointerException(); - } - if( - apiTeamIdHome.equals(tlwTeamIdHome) - && apiTeamIdGuest.equals(tlwTeamIdGuest) - ) { - foundMatches++; - matchingMatch = match; - } - } - return matchingMatch; - } - public ArrayList getTLWMatchesUpdated() { return this.tlwMatchesUpdated; } diff --git a/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java b/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java index a920662..9fa698e 100644 --- a/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java +++ b/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java @@ -35,6 +35,8 @@ public class TippligaSQLConnector { } private TippligaSQLConnector() { + String databaseUsername = System.getenv("TLW_DATABASE_USERNAME"); + String databasePassword = System.getenv("TLW_DATABASE_PASSWORD"); String jdbcUrlLocalhost = "jdbc:mysql://localhost:3306/d0144ddb" + "?user=root" + "&password=" + @@ -44,8 +46,8 @@ public class TippligaSQLConnector { "&serverTimezone=UTC" + "&allowMultiQueries=true"; String jdbcUrlProd = "jdbc:mysql://w00f4eaf.kasserver.com:3306/d0144ddb" + - "?user=d0144ddb" + - "&password=TippligaWuerzb_1" + + "?user=" + databaseUsername + + "&password=" + databasePassword + "&useUnicode=true" + "&useJDBCCompliantTimezoneShift=true" + "&useLegacyDatetimeCode=false" + diff --git a/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java b/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java index 2c79e27..a4b71cc 100644 --- a/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java +++ b/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java @@ -1,5 +1,6 @@ package de.jeyp91.tippligaforum; +import de.jeyp91.tippliga.TLWMatch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -24,9 +25,10 @@ public class TippligaWebsiteConnector { CookieStore cookieStore = cm.getCookieStore(); private static final Logger logger = LogManager.getLogger(TippligaWebsiteConnector.class); private final HttpClient client; - - private final String username = "Julian"; - private final String password = "original"; + private final String username = System.getenv("FORUM_USERNAME"); + private final String password = System.getenv("FORUM_PASSWORD"); + private String lastCreationTime = ""; + private String lastFormToken = ""; public TippligaWebsiteConnector() throws IOException { CookieHandler.setDefault(cm); @@ -37,7 +39,6 @@ public class TippligaWebsiteConnector { .build(); login(); adminLogin(); - updateResultsAdmin(); } private void login() throws IOException { @@ -57,8 +58,8 @@ public class TippligaWebsiteConnector { Matcher matcher = pattern.matcher(reqBody); matcher.find(); - String creationTime = matcher.group(1); - String formToken = matcher.group(2); + this.lastCreationTime = matcher.group(1); + this.lastFormToken = matcher.group(2); String sid = matcher.group(3); Map loginParameters = new HashMap<>(); @@ -68,8 +69,8 @@ public class TippligaWebsiteConnector { loginParameters.put("viewonline", "on"); loginParameters.put("username", username); loginParameters.put("password", password); - loginParameters.put("creation_time", creationTime); - loginParameters.put("form_token", formToken); + loginParameters.put("creation_time", this.lastCreationTime); + loginParameters.put("form_token", this.lastFormToken); loginParameters.put("sid", sid); String loginForm = loginParameters.entrySet() @@ -113,8 +114,8 @@ public class TippligaWebsiteConnector { Matcher matcher = pattern.matcher(reqBody); matcher.find(); - String creationTime = matcher.group(1); - String formToken = matcher.group(2); + this.lastCreationTime = matcher.group(1); + this.lastFormToken = matcher.group(2); sid = matcher.group(3); String credential = matcher.group(4); @@ -123,8 +124,8 @@ public class TippligaWebsiteConnector { loginParameters.put("redirect", "./../adm/index.php?sid=" + sid); loginParameters.put("username", username); loginParameters.put("password_" + credential, password); - loginParameters.put("creation_time", creationTime); - loginParameters.put("form_token", formToken); + loginParameters.put("creation_time", this.lastCreationTime); + loginParameters.put("form_token", this.lastFormToken); loginParameters.put("sid", sid); loginParameters.put("credential", credential); @@ -150,7 +151,7 @@ public class TippligaWebsiteConnector { List loginReqCookieList = cookieStore.getCookies(); } - private void updateResultsAdmin() { + public void updateResultsAdmin(int season, int league, int matchday, List matches) { String sid = getSidFromCookie(); String url = "https://tippliga-wuerzburg.de/adm/index.php?i=-football-football-acp-results_module&mode=manage&sid=" + sid; @@ -170,26 +171,23 @@ public class TippligaWebsiteConnector { Matcher matcher = pattern.matcher(reqBody); matcher.find(); - String creationTime = matcher.group(1); - String formToken = matcher.group(2); - return; - } + this.lastCreationTime = matcher.group(1); + this.lastFormToken = matcher.group(2); - private void updateResults() { Map resultParameters = new HashMap<>(); - resultParameters.put("s", "2023"); - resultParameters.put("l", "2"); - resultParameters.put("m", "32"); - resultParameters.put("action", "result"); - resultParameters.put("goalsh384", "4"); - resultParameters.put("oldgoalsh384", "4"); - resultParameters.put("goalsg384", "3"); - resultParameters.put("oldgoalsg384", "4"); - resultParameters.put("goalsh380", ""); - resultParameters.put("oldgoalsh380", ""); - resultParameters.put("goalsg380", ""); - resultParameters.put("oldgoalsg380", ""); + resultParameters.put("s", String.valueOf(season)); + resultParameters.put("l", String.valueOf(league)); + resultParameters.put("m", String.valueOf(matchday)); + resultParameters.put("edit", "Speichern"); + resultParameters.put("creation_time", this.lastCreationTime); + resultParameters.put("form_token", this.lastFormToken); + matches.forEach(tlwMatch -> { + String matchNo = String.valueOf(tlwMatch.getMatchNo()); + resultParameters.put("select_" + matchNo, "on"); + resultParameters.put("goals_home_" + matchNo, String.valueOf(tlwMatch.getGoalsHome())); + resultParameters.put("goals_guest_" + matchNo, String.valueOf(tlwMatch.getGoalsGuest())); + }); String resultForm = resultParameters.entrySet() .stream() @@ -197,7 +195,7 @@ public class TippligaWebsiteConnector { .collect(Collectors.joining("&")); HttpRequest resultReq = HttpRequest.newBuilder() - .uri(URI.create("https://tippliga-wuerzburg.de/app.php/football/results?s=2023&l=2&m=32&action=result")) + .uri(URI.create("https://tippliga-wuerzburg.de/adm/index.php?i=-football-football-acp-results_module&mode=manage&sid=" + sid)) .headers("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString(resultForm)) .build(); @@ -209,8 +207,11 @@ public class TippligaWebsiteConnector { } catch (IOException | InterruptedException e) { e.printStackTrace(); } + matcher = pattern.matcher(resultReqBody); + matcher.find(); - logger.info(resultReqBody); + this.lastCreationTime = matcher.group(1); + this.lastFormToken = matcher.group(2); } private String getSidFromCookie() { diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java new file mode 100644 index 0000000..dbb7c7b --- /dev/null +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java @@ -0,0 +1,13 @@ +package de.jeyp91.tippliga; + +import org.junit.Test; + +public class TLWMatchesResultsUpdaterTest { + + @Test + public void updateResultsTest1() { + TLWMatchesResultsUpdater updater = new TLWMatchesResultsUpdater(2023, 2, "Tippliga"); + System.out.println(updater.getBeautifulInfo()); + return; + } +} diff --git a/src/test/java/de/jeyp91/tippligaforum/TippligaWebsiteConnectorTest.java b/src/test/java/de/jeyp91/tippligaforum/TippligaWebsiteConnectorTest.java index 8cd19a0..62f7189 100644 --- a/src/test/java/de/jeyp91/tippligaforum/TippligaWebsiteConnectorTest.java +++ b/src/test/java/de/jeyp91/tippligaforum/TippligaWebsiteConnectorTest.java @@ -1,12 +1,21 @@ package de.jeyp91.tippligaforum; +import de.jeyp91.tippliga.TLWMatch; import org.junit.Test; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; public class TippligaWebsiteConnectorTest { @Test - public void getChecksumOfPostTest() throws IOException { + public void updateResultsAdminTest() throws IOException { TippligaWebsiteConnector con = new TippligaWebsiteConnector(); + List matches = new ArrayList<>(); + TLWMatch match = new TLWMatch(2023, 48, 5, 61, "0", 3, 0); + match.setGoalsHome(1); + match.setGoalsGuest(5); + matches.add(match); + con.updateResultsAdmin(2023, 48, 5, matches); } }