Improve error output and add team id matcher template creator

This commit is contained in:
2020-11-04 21:22:27 +01:00
parent 2f411aae67
commit d42edcf01c
15 changed files with 158 additions and 154 deletions

View File

@@ -49,10 +49,10 @@ public class App {
break;
case "APIFootballUpdater":
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater(season);
apiFootballUpdater.updateAllFixtures(configFile);
apiFootballUpdater.updateAllFixtures();
break;
case "MatchesListGistUpdater":
MatchesListGistUpdater matchesListGistUpdater = new MatchesListGistUpdater(configFile);
MatchesListGistUpdater matchesListGistUpdater = new MatchesListGistUpdater();
matchesListGistUpdater.updateAllLeagues();
break;
default:

View File

@@ -1,6 +1,7 @@
package de.jeyp91;
import com.google.common.collect.HashBiMap;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.gists.GistProvider;
import org.apache.logging.log4j.*;
import org.json.simple.JSONArray;
@@ -12,6 +13,8 @@ public class TeamIDMatcher {
private static final Logger logger = LogManager.getLogger(TeamIDMatcher.class);
private static JSONArray teams;
private static boolean init = false;
public static final int HOME = 0;
public static final int GUEST = 1;
private static void initBiMap() {
if(!init) {
@@ -39,16 +42,19 @@ public class TeamIDMatcher {
}
}
public static Integer getTippligaIdFromApiFootballId(Integer id) {
if(id == null) return null;
public static Integer getTippligaIdFromApiFootballId(APIFootballMatch match, int homeguest) {
Integer apiFootballId = homeguest == HOME ? match.getTeamIdHome() : match.getTeamIdGuest();
Integer tlwId = null;
if(apiFootballId == null) return null;
initBiMap();
if(ids.inverse().containsKey(id)) {
return ids.inverse().get(id);
if(ids.inverse().containsKey(apiFootballId)) {
tlwId = ids.inverse().get(apiFootballId);
} else {
logger.error("API Football ID: " + id + " not in ID Matcher.");
String teamname = homeguest == HOME ? match.getTeamNameHome() : match.getTeamNameGuest();
logger.error("Could not find id " + apiFootballId + " for " + teamname);
StatusHolder.setError();
return null;
}
return tlwId;
}
public static String getTeamNameFromTippligaId(Integer id) {

View File

@@ -121,4 +121,23 @@ public class APIFootballConnector {
return object;
}
public JSONObject getTeamsForLeague(int league) {
String url = "https://v2.api-football.com/teams/league/" + league;
String content = new APIFootballUpdater(0).getRawData(url);
return stringToJSONObject(content);
}
private JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
return data;
}
}

View File

@@ -1,24 +1,19 @@
package de.jeyp91.apifootball;
import com.google.common.io.Resources;
import de.jeyp91.gists.GistProvider;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.util.HashSet;
public class APIFootballUpdater {
private int season;
private GistProvider provider;
private final int season;
private final GistProvider provider;
private final String exceededLimitError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}";
@@ -36,8 +31,8 @@ public class APIFootballUpdater {
}
}
public void updateAllFixtures(String configPath) throws IOException {
HashSet<Integer> leagues = getAllLeaguesFromLeagueConfig(configPath);
public void updateAllFixtures() throws IOException {
HashSet<Integer> leagues = provider.getLeagues();
for (Integer league : leagues) {
updateFixtures(league);
}
@@ -52,8 +47,8 @@ public class APIFootballUpdater {
}
}
public void updateAllRounds(String configPath) throws IOException {
HashSet<Integer> leagues = getAllLeaguesFromLeagueConfig(configPath);
public void updateAllRounds() throws IOException {
HashSet<Integer> leagues = provider.getLeagues();
for (Integer league : leagues) {
updateRounds(league);
}
@@ -64,37 +59,6 @@ public class APIFootballUpdater {
this.provider.updateGist(id, filename, content);
}
public static HashSet<Integer> getAllLeaguesFromLeagueConfig(String config) {
HashSet<Integer> leagues = new HashSet<>();
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(config);
JSONObject configObject = null;
try {
configObject = (JSONObject) jsonParser.parse(jsonConfig);
} catch (ParseException e) {
e.printStackTrace();
}
JSONArray matchdayConfigs = (JSONArray) configObject.get("matchdayConfig");
for (Object matchdayConfig : matchdayConfigs) {
JSONArray matchesConfig = (JSONArray) ((JSONObject) matchdayConfig).get("matchesConfig");
for (Object matchConfig : matchesConfig) {
if (((JSONObject) matchConfig).containsKey("matchesLeague")) {
int league = Integer.parseInt(((JSONObject) matchConfig).get("matchesLeague").toString());
leagues.add(league);
}
if (((JSONObject) matchConfig).containsKey("matchLeague")) {
int league = Integer.parseInt(((JSONObject) matchConfig).get("matchLeague").toString());
leagues.add(league);
}
}
}
return leagues;
}
public String getRawData(String requestUrl) {
HttpClient client = HttpClientBuilder.create().build();
@@ -106,9 +70,6 @@ public class APIFootballUpdater {
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
/* TODO */
e.printStackTrace();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
@@ -116,6 +77,7 @@ public class APIFootballUpdater {
BufferedReader rd = null;
try {
assert response != null;
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())
);

View File

@@ -21,6 +21,7 @@ import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
public class GistProvider {
@@ -156,6 +157,18 @@ public class GistProvider {
return stringToJSONArray(getFileFromGist("Team_ID_Matcher.json"));
}
public HashSet<Integer> getLeagues() {
JSONObject leaguesObject = stringToJSONObject(getFileFromGist("Ligen.json"));
JSONArray leaguesArray = (JSONArray) leaguesObject.get("Ligen");
HashSet<Integer> leagues = new HashSet<>();
for (Object leagueObject : leaguesArray) {
JSONObject leagueJSONObject = (JSONObject) leagueObject;
Integer leagueId = ((Long) leagueJSONObject.get("league_id")).intValue();
leagues.add(leagueId);
}
return leagues;
}
public String getFileFromGist(String filename) {
String fileurl = null;
for (Object o : this.gistList) {
@@ -176,6 +189,8 @@ public class GistProvider {
private String getRawData(String requestUrl) {
System.out.println("Gist request: " + requestUrl);
String auth = this.username + ":" + this.password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
@@ -229,7 +244,7 @@ public class GistProvider {
JSONArray data = null;
try {
data = (JSONArray) parser.parse(rawData);
} catch (ParseException e) {
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
@@ -242,7 +257,7 @@ public class GistProvider {
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (ParseException e) {
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}

View File

@@ -4,9 +4,6 @@ import com.google.gson.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Comparator;
public class MatchesListCreator {
private final JSONObject matches = new JSONObject();
@@ -29,14 +26,13 @@ public class MatchesListCreator {
}
private void populateMatches(JSONArray matchesAPIFootball) {
matchesAPIFootball = sortFixtures(matchesAPIFootball);
JSONArray matches = new JSONArray();
for(Object matchAPIFootballObject : matchesAPIFootball) {
JSONObject matchAPIFootball = (JSONObject) matchAPIFootballObject;
JSONObject match = new JSONObject();
String matchday = matchAPIFootball.get("round").toString();
match.put("matchday", Integer.parseInt(matchday.substring(17)));
match.put("matchday", matchday);
JSONObject teamHome = (JSONObject) matchAPIFootball.get("homeTeam");
String teamNameHome = teamHome.get("team_name").toString();
@@ -65,42 +61,6 @@ public class MatchesListCreator {
return gson.toJson(jelement);
}
public JSONArray sortFixtures(JSONArray fixturesArray) {
JSONArray sortedJsonArray = new JSONArray();
ArrayList<JSONObject> fixturesList = new ArrayList<>();
for (int i = 0; i < fixturesArray.size(); i++) {
fixturesList.add((JSONObject) fixturesArray.get(i));
}
fixturesList.sort(new Comparator<JSONObject>() {
private static final String KEY_NAME = "round";
@Override
public int compare(JSONObject a, JSONObject b) {
String matchdayNameA = (String) a.get(KEY_NAME);
Integer matchdayNumberA = getMatchdayNumberFromString(matchdayNameA);
String matchdayNameB = (String) b.get(KEY_NAME);
Integer matchdayNumberB = getMatchdayNumberFromString(matchdayNameB);
return matchdayNumberA.compareTo(matchdayNumberB);
}
});
for (int i = 0; i < fixturesArray.size(); i++) {
sortedJsonArray.add(fixturesList.get(i));
}
return sortedJsonArray;
}
private Integer getMatchdayNumberFromString(String matchdayName) {
Integer matchdayNumber = null;
if(matchdayName.startsWith("Regular Season - ")) {
matchdayNumber = Integer.parseInt(matchdayName.substring(17, matchdayName.length()));
}
return matchdayNumber;
}
public String getFilename() {
return "Spiele " + this.country + " " + this.leagueName + ".json";
}

View File

@@ -1,15 +1,17 @@
package de.jeyp91.gists;
import de.jeyp91.apifootball.APIFootballUpdater;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
public class MatchesListGistUpdater {
private HashSet<Integer> leagues;
private final String gistId;
private final GistProvider prov;
public MatchesListGistUpdater(String configFile) {
this.leagues = APIFootballUpdater.getAllLeaguesFromLeagueConfig(configFile);
public MatchesListGistUpdater() {
this.prov = GistProvider.getInstance();
this.gistId = prov.getGistID("Spiele");
this.leagues = prov.getLeagues();
}
public void updateAllLeagues() throws UnsupportedEncodingException {
@@ -23,7 +25,6 @@ public class MatchesListGistUpdater {
String filename = creator.getFilename();
String content = creator.getMatchesBeautful();
GistProvider prov = GistProvider.getInstance();
String gistId = prov.getGistID("Spiele");
prov.updateGist(gistId, filename, content);
prov.updateGist(this.gistId, filename, content);
}
}

View File

@@ -0,0 +1,31 @@
package de.jeyp91.gists;
import de.jeyp91.apifootball.APIFootballConnector;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class TeamIDMatcherTemplateCreator {
APIFootballConnector con;
JSONArray teams;
public TeamIDMatcherTemplateCreator(int season, int league) {
this.con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject apiObject = con.getTeamsForLeague(league);
JSONObject teamsObject = (JSONObject) apiObject.get("api");
this.teams = (JSONArray) teamsObject.get("teams");
}
public String getTeamsTemplate() {
String teamsTemplate = "";
for (Object team : this.teams) {
JSONObject teamObject = (JSONObject) team;
teamsTemplate += ",\n";
teamsTemplate += " {\n";
teamsTemplate += " \"teamname\": \"" + teamObject.get("name") + "\",\n";
teamsTemplate += " \"tippligaID\": " + ",\n";
teamsTemplate += " \"apiFootballID\": " + teamObject.get("team_id") + "\n";
teamsTemplate += " }";
}
return teamsTemplate;
}
}

View File

@@ -94,29 +94,13 @@ public class TLWMatch extends BaseMatch{
}
}
public TLWMatch(OpenLigaDBMatch oldbmatch, int season, int league, int matchday, int matchNo) {
this.season = season;
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdGuest());
this.goalsHome = oldbmatch.getGoalsHome();
this.goalsGuest = oldbmatch.getGoalsGuest();
this.matchDatetime = oldbmatch.getMatchDateTime().replace("T", " ");
this.groupId = "";
this.formulaHome = "";
this.formulaGuest = "";
this.status = 0;
}
public TLWMatch(APIFootballMatch apiFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch) {
this.season = season;
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = this.getTeamIdHomeFromApiFootballMatch(apiFootballMatch);
this.teamIdGuest = this.getTeamIdGuestFromApiFootballMatch(apiFootballMatch);
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME);
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST);
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
@@ -279,30 +263,12 @@ public class TLWMatch extends BaseMatch{
}
public void updateMatch(APIFootballMatch apiFootballMatch) {
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME);
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST);
this.teamNameHome = apiFootballMatch.getTeamNameHome();
this.teamNameGuest = apiFootballMatch.getTeamNameGuest();
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
}
private Integer getTeamIdHomeFromApiFootballMatch(APIFootballMatch match) {
Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdHome());
if(id == null) {
logger.error("Could not find id " + match.getTeamIdHome() + " for " + match.getTeamNameHome());
StatusHolder.setError();
}
return id;
}
private Integer getTeamIdGuestFromApiFootballMatch(APIFootballMatch match) {
Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdGuest());
if(id == null) {
logger.error("Could not find id " + match.getTeamIdGuest() + " for " + match.getTeamNameGuest());
StatusHolder.setError();
}
return id;
}
}

View File

@@ -208,8 +208,12 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
int foundMatches = 0;
TLWMatch matchingMatch = null;
for(TLWMatch match : tlwMatches) {
int apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
int apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
Integer apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME);
Integer apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST);
if(apiTeamIdHome == null
|| apiTeamIdGuest == null) {
throw new NullPointerException();
}
if(
apiTeamIdHome == match.getTeamIdHome()
&& apiTeamIdGuest == match.getTeamIdGuest()

View File

@@ -1,4 +1,8 @@
package de.jeyp91;
import de.jeyp91.apifootball.APIFootballMatch;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@@ -18,14 +22,39 @@ public class TeamIDMatcherTest {
}
@Test
public void getTippligaIdFromOpenLigaDbIdTest() {
public void getTippligaIdFromOpenLigaDbIdTest() throws ParseException {
int tlwId;
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(80);
String jsonMatch = "{" +
"\"fixture_id\":1," +
"\"league_id\":1240," +
"\"league\":{" +
"\"name\":\"Testliga\"" +
"}," +
"\"event_date\":\"2020-01-01T00:00:00+02:00\"" +
"\"round\":\"Bayern - 1\"," +
"\"statusShort\":\"FT\"" +
"\"homeTeam\":{" +
"\"team_id\":9330," +
"\"team_name\":\"TestHome\"" +
"}," +
"\"awayTeam\":{" +
"\"team_id\":9331," +
"\"team_name\":\"TestGuest\"" +
"}" +
"}";
JSONObject matchObject;
JSONParser jsonParser = new JSONParser();
matchObject = (JSONObject) jsonParser.parse(jsonMatch);
APIFootballMatch match = new APIFootballMatch(matchObject, 2021);
;
match.teamIdHome = 80;
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(match, TeamIDMatcher.HOME);
assertEquals(505, tlwId);
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(91);
match.teamIdHome = 91;
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(match, TeamIDMatcher.HOME);
assertEquals(160, tlwId);
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(12754);
match.teamIdHome = 12754;
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(match, TeamIDMatcher.HOME);
assertEquals(182, tlwId);
}
}

View File

@@ -9,20 +9,18 @@ public class APIFootballUpdaterTest {
@Test
public void updateFixturesTest() throws IOException {
APIFootballUpdater updater = new APIFootballUpdater(2021);
// updater.updateFixtures(2743);
// updater.updateFixtures(2692);
}
@Test
public void updateAllFixturesTest() throws IOException {
APIFootballUpdater updater = new APIFootballUpdater(2021);
// updater.updateAllFixtures("Tippliga.json");
// updater.updateAllFixtures("WTL_Pokal.json");
// updater.updateAllFixtures();
}
@Test
public void updateAllRoundsTest() throws IOException {
APIFootballUpdater updater = new APIFootballUpdater(2021);
// updater.updateAllRounds("Tippliga.json");
// updater.updateAllRounds("WTL_Pokal.json");
// updater.updateAllRounds();
}
}

View File

@@ -8,7 +8,7 @@ public class MatchesListGistUpdaterTest {
@Test
public void updateAllLeaguesTest() throws UnsupportedEncodingException {
MatchesListGistUpdater updater = new MatchesListGistUpdater("Tippliga.json");
// updater.updateAllLeagues();
MatchesListGistUpdater updater = new MatchesListGistUpdater();
updater.updateAllLeagues();
}
}

View File

@@ -0,0 +1,13 @@
package de.jeyp91.gists;
import org.junit.Test;
public class TeamIDMatcherTemplateCreatorTest {
@Test
public void getTeamsTemplate() {
TeamIDMatcherTemplateCreator creator = new TeamIDMatcherTemplateCreator(0, 2790);
String template = creator.getTeamsTemplate();
System.out.println(template);
}
}

Binary file not shown.