Improve match list creator
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package de.jeyp91;
|
||||
import de.jeyp91.apifootball.APIFootballUpdater;
|
||||
import de.jeyp91.gists.MatchesListGistUpdater;
|
||||
import de.jeyp91.tippliga.*;
|
||||
import net.sourceforge.argparse4j.ArgumentParsers;
|
||||
import net.sourceforge.argparse4j.inf.ArgumentParser;
|
||||
@@ -50,6 +51,10 @@ public class App {
|
||||
APIFootballUpdater apiFootballUpdater = new APIFootballUpdater(season);
|
||||
apiFootballUpdater.updateAllFixtures(configFile);
|
||||
break;
|
||||
case "MatchesListGistUpdater":
|
||||
MatchesListGistUpdater matchesListGistUpdater = new MatchesListGistUpdater(configFile);
|
||||
matchesListGistUpdater.updateAllLeagues();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -70,7 +75,7 @@ public class App {
|
||||
|
||||
parser.addArgument("-m", "--mode")
|
||||
.dest("mode")
|
||||
.choices("MatchdaysUpdater", "MatchesCreatorFootball", "MatchesUpdaterFootball", "TeamsUpdater", "APIFootballUpdater")
|
||||
.choices("MatchdaysUpdater", "MatchesCreatorFootball", "MatchesUpdaterFootball", "TeamsUpdater", "APIFootballUpdater", "MatchesListGistUpdater")
|
||||
.help("")
|
||||
.required(true)
|
||||
.type(String.class);
|
||||
|
||||
@@ -64,7 +64,7 @@ public class APIFootballUpdater {
|
||||
this.provider.updateGist(id, filename, content);
|
||||
}
|
||||
|
||||
private HashSet<Integer> getAllLeaguesFromLeagueConfig(String config) {
|
||||
public static HashSet<Integer> getAllLeaguesFromLeagueConfig(String config) {
|
||||
|
||||
HashSet<Integer> leagues = new HashSet<>();
|
||||
|
||||
|
||||
@@ -4,9 +4,14 @@ 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();
|
||||
private final String country;
|
||||
private final String leagueName;
|
||||
|
||||
public MatchesListCreator(int league) {
|
||||
GistProvider provider = GistProvider.getInstance();
|
||||
@@ -15,22 +20,23 @@ public class MatchesListCreator {
|
||||
JSONArray matchesAPIFootball = (JSONArray) api.get("fixtures");
|
||||
JSONObject firstMatchAPIFootball = (JSONObject) matchesAPIFootball.get(0);
|
||||
JSONObject leagueObject = (JSONObject) firstMatchAPIFootball.get("league");
|
||||
String country = leagueObject.get("country").toString();
|
||||
String leagueName = leagueObject.get("name").toString();
|
||||
matches.put("country", country);
|
||||
matches.put("leagueName", leagueName);
|
||||
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);
|
||||
}
|
||||
|
||||
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", matchday);
|
||||
match.put("matchday", Integer.parseInt(matchday.substring(17)));
|
||||
|
||||
JSONObject teamHome = (JSONObject) matchAPIFootball.get("homeTeam");
|
||||
String teamNameHome = teamHome.get("team_name").toString();
|
||||
@@ -38,6 +44,9 @@ public class MatchesListCreator {
|
||||
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("matchtime", matchtime);
|
||||
|
||||
Long fixtureId = (Long) matchAPIFootball.get("fixture_id");
|
||||
match.put("matchId", fixtureId);
|
||||
|
||||
@@ -55,4 +64,44 @@ public class MatchesListCreator {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
29
src/main/java/de/jeyp91/gists/MatchesListGistUpdater.java
Normal file
29
src/main/java/de/jeyp91/gists/MatchesListGistUpdater.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package de.jeyp91.gists;
|
||||
|
||||
import de.jeyp91.apifootball.APIFootballUpdater;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class MatchesListGistUpdater {
|
||||
private HashSet<Integer> leagues;
|
||||
|
||||
public MatchesListGistUpdater(String configFile) {
|
||||
this.leagues = APIFootballUpdater.getAllLeaguesFromLeagueConfig(configFile);
|
||||
}
|
||||
|
||||
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();
|
||||
String gistId = prov.getGistID("Spiele");
|
||||
prov.updateGist(gistId, filename, content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package de.jeyp91.gists;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class MatchesListGistUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void updateAllLeaguesTest() throws UnsupportedEncodingException {
|
||||
MatchesListGistUpdater updater = new MatchesListGistUpdater("Tippliga.json");
|
||||
updater.updateAllLeagues();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user