Remove Gist

This commit is contained in:
2020-11-08 23:04:32 +01:00
parent 5862a99d73
commit 8cfb2a325a
48 changed files with 2792 additions and 668 deletions

View File

@@ -1,7 +1,8 @@
package de.jeyp91;
import de.jeyp91.apifootball.APIFootballUpdater;
import de.jeyp91.gists.MatchesListGistUpdater;
import de.jeyp91.tippligaforum.MatchesListForumUpdater;
import de.jeyp91.tippliga.*;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
@@ -48,18 +49,18 @@ public class App {
sql = teamsUpdater.getInsertSQL();
break;
case "APIFootballUpdater":
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater(season);
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater();
apiFootballUpdater.updateAllFixtures();
break;
case "MatchesListGistUpdater":
MatchesListGistUpdater matchesListGistUpdater = new MatchesListGistUpdater();
matchesListGistUpdater.updateAllLeagues();
MatchesListForumUpdater matchesListForumUpdater = new MatchesListForumUpdater();
matchesListForumUpdater.updateAllLeagues();
break;
default:
break;
}
if(!StatusHolder.getError() && !sql.equals("")) {
TippligaSQLConnector con = new TippligaSQLConnector();
TippligaSQLConnector con = TippligaSQLConnector.getInstance();
con.executeUpdate(sql);
logger.info(beautifulInfo);
}

View File

@@ -0,0 +1,45 @@
package de.jeyp91;
import com.google.common.io.Resources;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class ResourceProvider {
private static JSONArray getJSONArrayFromResource(String path) {
URL url = Resources.getResource(path);
String jsonConfig;
JSONArray array = null;
try {
JSONParser jsonParser = new JSONParser();
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
array = (JSONArray) jsonParser.parse(jsonConfig);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return array;
}
public static JSONArray getLigenConfig() {
return getJSONArrayFromResource("Tippliga/Ligen.json");
}
public static JSONArray getTeamIDMatcherConfig() {
return getJSONArrayFromResource("Tippliga/Team_ID_Matcher_Config.json");
}
public static JSONArray getTipperMatchPairConfig() {
return getJSONArrayFromResource("Tippliga/Tipper_Match_Pair_Config.json");
}
public static JSONArray getTipperTeamConfig() {
return getJSONArrayFromResource("Tippliga/Tipper_Team_Config.json");
}
}

View File

@@ -0,0 +1,95 @@
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;
public class S3Provider {
private static final Regions AWS_DEFAULT_REGION = Regions.EU_CENTRAL_1;
private static final String BUCKET_NAME = "tlw-database-tool-api-football-data";
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());
}
}
public void writeFixturesToS3(int league, String content) {
writeToS3("fixtures/" + league + ".json", content);
}
public void writeRoundsToS3(int league, String content) {
writeToS3("rounds/" + league + ".json", 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));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (SdkClientException | IOException e) {
e.printStackTrace();
}
return builder.toString();
}
String getFixturesStringFromS3(int league) {
return getFileFromS3("fixtures/" + league + ".json");
}
public JSONObject getFixturesJSONFromS3(int league) {
String fixturesString = getFixturesStringFromS3(league);
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {
jsonObject = (JSONObject) parser.parse(fixturesString);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return jsonObject;
}
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;
}
}

View File

@@ -1,29 +1,22 @@
package de.jeyp91.apifootball;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.S3Provider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
*/
public class APIFootballConnector {
private static APIFootballConnector conn = null;
private final int season;
private final String gistId;
private final HashMap<String, JSONObject> rounds = new HashMap<>();
private final HashMap<String, JSONObject> matches = new HashMap<>();
private final HashMap<Integer, JSONObject> rounds = new HashMap<>();
private final HashMap<Integer, JSONObject> matches = new HashMap<>();
private APIFootballConnector(int season) {
this.season = season;
GistProvider prov = GistProvider.getInstance();
this.gistId = prov.getGistID("APIFootball_" + (season + 1));
}
public static APIFootballConnector getAPIFootballConnectorInstance(int season) {
@@ -62,14 +55,14 @@ public class APIFootballConnector {
return matchesOfMatchday;
}
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int leagueId) {
ArrayList<APIFootballMatch> matchesList = new ArrayList<>();
S3Provider prov = new S3Provider();
String filename = "matches_league_" + id + ".json";
if(!this.matches.containsKey(filename)) {
this.matches.put(filename, readObjectFromFile(filename));
if(!this.matches.containsKey(leagueId)) {
this.matches.put(leagueId, prov.getFixturesJSONFromS3(leagueId));
}
JSONObject matches = this.matches.get(filename);
JSONObject matches = this.matches.get(leagueId);
JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));
for(int i = 0; i < matchArray.size(); i++) {
@@ -79,52 +72,17 @@ public class APIFootballConnector {
return matchesList;
}
public JSONObject getMatchdays(int id) {
String filename = "rounds_" + id + ".json";
if(!this.rounds.containsKey(filename)) {
this.rounds.put(filename, readObjectFromFile(filename));
public JSONObject getMatchdays(int leagueId) {
S3Provider prov = new S3Provider();
if(!this.rounds.containsKey(leagueId)) {
this.rounds.put(leagueId, prov.getRoundsJSONFromS3(leagueId));
}
return this.rounds.get(filename);
}
public JSONObject readObjectFromFile(String filename) {
JSONObject object = null;
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getFileFromGist(filename);
try {
object = (JSONObject) jsonParser.parse(jsonConfig);
} catch (ParseException e) {
e.printStackTrace();
}
return object;
}
public JSONArray readArrayFromFile(String filename) {
JSONArray object = null;
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getFileFromGist(filename);
try {
object = (JSONArray) jsonParser.parse(jsonConfig);
} catch (ParseException e) {
e.printStackTrace();
}
return object;
return this.rounds.get(leagueId);
}
public JSONObject getTeamsForLeague(int league) {
String url = "https://v2.api-football.com/teams/league/" + league;
String content = new APIFootballUpdater(0).getRawData(url);
String content = new APIFootballUpdater().getRawData(url);
return stringToJSONObject(content);
}

View File

@@ -1,29 +1,18 @@
package de.jeyp91.apifootball;
import com.google.common.io.Resources;
import de.jeyp91.BaseMatch;
import de.jeyp91.gists.GistProvider;
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.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class APIFootballMatch extends BaseMatch {
private final int season;
private final int matchId;
private final int leagueId;
private String matchStatus;
private final String teamNameHome;
private final String teamNameGuest;
private Boolean showTable = null;
public APIFootballMatch(JSONObject json, int season) {
this.season = season;
this.matchId = Integer.parseInt(json.get("fixture_id").toString());
// TODO
this.leagueId = Integer.parseInt(json.get("league_id").toString());
@@ -33,8 +22,7 @@ public class APIFootballMatch extends BaseMatch {
this.teamNameGuest = ((JSONObject) json.get("awayTeam")).get("team_name").toString();
this.goalsHome = getNumberOrNull(json.get("goalsHomeTeam"));
this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam"));
this.matchday = getMatchdayFromRoundString(json.get("round").toString(), this.leagueId);
this.matchStatus = json.get("statusShort").toString();
this.matchday = getMatchdayFromRoundString(season, json.get("round").toString(), this.leagueId);
this.matchDatetime = (String) json.get("event_date");
}
@@ -42,10 +30,10 @@ public class APIFootballMatch extends BaseMatch {
return this.matchId;
}
private int getMatchdayFromRoundString(String round, int leagueId) {
public static int getMatchdayFromRoundString(int season, String round, int leagueId) {
round = round.replace(" ", "_");
Integer matchday = null;
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(this.season);
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject roundsObject = con.getMatchdays(leagueId);
JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures"));
for (int i = 0; i < roundsArray.size(); i++) {

View File

@@ -1,10 +1,12 @@
package de.jeyp91.apifootball;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
import org.apache.http.HttpResponse;
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 java.io.*;
@@ -12,51 +14,42 @@ import java.util.HashSet;
public class APIFootballUpdater {
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\"}}";
public APIFootballUpdater(int season) {
this.season = season;
this.provider = GistProvider.getInstance();
public APIFootballUpdater() {
}
public void updateFixtures(int league) throws IOException {
public void updateFixtures(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
String filename = "matches_league_" + league + ".json";
String content = getRawData(apiFootballUrl);
if(!content.equals(this.exceededLimitError)) {
writeStringToGist(filename, content);
S3Provider prov = new S3Provider();
prov.writeFixturesToS3(league, content);
}
}
public void updateAllFixtures() throws IOException {
HashSet<Integer> leagues = provider.getLeagues();
public void updateAllFixtures() {
HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) {
updateFixtures(league);
}
}
public void updateRounds(int league) throws IOException {
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
String filename = "rounds_" + league + ".json";
String content = getRawData(apiFootballUrl);
if(!content.equals(this.exceededLimitError)) {
writeStringToGist(filename, content);
}
}
public void updateAllRounds() throws IOException {
HashSet<Integer> leagues = provider.getLeagues();
public void updateAllRounds() {
HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) {
updateRounds(league);
}
}
private void writeStringToGist(String filename, String content) throws UnsupportedEncodingException {
String id = this.provider.getGistID("APIFootball_" + this.season);
this.provider.updateGist(id, filename, content);
public void updateRounds(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
String content = getRawData(apiFootballUrl);
if(!content.equals(this.exceededLimitError)) {
S3Provider prov = new S3Provider();
prov.writeRoundsToS3(league, content);
}
}
public String getRawData(String requestUrl) {
@@ -103,4 +96,15 @@ public class APIFootballUpdater {
}
return result.toString();
}
public HashSet<Integer> getLeagues() {
JSONArray leaguesArray = ResourceProvider.getLigenConfig();
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;
}
}

View File

@@ -1,278 +0,0 @@
package de.jeyp91.gists;
import com.google.api.client.util.DateTime;
import com.google.common.io.Resources;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.StringEntity;
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.BufferedReader;
import java.io.IOException;
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 {
private static GistProvider prov = null;
private final JSONArray gistList;
private String username;
private String password;
private GistProvider() {
readConfig();
this.gistList = listAllGists();
}
private void readConfig() {
JSONParser jsonParser = new JSONParser();
URL url = Resources.getResource("Gist_Config.json");
JSONObject gistConfig = null;
try {
String jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
//Read JSON file
gistConfig = (JSONObject) jsonParser.parse(jsonConfig);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
this.username = gistConfig.get("username").toString();
this.password = gistConfig.get("password").toString();
}
public static GistProvider getInstance() {
if (prov == null) {
prov = new GistProvider();
}
return prov;
}
public JSONArray listAllGists() {
String gistsRaw = getRawData("https://api.github.com/gists");
return stringToJSONArray(gistsRaw);
}
public DateTime getGistUpdatedTimestamp(String gistDescription) {
DateTime updated = null;
for (Object o : this.gistList) {
JSONObject gist = (JSONObject) o;
String desc = gist.get("description").toString();
if (gistDescription.equals(desc)) {
updated = new DateTime(gist.get("updated_at").toString());
}
}
return updated;
}
public String getGistID(String gistDescription) {
String id = null;
for (Object o : this.gistList) {
JSONObject gist = (JSONObject) o;
String desc = gist.get("description").toString();
if (gistDescription.equals(desc)) {
id = gist.get("id").toString();
}
}
return id;
}
public String updateGist(String id, String file, String content) throws UnsupportedEncodingException {
String requestUrl = "https://api.github.com/gists/" + id;
String auth = this.username + ":" + this.password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
HttpClient client = HttpClientBuilder
.create()
.build();
HttpPatch request = new HttpPatch(requestUrl);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader(HttpHeaders.ACCEPT, "application/vnd.github.v3+json");
JSONObject contentObject = new JSONObject();
contentObject.put("content", content);
JSONObject fileObject = new JSONObject();
fileObject.put(file, contentObject);
JSONObject filesObject = new JSONObject();
filesObject.put("files", fileObject);
String bodyString = filesObject.toString();
StringEntity body = new StringEntity(filesObject.toString());
body.setContentType("application/json");
body.setContentEncoding("UTF-8");
request.setEntity(body);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
result.append(line);
}
return result.toString();
}
public JSONArray getTeamIdMatcher() {
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) {
JSONObject gist = (JSONObject) o;
JSONObject files = (JSONObject)gist.get("files");
Set<String> filenames = files.keySet();
if(filenames.contains(filename)) {
fileurl = ((JSONObject)files.get(filename)).get("raw_url").toString();
}
}
String content = null;
if(fileurl != null) {
content = getRawData(fileurl);
}
return content;
}
private String getRawData(String requestUrl) {
String auth = this.username + ":" + this.password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
HttpClient client = HttpClientBuilder
.create()
.build();
HttpGet request = new HttpGet(requestUrl);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), "UTF-8")
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
result.append(line);
}
return result.toString();
}
private JSONArray stringToJSONArray(String rawData) {
JSONParser parser = new JSONParser();
JSONArray data = null;
try {
data = (JSONArray) parser.parse(rawData);
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
return data;
}
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;
}
public String getTippligaConfig(String filename) {
return getFileFromGist(filename);
}
public String getTippligaBaseConfig(String filename) {
return getFileFromGist(filename);
}
public JSONObject getAPIFootballLeagueConfig(int league) {
String filename = "matches_league_" + league + ".json";
String file = getFileFromGist(filename);
return stringToJSONObject(file);
}
}

View File

@@ -1,30 +0,0 @@
package de.jeyp91.gists;
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() {
this.prov = GistProvider.getInstance();
this.gistId = prov.getGistID("Spiele");
this.leagues = prov.getLeagues();
}
public void updateAllLeagues() throws UnsupportedEncodingException {
for(Integer league : this.leagues) {
updateLeague(league);
}
}
private void updateLeague(int league) throws UnsupportedEncodingException {
MatchesListCreator creator = new MatchesListCreator(league);
String filename = creator.getFilename();
String content = creator.getMatchesBeautful();
GistProvider prov = GistProvider.getInstance();
prov.updateGist(this.gistId, filename, content);
}
}

View File

@@ -1,8 +1,9 @@
package de.jeyp91;
package de.jeyp91.teamidmatcher;
import com.google.common.collect.HashBiMap;
import de.jeyp91.ResourceProvider;
import de.jeyp91.StatusHolder;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.gists.GistProvider;
import org.apache.logging.log4j.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -19,14 +20,15 @@ public class TeamIDMatcher {
private static void initBiMap() {
if(!init) {
ids = HashBiMap.create();
GistProvider prov = GistProvider.getInstance();
teams = prov.getTeamIdMatcher();
teams = ResourceProvider.getTeamIDMatcherConfig();
for (Object team : teams) {
int tippligaID = ((Long) ((JSONObject) team).get("tippligaID")).intValue();
int apiFootballID = ((Long) ((JSONObject) team).get("apiFootballID")).intValue();
ids.put(tippligaID, apiFootballID);
}
init = true;
}
}
@@ -60,7 +62,7 @@ public class TeamIDMatcher {
public static String getTeamNameFromTippligaId(Integer id) {
initBiMap();
String name = "";
for(Object team:teams) {
for(Object team : teams) {
int tippligaID = ((Long)((JSONObject) team).get("tippligaID")).intValue();
if(id == tippligaID) {
name = (((JSONObject) team).get("teamname")).toString();

View File

@@ -1,4 +1,4 @@
package de.jeyp91.gists;
package de.jeyp91.teamidmatcher;
import de.jeyp91.apifootball.APIFootballConnector;
import org.json.simple.JSONArray;

View File

@@ -4,10 +4,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import de.jeyp91.BaseMatch;
import de.jeyp91.StatusHolder;
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.teamidmatcher.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.openligadb.OpenLigaDBMatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@@ -1,13 +1,9 @@
package de.jeyp91.tippliga;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -27,15 +23,9 @@ public class TLWMatchdaysCreator {
TLWMatchesCreatorFootball matchesCreator = new TLWMatchesCreatorFootball(2021, 1, configPath);
this.matches = matchesCreator.getMatches();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(configPath);
TippligaConfigProvider prov = new TippligaConfigProvider(season);
this.configObject = prov.getTippligaConfig(configPath);
JSONParser jsonParser = new JSONParser();
try {
this.configObject = (JSONObject) jsonParser.parse(jsonConfig);
} catch (ParseException e) {
e.printStackTrace();
}
//Read JSON file
this.matchesPerMatchday = ((Long) this.configObject.get("matchesPerMatchday")).intValue();
}

View File

@@ -1,6 +1,7 @@
package de.jeyp91.tippliga;
import java.text.ParseException;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
@@ -14,11 +15,11 @@ public class TLWMatchdaysUpdater {
ArrayList<TLWMatchday> matchdaysUpdated;
String beautifulInfo = "";
public TLWMatchdaysUpdater(int season, int league, String configPath) throws ParseException {
public TLWMatchdaysUpdater(int season, int league, String configPath) {
this.season = season;
this.league = league;
TippligaSQLConnector conn = new TippligaSQLConnector();
TippligaSQLConnector conn = TippligaSQLConnector.getInstance();
this.matchdaysOriginal = conn.getMatchdays(String.valueOf(season), String.valueOf(league));
this.matchdaysOriginal.sort(Comparator.comparing(TLWMatchday::getMatchday));

View File

@@ -1,21 +1,13 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import de.jeyp91.apifootball.APIFootballConnector;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
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.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
@@ -35,20 +27,12 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
this.league = league;
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season - 1);
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(configFileName);
TippligaConfigProvider prov = new TippligaConfigProvider(season);
JSONObject config = prov.getTippligaConfig(configFileName);
try {
JSONParser jsonParser = new JSONParser();
//Read JSON file
JSONObject config = (JSONObject) jsonParser.parse(jsonConfig);
this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
} catch (ParseException e) {
e.printStackTrace();
}
this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
this.publicateMatchObjects();
}

View File

@@ -1,13 +1,12 @@
package de.jeyp91.tippliga;
import de.jeyp91.ResourceProvider;
import de.jeyp91.StatusHolder;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
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 org.json.simple.parser.ParseException;
import java.util.ArrayList;
@@ -28,22 +27,12 @@ public class TLWMatchesCreatorTipperLeague extends TLWMatchesCreatorBase {
this.league = league;
this.matchdays = matchdays;
try {
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String matchPairingConfigString = prov.getTippligaBaseConfig("Tipper_Match_Pair_Config.json");
this.matchPairingConfig = (JSONArray) jsonParser.parse(matchPairingConfigString);
this.matchPairingConfig = ResourceProvider.getTipperMatchPairConfig();
this.tipperTeamConfig = ResourceProvider.getTipperTeamConfig();
String tipperListString = prov.getTippligaConfig(configFileName);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = prov.getTippligaBaseConfig("Tipper_Team_Config.json");
this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);
} catch (ParseException e) {
e.printStackTrace();
}
TippligaConfigProvider prov = new TippligaConfigProvider(season);
this.tipperList = prov.getTippligaConfig(configFileName);
this.publicateMatchObjects();
}

View File

@@ -1,12 +1,11 @@
package de.jeyp91.tippliga;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.ResourceProvider;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
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 org.json.simple.parser.ParseException;
import java.util.ArrayList;
@@ -27,24 +26,13 @@ public class TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{
this.league = league;
this.matchdays = matchdays;
TippligaConfigProvider prov = new TippligaConfigProvider(season);
this.tipperList = prov.getTippligaConfig(configFileName);
GistProvider prov = GistProvider.getInstance();
this.tipperTeamConfig = ResourceProvider.getTipperTeamConfig();
this.TLWMatches = new ArrayList<>();
try {
JSONParser jsonParser = new JSONParser();
String tipperListString = prov.getTippligaConfig(configFileName);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = prov.getTippligaBaseConfig("Tipper_Team_Config.json");
this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);
} catch (ParseException e) {
e.printStackTrace();
}
this.publicateMatchObjects();
}

View File

@@ -1,19 +1,14 @@
package de.jeyp91.tippliga;
import com.google.api.client.util.DateTime;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.gists.GistProvider;
import de.jeyp91.tippligaforum.TippligaConfigProvider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
public class TLWMatchesManagerBase {
@@ -27,19 +22,11 @@ public class TLWMatchesManagerBase {
protected void initConfigParamsFromFile(String configFileName) {
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(configFileName);
try {
JSONParser jsonParser = new JSONParser();
//Read JSON file
JSONObject config = (JSONObject) jsonParser.parse(jsonConfig);
this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
} catch (ParseException e) {
e.printStackTrace();
}
TippligaConfigProvider prov = new TippligaConfigProvider(this.season);
JSONObject config = prov.getTippligaConfig(configFileName);
this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
}
public static ArrayList<TLWMatch> getMatchesStartingFromSecondDay(ArrayList<TLWMatch> matches) {

View File

@@ -2,9 +2,10 @@ package de.jeyp91.tippliga;
import de.jeyp91.App;
import de.jeyp91.StatusHolder;
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.teamidmatcher.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.apifootball.APIFootballMatchesProvider;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
@@ -35,7 +36,7 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
private void initUpdates() {
APIFootballMatchesProvider apiFootballMatchesProvider = new APIFootballMatchesProvider(this.season);
TippligaSQLConnector tippligaSQLConnector = new TippligaSQLConnector();
TippligaSQLConnector tippligaSQLConnector = TippligaSQLConnector.getInstance();
for (Object singleMatchdayConfig : this.matchdayConfig) {
int tlwMatchday = ((Long) ((JSONObject) singleMatchdayConfig).get("TLWMatchday")).intValue();
JSONArray matchesConfig = (JSONArray) ((JSONObject) singleMatchdayConfig).get("matchesConfig");

View File

@@ -1,5 +1,7 @@
package de.jeyp91.tippliga;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
@@ -8,7 +10,7 @@ public class TLWTeamsCreator {
int season;
int league;
ArrayList<TLWMatch> matches;
TippligaSQLConnector connector = new TippligaSQLConnector();
TippligaSQLConnector connector = TippligaSQLConnector.getInstance();
public TLWTeamsCreator(int season, int league, ArrayList<TLWMatch> matches) {
this.season = season;

View File

@@ -1,5 +1,7 @@
package de.jeyp91.tippliga;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import java.util.ArrayList;
import java.util.HashSet;
@@ -13,7 +15,7 @@ public class TLWTeamsUpdater {
public TLWTeamsUpdater(int season, int league, String configFileName) throws Exception {
TippligaSQLConnector conn = new TippligaSQLConnector();
TippligaSQLConnector conn = TippligaSQLConnector.getInstance();
this.season = season;
this.league = league;
@@ -47,7 +49,7 @@ public class TLWTeamsUpdater {
}
private void initMissingTeams(HashSet<Integer> missingIds) {
TippligaSQLConnector conn = new TippligaSQLConnector();
TippligaSQLConnector conn = TippligaSQLConnector.getInstance();
for (Integer id : missingIds) {
ArrayList<TLWTeam> teams = conn.getTeams(String.valueOf(id));
missingTeams.add(new TLWTeam(

View File

@@ -1,27 +1,26 @@
package de.jeyp91.gists;
package de.jeyp91.tippligaforum;
import com.google.gson.*;
import de.jeyp91.S3Provider;
import de.jeyp91.apifootball.APIFootballMatch;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class MatchesListCreator {
private final JSONObject matches = new JSONObject();
private JSONArray matches;
private final String country;
private final String leagueName;
public MatchesListCreator(int league) {
GistProvider provider = GistProvider.getInstance();
JSONObject leagueConfig = provider.getAPIFootballLeagueConfig(league);
S3Provider prov = new S3Provider();
JSONObject leagueConfig = prov.getFixturesJSONFromS3(league);
JSONObject api = (JSONObject) leagueConfig.get("api");
JSONArray matchesAPIFootball = (JSONArray) api.get("fixtures");
JSONObject firstMatchAPIFootball = (JSONObject) matchesAPIFootball.get(0);
JSONObject leagueObject = (JSONObject) firstMatchAPIFootball.get("league");
this.country = leagueObject.get("country").toString();
this.leagueName = leagueObject.get("name").toString();
matches.put("country", this.country);
matches.put("leagueName", this.leagueName);
matches.put("leagueId", league);
populateMatches(matchesAPIFootball);
}
@@ -31,37 +30,44 @@ public class MatchesListCreator {
JSONObject matchAPIFootball = (JSONObject) matchAPIFootballObject;
JSONObject match = new JSONObject();
String matchday = matchAPIFootball.get("round").toString();
match.put("matchday", matchday);
Long leagueId = (Long) matchAPIFootball.get("league_id");
String matchdayString = matchAPIFootball.get("round").toString();
int matchday = APIFootballMatch.getMatchdayFromRoundString(2021, matchdayString, leagueId.intValue());
String matchtime = matchAPIFootball.get("event_date").toString().replace("T", " ").substring(0, 16);
Long fixtureId = (Long) matchAPIFootball.get("fixture_id");
JSONObject teamHome = (JSONObject) matchAPIFootball.get("homeTeam");
String teamNameHome = teamHome.get("team_name").toString();
JSONObject teamGuest = (JSONObject) matchAPIFootball.get("awayTeam");
String teamNameGuest = teamGuest.get("team_name").toString();
match.put("match", teamNameHome + " - " + teamNameGuest);
String matchtime = matchAPIFootball.get("event_date").toString().replace("T", " ").substring(0, 16);
match.put("matchday", matchday);
match.put("matchtime", matchtime);
Long fixtureId = (Long) matchAPIFootball.get("fixture_id");
match.put("type", "SingleMatch");
match.put("matchLeague", leagueId);
match.put("matchId", fixtureId);
match.put("showTable", false);
matches.add(match);
}
this.matches.put("matches", matches);
this.matches = matches;
}
public JSONObject getMatches() {
public JSONArray getMatches() {
return this.matches;
}
public String getMatchesBeautful() {
public String getMatchesBeautiful() {
JsonElement jelement = JsonParser.parseString(this.matches.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jelement);
}
public String getFilename() {
return "Spiele " + this.country + " " + this.leagueName + ".json";
public String getCountry() {
return country;
}
public String getLeagueName() {
return leagueName;
}
}

View File

@@ -0,0 +1,43 @@
package de.jeyp91.tippligaforum;
import de.jeyp91.apifootball.APIFootballUpdater;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
public class MatchesListForumUpdater {
private HashSet<Integer> leagues;
public MatchesListForumUpdater() {
this.leagues = new APIFootballUpdater().getLeagues();
}
public void updateAllLeagues() {
for(Integer league : this.leagues) {
updateLeague(league);
}
}
public void updateLeague(int league) {
MatchesListCreator creator = new MatchesListCreator(league);
String content = creator.getMatchesBeautiful();
String contentWithCodeBBCode = "<r><CODE><s>[code]</s>" + content + "<e>[/code]</e></CODE></r>";
int postId = getPostId(creator.getCountry(), creator.getLeagueName());
TippligaSQLConnector con = TippligaSQLConnector.getInstance();
con.updatePost(postId, contentWithCodeBBCode);
}
private Integer getPostId(String country, String league) {
String query = "SELECT post_id FROM phpbb_posts WHERE post_subject = '" + country + " " + league + "';";
TippligaSQLConnector con = TippligaSQLConnector.getInstance();
ResultSet rset = con.executeQuery(query);
Integer postId = null;
try {
rset.next();
postId = Integer.parseInt(rset.getString(1));
} catch (SQLException e) {
e.printStackTrace();
}
return postId;
}
}

View File

@@ -0,0 +1,32 @@
package de.jeyp91.tippligaforum;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class TippligaConfigProvider {
TippligaSQLConnector con;
Integer seasonForumId;
public TippligaConfigProvider(int season) {
this.con = TippligaSQLConnector.getInstance();
Integer adminForumId = con.getForumId("Admin");
Integer tippligaConfigForumId = con.getForumId("Tippliga-Config", adminForumId);
this.seasonForumId = con.getForumId(String.valueOf(season), tippligaConfigForumId);
}
public JSONObject getTippligaConfig(String config) {
String post = this.con.getPost(config, this.seasonForumId);
int postLength = post.length();
post = post.substring(22, postLength - 25);
JSONObject tippligaConfig = null;
JSONParser jsonParser = new JSONParser();
try {
tippligaConfig = (JSONObject) jsonParser.parse(post);
} catch (ParseException e) {
e.printStackTrace();
}
return tippligaConfig;
}
}

View File

@@ -1,12 +1,16 @@
package de.jeyp91.tippliga;
package de.jeyp91.tippligaforum;
import de.jeyp91.StatusHolder;
import de.jeyp91.tippliga.TLWMatch;
import de.jeyp91.tippliga.TLWMatchday;
import de.jeyp91.tippliga.TLWTeam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.xml.transform.Result;
import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -19,6 +23,7 @@ public class TippligaSQLConnector {
Connection con;
private static final Logger logger = LogManager.getLogger(TippligaSQLConnector.class);
private static TippligaSQLConnector tlwSqlCon = null;
static {
try {
@@ -29,7 +34,7 @@ public class TippligaSQLConnector {
}
}
public TippligaSQLConnector() {
private TippligaSQLConnector() {
String jdbcUrlLocalhost = "jdbc:mysql://localhost/d0144ddb" +
"?user=root" +
"&password=" +
@@ -54,6 +59,13 @@ public class TippligaSQLConnector {
}
}
public static TippligaSQLConnector getInstance() {
if(tlwSqlCon == null) {
tlwSqlCon = new TippligaSQLConnector();
}
return tlwSqlCon;
}
public ArrayList<TLWTeam> getTeams(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `season` = " + season + " AND `league` = " + league + ";";
@@ -125,13 +137,46 @@ public class TippligaSQLConnector {
return matchdays;
}
public void updateMatchDateTime(String season, String league, String matchNo, String datetime) {
String queryString = "UPDATE `phpbb_footb_matches` "
+ "SET match_datetime = " + datetime
+ " WHERE `season` = " + season
+ " AND `league` = " + league
+ " AND match_no = " + matchNo + ";";
executeQuery(queryString);
public Integer getForumId(String forumName, int parentId) {
String queryString = "SELECT forum_id FROM phpbb_forums WHERE forum_name = \"" + forumName + "\" AND parent_id = " + parentId + ";";
ResultSet rset = executeQuery(queryString);
Integer id = null;
try {
while (rset.next()) {
id = Integer.parseInt(rset.getString(1));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return id;
}
public Integer getForumId(String forumName) {
String queryString = "SELECT forum_id FROM phpbb_forums WHERE forum_name = \"" + forumName + "\";";
ResultSet rset = executeQuery(queryString);
Integer id = null;
try {
while (rset.next()) {
id = Integer.parseInt(rset.getString(1));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return id;
}
public String getPost(String subject, int forum_id) {
String queryString = "SELECT post_text FROM phpbb_posts WHERE post_subject = \"" + subject + "\" AND forum_id = \"" + forum_id + "\";";
ResultSet rset = executeQuery(queryString);
String post = null;
try {
while (rset.next()) {
post = rset.getString(1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return post;
}
public ResultSet executeQuery (String queryString){
@@ -155,4 +200,47 @@ public class TippligaSQLConnector {
logger.error(e.getMessage());
}
}
public void updatePost(int postId, String postText) {
String postChecksum = getChecksum(postText);
final long postEditTime = Instant.now().getEpochSecond();;
final String postEditReason = "Update by tool.";
int postEditUser = 2;
String query = getPostUpdateQuery(postId, postText, postChecksum, postEditTime, postEditReason, postEditUser);
executeUpdate(query);
}
public String getPostUpdateQuery(int postId, String postText, String postChecksum, long postEditTime, String postEditReason, int postEditUser) {
return "UPDATE phpbb_posts SET " +
"post_text = '" + postText + "', " +
"post_checksum = '" + postChecksum + "', " +
"post_edit_time = " + postEditTime + ", " +
"post_edit_reason = '" + postEditReason + "', " +
"post_edit_user = " + postEditUser + ", " +
"post_edit_count = post_edit_count + 1 " +
"WHERE post_id = " + postId + ";";
}
public String getChecksum(String postText) {
String postTextForMd5 = replaceXmlMetacharacters(postText);
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
assert md != null;
md.update(postTextForMd5.getBytes());
byte[] digest = md.digest();
return DatatypeConverter.printHexBinary(digest).toLowerCase();
}
public String replaceXmlMetacharacters(String post) {
post = post.replace("&", "&amp;");
post = post.replace("<", "&lt;");
post = post.replace(">", "&gt;");
post = post.replace("\"", "&quot;");
post = post.replace("'", "&#039;");
return post;
}
}