Refactor API Football to v3

This commit is contained in:
2025-01-26 22:03:10 +01:00
parent bed7c64189
commit baffe58204
24 changed files with 1719 additions and 242 deletions

40
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,40 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "APIFootballUpdater",
"request": "launch",
"mainClass": "de.jeyp91.App",
"envFile": "${workspaceFolder}/.env",
"args": " --mode APIFootballUpdater --season 2025 --league 1 --configFile Tippliga"
},
{
"type": "java",
"name": "MatchesUpdaterFootball",
"request": "launch",
"mainClass": "de.jeyp91.App",
"envFile": "${workspaceFolder}/.env",
"args": " --mode MatchesUpdaterFootball --season 2025 --league 1 --configFile Tippliga"
},
{
"type": "java",
"name": "MatchdaysUpdater",
"request": "launch",
"mainClass": "de.jeyp91.App",
"envFile": "${workspaceFolder}/.env",
"args": " --mode MatchdaysUpdater --season 2025 --league 1 --configFile Tippliga"
},
{
"type": "java",
"name": "MatchesResultsUpdater",
"request": "launch",
"mainClass": "de.jeyp91.App",
"envFile": "${workspaceFolder}/.env",
"args": " --mode MatchesResultsUpdater --season 2025 --league 2 --configFile Tippliga"
}
]
}

View File

@@ -2,12 +2,13 @@
* This file was generated by the Gradle 'init' task.
*/
apply plugin: 'java'
apply plugin: 'application'
plugins {
id 'com.github.ben-manes.versions' version '0.52.0'
id 'java'
id 'application'
}
mainClassName = 'App'
sourceCompatibility = 16
targetCompatibility = 16
version = '1.0'
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
@@ -18,23 +19,22 @@ repositories {
}
dependencies {
implementation 'mysql:mysql-connector-java:8.0.32'
implementation 'mysql:mysql-connector-java:8.0.33'
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'com.google.code.gson:gson:2.10.1'
implementation platform('com.amazonaws:aws-java-sdk-bom:1.12.429')
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.429'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.16.0'
implementation 'com.google.api-client:google-api-client:2.2.0'
implementation 'com.google.apis:google-api-services-calendar:v3-rev20220715-2.0.0'
implementation 'com.google.guava:guava:31.1-jre'
implementation 'commons-cli:commons-cli:1.5.0'
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.31.0'
implementation 'com.google.api-client:google-api-client:2.7.2'
implementation 'com.google.apis:google-api-services-calendar:v3-rev20250115-2.0.0'
implementation 'com.google.guava:guava:33.4.0-jre'
implementation 'commons-cli:commons-cli:1.9.0'
implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0'
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'org.apache.logging.log4j:log4j-api:3.0.0-beta2'
implementation 'org.apache.logging.log4j:log4j-core:3.0.0-beta2'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
implementation 'software.amazon.awssdk:s3:2.30.6'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.testng:testng:7.7.0'
testImplementation 'org.testng:testng:7.10.2'
}
jar {
@@ -50,4 +50,4 @@ jar {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}

View File

@@ -0,0 +1,2 @@
#This file is generated by updateDaemonJvm
toolchainVersion=22

View File

@@ -1,5 +1,5 @@
#Tue May 26 17:58:07 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
distributionUrl=https://services.gradle.org/distributions/gradle-8.12.1-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists

View File

@@ -1,15 +1,4 @@
package de.jeyp91;
import de.jeyp91.apifootball.APIFootballUpdater;
import de.jeyp91.googlecalendar.TippligaGoogleEventManager;
import de.jeyp91.tippligaforum.MatchesListForumUpdater;
import de.jeyp91.tippliga.*;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import de.jeyp91.whatsapp.WhatsAppNotifier;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -19,6 +8,21 @@ import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFact
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import de.jeyp91.apifootball.APIFootballUpdater;
import de.jeyp91.tippliga.TLWMatchdaysUpdater;
import de.jeyp91.tippliga.TLWMatchesCreatorFootball;
import de.jeyp91.tippliga.TLWMatchesResultsUpdater;
import de.jeyp91.tippliga.TLWMatchesUpdaterFootball;
import de.jeyp91.tippliga.TLWTeamsUpdater;
import de.jeyp91.tippligaforum.MatchesListForumUpdater;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import de.jeyp91.whatsapp.WhatsAppNotifier;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
/**
* Hello world!
*

View File

@@ -27,8 +27,8 @@ public class ResourceProvider {
return array;
}
public static JSONArray getLigenConfig(int season) {
return getJSONArrayFromResource("Tippliga/" + season + "_Ligen.json");
public static JSONArray getLigenConfig() {
return getJSONArrayFromResource("Tippliga/Ligen_v3.json");
}
public static JSONArray getTeamIDMatcherConfig() {

View File

@@ -1,95 +1,100 @@
package de.jeyp91;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
public class S3Provider {
private static final Regions AWS_DEFAULT_REGION = Regions.EU_CENTRAL_1;
private static final Region AWS_DEFAULT_REGION = Region.EU_CENTRAL_1;
private static final String BUCKET_NAME = "tlw-database-tool-api-football-data";
private final S3Client s3;
public S3Provider() {
s3 = S3Client.builder()
.region(AWS_DEFAULT_REGION)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
}
private void writeToS3(String filename, String content) {
final AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withRegion(AWS_DEFAULT_REGION)
.build();
try {
s3.putObject(BUCKET_NAME, filename, content);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
this.s3.putObject(
PutObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(filename)
.build(),
RequestBody.fromString(content)
);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
}
}
public void writeFixturesToS3(int league, String content) {
writeToS3("fixtures/" + league + ".json", content);
public void writeFixturesToS3(int season, int league, String content) {
writeToS3(v3Filepath("fixtures", season, league), content);
}
public void writeRoundsToS3(int league, String content) {
writeToS3("rounds/" + league + ".json", content);
public void writeRoundsToS3(int season, int league, String content) {
writeToS3(v3Filepath("rounds", season, league), content);
}
private String getFileFromS3(String filename) {
final AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withRegion(AWS_DEFAULT_REGION)
.build();
StringBuilder builder = new StringBuilder();
try {
S3Object o = s3.getObject(BUCKET_NAME, filename);
S3ObjectInputStream s3is = o.getObjectContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(s3is));
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(filename)
.build();
BufferedReader reader = new BufferedReader(new InputStreamReader(
this.s3.getObject(getObjectRequest), StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (SdkClientException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return builder.toString();
}
String getFixturesStringFromS3(int league) {
return getFileFromS3("fixtures/" + league + ".json");
public JSONObject getFixturesJSONFromS3(int season, int league) {
return stringToJSONObject(getFileFromS3(v3Filepath("fixtures", season, league)));
}
public JSONObject getFixturesJSONFromS3(int league) {
String fixturesString = getFixturesStringFromS3(league);
public JSONObject getRoundsJSONFromS3(int season, int league) {
return stringToJSONObject(getFileFromS3(v3Filepath("rounds", season, league)));
}
private JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
JSONObject data = null;
try {
jsonObject = (JSONObject) parser.parse(fixturesString);
data = (JSONObject) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return jsonObject;
return data;
}
private String getRoundsStringFromS3(int league) {
return getFileFromS3("rounds/" + league + ".json");
}
public JSONObject getRoundsJSONFromS3(int league) {
String fixturesString = getRoundsStringFromS3(league);
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {
jsonObject = (JSONObject) parser.parse(fixturesString);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return jsonObject;
}
private String v3Filepath (String prefix, int season, int league) {
return "v3/" + prefix + "_" + season + "_" + league + ".json";
}
}

View File

@@ -1,14 +1,15 @@
package de.jeyp91.apifootball;
import de.jeyp91.S3Provider;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.util.ArrayList;
import java.util.HashMap;
import de.jeyp91.S3Provider;
public class APIFootballConnector {
@@ -33,7 +34,7 @@ public class APIFootballConnector {
APIFootballMatch matchWithID = null;
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(this.season, league);
for (APIFootballMatch singleMatch : allMatches) {
if (singleMatch.getAPIFootBallMatchID() == id) {
matchWithID = singleMatch;
@@ -48,7 +49,7 @@ public class APIFootballConnector {
ArrayList<APIFootballMatch> matchesOfMatchday = new ArrayList<>();
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(this.season, league);
for (APIFootballMatch singleMatch : allMatches) {
if (singleMatch.getMatchday() == matchday) {
matchesOfMatchday.add(singleMatch);
@@ -58,15 +59,15 @@ public class APIFootballConnector {
return matchesOfMatchday;
}
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int leagueId) {
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int season, int leagueId) {
ArrayList<APIFootballMatch> matchesList = new ArrayList<>();
S3Provider prov = new S3Provider();
if(!this.matches.containsKey(leagueId)) {
this.matches.put(leagueId, prov.getFixturesJSONFromS3(leagueId));
this.matches.put(leagueId, prov.getFixturesJSONFromS3(season, leagueId));
}
JSONObject matches = this.matches.get(leagueId);
JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));
JSONObject matchesObject = this.matches.get(leagueId);
JSONArray matchArray = (JSONArray) (matchesObject.get("response"));
for(int i = 0; i < matchArray.size(); i++) {
try {
@@ -83,19 +84,18 @@ public class APIFootballConnector {
public JSONObject getMatchdays(int leagueId) {
S3Provider prov = new S3Provider();
if(!this.rounds.containsKey(leagueId)) {
this.rounds.put(leagueId, prov.getRoundsJSONFromS3(leagueId));
this.rounds.put(leagueId, prov.getRoundsJSONFromS3(this.season, leagueId));
}
return this.rounds.get(leagueId);
}
public JSONObject getTeamsForLeague(int league) throws Exception {
String url = "https://v2.api-football.com/teams/league/" + league;
String content = new APIFootballUpdater().getRawData(url);
public JSONObject getTeamsForLeague(int season, int league) throws Exception {
String requestPath = "teams";
String content = new APIFootballUpdater().getRawData(requestPath, season, league);
return stringToJSONObject(content);
}
public static JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject data = null;
try {

View File

@@ -1,35 +1,31 @@
package de.jeyp91.apifootball;
import de.jeyp91.BaseMatch;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import de.jeyp91.BaseMatch;
public class APIFootballMatch extends BaseMatch {
private int matchId;
private int leagueId;
private String teamNameHome;
private String teamNameGuest;
private String round;
public APIFootballMatch(JSONObject json, int season) throws Exception {
this.matchId = Integer.parseInt(json.get("fixture_id").toString());
// TODO
this.leagueId = Integer.parseInt(json.get("league_id").toString());
this.teamIdHome = Integer.parseInt(((JSONObject) json.get("homeTeam")).get("team_id").toString());
this.teamIdGuest = Integer.parseInt(((JSONObject) json.get("awayTeam")).get("team_id").toString());
this.teamNameHome = ((JSONObject) json.get("homeTeam")).get("team_name").toString();
this.teamNameGuest = ((JSONObject) json.get("awayTeam")).get("team_name").toString();
this.goalsHome = getNumberOrNull(json.get("goalsHomeTeam"));
this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam"));
this.round = json.get("round").toString();
this.matchId = Integer.parseInt(((JSONObject) json.get("fixture")).get("id").toString());
this.leagueId = Integer.parseInt(((JSONObject) json.get("league")).get("id").toString());
this.teamIdHome = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("id").toString());
this.teamIdGuest = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("id").toString());
this.teamNameHome = ((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("name").toString();
this.teamNameGuest = ((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("name").toString();
this.round = ((JSONObject) json.get("league")).get("round").toString();
try {
this.matchday = getMatchdayFromRoundString(season, this.round, this.leagueId);
} catch (Exception e) {
throw new Exception("Did not find matchday for league '" + this.leagueId + "': '" + json.get("round").toString() + "'");
}
this.matchDatetime = (String) json.get("event_date");
this.status = parseStatus(json.get("statusShort").toString());
this.matchDatetime = (String) ((JSONObject) json.get("fixture")).get("date");
this.status = parseStatus(((JSONObject) ((JSONObject) json.get("fixture")).get("status")).get("short").toString());
this.parseResult(json);
}
@@ -39,15 +35,15 @@ public class APIFootballMatch extends BaseMatch {
public static int getMatchdayFromRoundString(int season, String round, int leagueId) {
round = round.replace(" ", "_");
Integer matchday = null;
Integer matchday = -1;
if (round.startsWith("Regular_Season_-_")) {
matchday = Integer.parseInt(round.replace("Regular_Season_-_", ""));
matchday = Integer.valueOf(round.replace("Regular_Season_-_", ""));
} else {
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject roundsObject = con.getMatchdays(leagueId);
JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures"));
JSONArray roundsArray = (JSONArray)(roundsObject.get("response"));
for (int i = 0; i < roundsArray.size(); i++) {
if(roundsArray.get(i).toString().equals(round)) {
if(roundsArray.get(i).toString().replace("_", " ").equals(round.replace("_", " "))) {
matchday = i + 1;
break;
}
@@ -57,15 +53,7 @@ public class APIFootballMatch extends BaseMatch {
}
private Integer getNumberOrNull(Object object) {
return object != null ? Integer.parseInt(object.toString()) : null;
}
public String getTeamNameHome() {
return this.teamNameHome;
}
public String getTeamNameGuest() {
return this.teamNameGuest;
return object != null ? Integer.valueOf(object.toString()) : null;
}
public String getRound() {
@@ -73,36 +61,24 @@ public class APIFootballMatch extends BaseMatch {
}
private int parseStatus(String statusShort) {
switch (statusShort) {
case "TBD":
case "NS": return 0;
case "FT":
case "AET":
case "PEN": return 3;
default: return 1;
}
return switch (statusShort) {
case "TBD", "NS" -> 0;
case "FT", "AET", "PEN" -> 3;
default -> 1;
};
}
private void parseResult(JSONObject json) {
Object resultFulltime = ((JSONObject) json.get("score")).get("fulltime");
if(resultFulltime != null) {
String[] result = resultFulltime.toString().split("-");
this.goalsHome = Integer.parseInt(result[0]);
this.goalsGuest = Integer.parseInt(result[1]);
}
this.goalsHome = getNumberOrNull(((JSONObject) resultFulltime).get("home"));
this.goalsGuest = getNumberOrNull(((JSONObject) resultFulltime).get("away"));
Object resultExtratime = ((JSONObject) json.get("score")).get("extratime");
if(resultExtratime != null) {
String[] result = resultExtratime.toString().split("-");
this.goalsOvertimeHome = this.goalsHome + Integer.parseInt(result[0]);
this.goalsOvertimeGuest = this.goalsGuest + Integer.parseInt(result[1]);
}
this.goalsOvertimeHome = getNumberOrNull(((JSONObject) resultExtratime).get("home"));
this.goalsOvertimeGuest = getNumberOrNull(((JSONObject) resultExtratime).get("away"));
Object resultPenalty = ((JSONObject) json.get("score")).get("penalty");
if(resultPenalty != null) {
String[] result = resultPenalty.toString().split("-");
this.goalsPenaltyHome = this.goalsOvertimeHome + Integer.parseInt(result[0]);
this.goalsPenaltyGuest = this.goalsOvertimeGuest + Integer.parseInt(result[1]);
}
this.goalsPenaltyHome = getNumberOrNull(((JSONObject) resultPenalty).get("home"));
this.goalsPenaltyGuest = getNumberOrNull(((JSONObject) resultPenalty).get("away"));
}
}

View File

@@ -9,7 +9,7 @@ public class APIFootballMatchesProvider {
APIFootballConnector conn;
public APIFootballMatchesProvider(int season) {
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season - 1);
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season);
}
public ArrayList<APIFootballMatch> getAPIFootballMatchesFromConfig(JSONObject matchdayConfig) {

View File

@@ -1,7 +1,9 @@
package de.jeyp91.apifootball;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
@@ -12,29 +14,30 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.*;
import java.util.HashSet;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
public class APIFootballUpdater {
private static final Logger logger = LogManager.getLogger(APIFootballUpdater.class);
private static final String baseurl = "https://v3.football.api-sports.io/";
public APIFootballUpdater() {
}
public void updateFixtures(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
public void updateFixtures(int season, int league) {
String requestPath = "fixtures";
S3Provider prov = new S3Provider();
try {
String content = getRawData(apiFootballUrl);
prov.writeFixturesToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeFixturesToS3(season, league, content);
} catch (Exception e) {
if(e.getMessage().toLowerCase().contains("rate limit'")) {
try {
Thread.sleep(1000 * 60);
String content = getRawData(apiFootballUrl);
prov.writeFixturesToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeFixturesToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
@@ -49,7 +52,7 @@ public class APIFootballUpdater {
public void updateAllFixtures(int season) {
HashSet<Integer> leagues = getLeagues(season);
for (Integer league : leagues) {
updateFixtures(league);
updateFixtures(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
@@ -61,7 +64,7 @@ public class APIFootballUpdater {
public void updateAllRounds(int season) {
HashSet<Integer> leagues = getLeagues(season);
for (Integer league : leagues) {
updateRounds(league);
updateRounds(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
@@ -70,18 +73,18 @@ public class APIFootballUpdater {
}
}
public void updateRounds(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
public void updateRounds(int season, int league) {
String requestPath = "fixtures/rounds";
S3Provider prov = new S3Provider();
try {
String content = getRawData(apiFootballUrl);
prov.writeRoundsToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeRoundsToS3(season, league, content);
} catch (Exception e) {
if(e.getMessage().toLowerCase().contains("rate limit'")) {
try {
Thread.sleep(1000 * 60);
String content = getRawData(apiFootballUrl);
prov.writeRoundsToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeRoundsToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
@@ -93,46 +96,21 @@ public class APIFootballUpdater {
}
}
public String getRawData(String requestUrl) throws Exception {
public String getRawData(String requestPath, int season, int league) throws Exception {
HttpClient client = HttpClientBuilder.create().build();
String requestUrl = baseurl + requestPath + "?season=" + (season-1) + "&league=" + league + "&timezone=Europe/Berlin";
HttpGet request = new HttpGet(requestUrl);
// add request header
request.addHeader("X-RapidAPI-Key", "a607e6a7437d9d52f3ac73e0b2704d0b");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
assert response != null;
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
request.addHeader("x-rapidapi-host", "v3.football.api-sports.io");
request.addHeader("x-rapidapi-key", "a607e6a7437d9d52f3ac73e0b2704d0b");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
checkErrors(requestUrl, result.toString());
@@ -140,7 +118,7 @@ public class APIFootballUpdater {
}
public HashSet<Integer> getLeagues(int season) {
JSONArray leaguesArray = ResourceProvider.getLigenConfig(season);
JSONArray leaguesArray = ResourceProvider.getLigenConfig();
HashSet<Integer> leagues = new HashSet<>();
for (Object leagueObject : leaguesArray) {
JSONObject leagueJSONObject = (JSONObject) leagueObject;
@@ -152,10 +130,10 @@ public class APIFootballUpdater {
public void checkErrors(String requestUrl, String result) throws Exception {
JSONObject resultObject = stringToJSONObject(result);
boolean containsError = ((JSONObject) resultObject.get("api")).containsKey("error");
int results = Integer.parseInt(((JSONObject) resultObject.get("api")).get("results").toString());
boolean containsError = !((JSONArray) resultObject.get("errors")).isEmpty();
int results = Integer.parseInt(resultObject.get("results").toString());
if(containsError) {
String errorMessage = ((JSONObject) resultObject.get("api")).get("error").toString();
String errorMessage = resultObject.get("errors").toString();
throw new Exception(requestUrl + " returned error: '" + errorMessage + "'");
} else if (results == 0) {
String statusMessage = ((JSONObject) resultObject.get("api")).get("status").toString();

View File

@@ -10,7 +10,7 @@ public class TeamIDMatcherTemplateCreator {
public TeamIDMatcherTemplateCreator(int season, int league) throws Exception {
this.con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject apiObject = con.getTeamsForLeague(league);
JSONObject apiObject = con.getTeamsForLeague(season, league);
JSONObject teamsObject = (JSONObject) apiObject.get("api");
this.teams = (JSONArray) teamsObject.get("teams");
}

View File

@@ -6,11 +6,6 @@ import java.sql.SQLException;
public class TLWMatchday {
final Integer STATUS_NOTSTARTED = 0;
final Integer STATUS_STARTED = 1;
final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
final Integer STATUS_FINISHED = 3;
private Integer season = null;
private Integer league = null;
private Integer matchday = null;
@@ -34,15 +29,15 @@ public class TLWMatchday {
final int MATCHES = 9;
try {
this.season = Integer.parseInt(rset.getString(SEASON));
this.league = Integer.parseInt(rset.getString(LEAGUE));
this.matchday = Integer.parseInt(rset.getString(MATCHDAY));
this.status = Integer.parseInt(rset.getString(STATUS));
this.season = Integer.valueOf(rset.getString(SEASON));
this.league = Integer.valueOf(rset.getString(LEAGUE));
this.matchday = Integer.valueOf(rset.getString(MATCHDAY));
this.status = Integer.valueOf(rset.getString(STATUS));
this.deliveryDate = rset.getString(DELIVERY_DATE);
this.deliveryDate2 = rset.getString(DELIVERY_DATE_2);
this.deliveryDate3 = rset.getString(DELIVERY_DATE_3);
this.matchdayName = rset.getString(MATCHDAY_NAME);
this.matches = Integer.parseInt(rset.getString(MATCHES));
this.matches = Integer.valueOf(rset.getString(MATCHES));
} catch (SQLException e) {
/* TODO */
e.printStackTrace();

View File

@@ -26,7 +26,7 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
this.season = season;
this.league = league;
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season - 1);
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season);
TippligaConfigProvider prov = new TippligaConfigProvider(season);
this.config = prov.getTippligaConfig(configFileName);

View File

@@ -11,10 +11,14 @@ public class MatchesListCreator {
private final JSONObject mainObject = new JSONObject();
private final String country;
private final String leagueName;
private int season;
private int league;
public MatchesListCreator(int league) {
public MatchesListCreator(int season, int league) {
this.season = season;
this.league = league;
S3Provider prov = new S3Provider();
JSONObject leagueConfig = prov.getFixturesJSONFromS3(league);
JSONObject leagueConfig = prov.getFixturesJSONFromS3(season, league);
JSONObject api = (JSONObject) leagueConfig.get("api");
JSONArray matchesAPIFootball = (JSONArray) api.get("fixtures");
@@ -42,7 +46,7 @@ public class MatchesListCreator {
Long leagueId = (Long) matchAPIFootball.get("league_id");
String matchdayString = matchAPIFootball.get("round").toString();
int matchday = APIFootballMatch.getMatchdayFromRoundString(2021, matchdayString, leagueId.intValue());
int matchday = APIFootballMatch.getMatchdayFromRoundString(this.season, matchdayString, leagueId.intValue());
String matchtime = matchAPIFootball.get("event_date").toString().replace("T", " ").substring(0, 16);
Long fixtureId = (Long) matchAPIFootball.get("fixture_id");

View File

@@ -24,7 +24,7 @@ public class MatchesListForumUpdater {
}
public void updateLeague(int season, int league) {
MatchesListCreator creator = new MatchesListCreator(league);
MatchesListCreator creator = new MatchesListCreator(season, league);
String content = creator.getMatchesBeautiful();
String contentWithCodeBBCode = "<r><CODE><s>[code]</s>" + content + "<e>[/code]</e></CODE></r>";
Integer postId = getPostId(season, creator.getCountry(), creator.getLeagueName());

View File

@@ -0,0 +1,97 @@
[
{
"country": "Germany",
"name": "Bundesliga",
"league_id": 78
},
{
"country": "Germany",
"name": "2. Bundesliga",
"league_id": 79
},
{
"country": "Germany",
"name": "3. Liga",
"league_id": 80
},
{
"country": "Germany",
"name": "Regionalliga - Bayern",
"league_id": 83
},
{
"country": "Germany",
"name": "Regionalliga - SudWest",
"league_id": 86
},
{
"country": "Germany",
"name": "U19 Bundesliga",
"league_id": 488
},
{
"country": "Germany",
"name": "Frauen Bundesliga",
"league_id": 82
},
{
"country": "Germany",
"name": "DFB Pokal",
"league_id": 81
},
{
"country": "Germany",
"name": "Super Cup",
"league_id": 529
},
{
"country": "England",
"name": "Premier League",
"league_id": 39
},
{
"country": "Spain",
"name": "La Liga",
"league_id": 140
},
{
"country": "Italy",
"name": "Serie A",
"league_id": 135
},
{
"country": "France",
"name": "Ligue 1",
"league_id": 61
},
{
"country": "Turkey",
"name": "Süper Lig",
"league_id": 203
},
{
"country": "Netherlands",
"name": "Eredivisie",
"league_id": 88
},
{
"country": "World",
"name": "UEFA Champions League",
"league_id": 2
},
{
"country": "World",
"name": "UEFA Europa League",
"league_id": 3
},
{
"country": "World",
"name": "UEFA Europa Conference League",
"league_id": 848
},
{
"country": "World",
"name": "UEFA Super Cup",
"league_id": 531
}
]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,152 @@
{
"numberOfMatchdays": 5,
"matchesPerMatchday": 0,
"pointMode": 4,
"pointsTendency": 1,
"pointsDifference": 2,
"pointsDirectHit": 3,
"ko": 1,
"matchdayConfig": [
{
"TLWMatchday": 1,
"matchdayName": "1. Runde",
"numberOfMatches": 32,
"matchesConfig": [
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 1,
"showTable": true
},
{
"type": "Placeholder",
"placeholderDatetime": "2024-08-16 18:00:00"
}
]
},
{
"TLWMatchday": 2,
"numberOfMatches": 16,
"matchdayName": "Achtelfinale",
"matchesConfig": [
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 2,
"showTable": true
},
{
"type": "Placeholder",
"placeholderDatetime": "2024-10-29 18:00:00"
}
]
},
{
"TLWMatchday": 3,
"numberOfMatches": 8,
"matchdayName": "Viertelfinale",
"matchesConfig": [
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 3,
"showTable": true
},
{
"type": "Placeholder",
"placeholderDatetime": "2024-12-03 18:30:00"
}
]
},
{
"TLWMatchday": 4,
"numberOfMatches": 4,
"matchdayName": "Halbfinale",
"matchesConfig": [
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 4,
"showTable": true
},
{
"type": "Placeholder",
"placeholderDatetime": "2025-02-04 18:30:00"
}
]
},
{
"TLWMatchday": 5,
"numberOfMatches": 6,
"matchdayName": "Finale",
"matchesConfig": [
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 5,
"showTable": true
},
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 5,
"showTable": true
},
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 6,
"showTable": true
},
{
"type": "AllMatchesOfMatchday",
"matchesLeague": 81,
"leagueMatchday": 6,
"showTable": true
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-04-01 20:45:00",
"formulaHome": "D",
"formulaGuest": "D"
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-04-02 20:45:00",
"formulaHome": "D",
"formulaGuest": "D"
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-04-01 20:45:00",
"formulaHome": "D",
"formulaGuest": "D"
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-04-02 20:45:00",
"formulaHome": "D",
"formulaGuest": "D"
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-05-24 20:00:00",
"formulaHome": "D",
"formulaGuest": "D"
},
{
"type": "PlaceholderSingleMatch",
"koMatch": true,
"placeholderDatetime": "2025-05-24 20:00:00",
"formulaHome": "D",
"formulaGuest": "D"
}
]
}
]
}

View File

@@ -1,5 +1,6 @@
package de.jeyp91;
import org.json.simple.JSONObject;
import org.junit.Test;
public class S3ProviderTest {
@@ -7,7 +8,7 @@ public class S3ProviderTest {
@Test
public void getFixturesFromS3Test() {
S3Provider prov = new S3Provider();
String rounds = prov.getFixturesStringFromS3(1240);
System.out.println(rounds);
JSONObject rounds = prov.getFixturesJSONFromS3(2025, 1240);
System.out.println(rounds.toJSONString());
}
}

View File

@@ -8,20 +8,20 @@ public class APIFootballUpdaterTest {
@Test
public void checkErrorsTest() {
String exceededLimitDayError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}";
String rateLimitError = "{\"results\":0,\"errors\":{\"rateLimit\":\"Too many requests. Your rate limit is 10 requests per minute.\"}}";
APIFootballUpdater updater = new APIFootballUpdater();
try {
updater.checkErrors("https://test.url/api", exceededLimitDayError);
updater.checkErrors("https://test.url/api", rateLimitError);
} catch (Exception e) {
System.out.println(e.getMessage());
assert e.getMessage().equals("https://test.url/api returned error: 'You have reached the request limit for the day'");
assert e.getMessage().equals("https://test.url/api returned error: '{\"rateLimit\":\"Too many requests. Your rate limit is 10 requests per minute.\"}'");
}
}
@Test
public void updateFixturesTest() {
APIFootballUpdater updater = new APIFootballUpdater();
// updater.updateFixtures(1240);
updater.updateFixtures(2025, 79);
}
@Test

View File

@@ -2,10 +2,6 @@ package de.jeyp91.tippliga;
import org.junit.Test;
import java.text.ParseException;
import static org.junit.Assert.assertEquals;
public class TLWMatchesCreatorTipperTest {
int season = 2025;

View File

@@ -1,11 +1,8 @@
package de.jeyp91.tippliga;
import org.junit.Test;
import java.text.ParseException;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TLWTeamsCreatorTest {

View File

@@ -1,41 +1,40 @@
package de.jeyp91.tippligaforum;
import de.jeyp91.tippligaforum.MatchesListCreator;
import org.junit.Test;
public class MatchesListCreatorTest {
@Test
public void getMatchesTest2664() {
MatchesListCreator creator = new MatchesListCreator(2664);
MatchesListCreator creator = new MatchesListCreator(2025, 2664);
String matches = creator.getMatchesBeautiful();
// System.out.println(matches);
}
@Test
public void getMatchesTest2743() {
MatchesListCreator creator = new MatchesListCreator(2743);
MatchesListCreator creator = new MatchesListCreator(2025, 2743);
String matches = creator.getMatchesBeautiful();
System.out.println(matches);
}
@Test
public void getMatchesTest2755() {
MatchesListCreator creator = new MatchesListCreator(2755);
MatchesListCreator creator = new MatchesListCreator(2025, 2755);
String matches = creator.getMatchesBeautiful();
// System.out.println(matches);
}
@Test
public void getMatchesTest2790() {
MatchesListCreator creator = new MatchesListCreator(2790);
MatchesListCreator creator = new MatchesListCreator(2025, 2790);
String matches = creator.getMatchesBeautiful();
// System.out.println(matches);
}
@Test
public void getMatchesTest2795() {
MatchesListCreator creator = new MatchesListCreator(2795);
MatchesListCreator creator = new MatchesListCreator(2025, 2795);
String matches = creator.getMatchesBeautiful();
// System.out.println(matches);
}