Cleanup code and prepare for AI usage
This commit is contained in:
@@ -1,45 +1,14 @@
|
||||
package de.jeyp91.googlecalendar;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
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 CalendarConfigProvider {
|
||||
|
||||
private static JSONObject googleCalendarConfig = null;
|
||||
|
||||
private static JSONObject getGoogleCalendarConfig() {
|
||||
if(googleCalendarConfig == null) {
|
||||
//JSON parser object to parse read file
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
URL url = Resources.getResource("Google_Calendar_Config.json");
|
||||
String jsonConfig = null;
|
||||
|
||||
try {
|
||||
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
//Read JSON file
|
||||
googleCalendarConfig = (JSONObject) jsonParser.parse(jsonConfig);
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
return googleCalendarConfig;
|
||||
}
|
||||
private static String GOOGLE_CALENDAR_ID = "825f79shtm9n3uknj99iuu2qho";
|
||||
|
||||
public static String getCalendarUrl() {
|
||||
JSONObject leagueConfig = (JSONObject) getGoogleCalendarConfig().get(String.valueOf(1));
|
||||
return (String) leagueConfig.get("url");
|
||||
return GOOGLE_CALENDAR_ID + "@group.calendar.google.com";
|
||||
}
|
||||
|
||||
public static String getCalendarId() {
|
||||
JSONObject leagueConfig = (JSONObject) getGoogleCalendarConfig().get(String.valueOf(1));
|
||||
return (String) leagueConfig.get("id");
|
||||
return GOOGLE_CALENDAR_ID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,33 +8,69 @@ import com.google.api.services.calendar.Calendar;
|
||||
import com.google.api.services.calendar.CalendarScopes;
|
||||
import com.google.auth.http.HttpCredentialsAdapter;
|
||||
import com.google.auth.oauth2.GoogleCredentials;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
|
||||
public class GoogleCalendarConnector {
|
||||
private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
|
||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
/**
|
||||
* Global instance of the scopes required by this quickstart.
|
||||
* If modifying these scopes, delete your previously saved tokens/ folder.
|
||||
*/
|
||||
private static final String SERVICE_CREDENTIALS_FILE_PATH = "/tippliga-4e0def9ef447.json";
|
||||
private static final String PROJECT_ID = "tippliga";
|
||||
private static final String CLIENT_MAIL = "tlw-707@tippliga.iam.gserviceaccount.com";
|
||||
private static final String CLIENT_ID = "101590566618120833710";
|
||||
private static final String TOKEN_URI = "https://oauth2.googleapis.com/token";
|
||||
|
||||
/**
|
||||
* Creates an authorized Credential object.
|
||||
* @return An authorized Credential object.
|
||||
* @throws IOException If the Google_Credentials.json file cannot be found.
|
||||
*/
|
||||
private static GoogleCredentials getGoogleCredentials() throws IOException {
|
||||
InputStream in = GoogleCalendarConnector.class.getResourceAsStream(SERVICE_CREDENTIALS_FILE_PATH);
|
||||
GoogleCredentials credentials = GoogleCredentials.fromStream(in)
|
||||
String privateKeyId = getEnv("GOOGLE_CALENDAR_TIPPLIGA_SERVICE_ACCOUNT_PRIVATE_KEY_ID");
|
||||
String privateKeyPem = getEnv("GOOGLE_CALENDAR_TIPPLIGA_SERVICE_ACCOUNT_PRIVATE_KEY");
|
||||
|
||||
GoogleCredentials credentials = ServiceAccountCredentials.newBuilder()
|
||||
.setClientEmail(CLIENT_MAIL)
|
||||
.setClientId(CLIENT_ID)
|
||||
.setPrivateKey(parsePrivateKey(privateKeyPem))
|
||||
.setPrivateKeyId(privateKeyId)
|
||||
.setProjectId(PROJECT_ID)
|
||||
.setTokenServerUri(URI.create(TOKEN_URI))
|
||||
.build()
|
||||
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
|
||||
|
||||
credentials.refreshIfExpired();
|
||||
return credentials;
|
||||
}
|
||||
|
||||
private static PrivateKey parsePrivateKey(String pemData) throws IOException {
|
||||
String normalized = pemData.replace("\\n", "\n");
|
||||
String stripped = normalized
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "")
|
||||
.replace("-----END PRIVATE KEY-----", "")
|
||||
.replaceAll("\\s", "");
|
||||
byte[] encoded = Base64.getDecoder().decode(stripped);
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
|
||||
try {
|
||||
KeyFactory kf = KeyFactory.getInstance("RSA");
|
||||
return kf.generatePrivate(keySpec);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
||||
throw new IOException("Failed to parse private key", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getEnv(String name) throws IOException {
|
||||
String value = System.getenv(name);
|
||||
if (value == null || value.isEmpty()) {
|
||||
throw new IOException("Environment variable " + name + " is not set");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static com.google.api.services.calendar.Calendar getSerivce() {
|
||||
Calendar service = null;
|
||||
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
package de.jeyp91.openligadb;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class OpenLigaDBConnector {
|
||||
|
||||
private final String OPENLIGADB_API_URL = "http://www.openligadb.de/api/";
|
||||
|
||||
public OpenLigaDBConnector() {}
|
||||
|
||||
public OpenLigaDBMatch getMatchDataOfSingleMatch(int id) {
|
||||
String url = OPENLIGADB_API_URL + "getmatchdata/" + id;
|
||||
JSONObject matchAsJson = getDataAsJSONObject(url);
|
||||
return new OpenLigaDBMatch(matchAsJson);
|
||||
}
|
||||
|
||||
public ArrayList<OpenLigaDBMatch> getMatchDataOfMatchday(int season, String league, int matchday) {
|
||||
|
||||
String url = OPENLIGADB_API_URL + "getmatchdata/" + league + "/" + (season - 1) + "/" + matchday;
|
||||
|
||||
JSONArray matches = getDataAsJSONArray(url);
|
||||
ArrayList<OpenLigaDBMatch> matchesList = new ArrayList<>();
|
||||
|
||||
for(Object match: matches) {
|
||||
matchesList.add(new OpenLigaDBMatch((JSONObject) match));
|
||||
}
|
||||
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
public ArrayList<OpenLigaDBMatch> getMatchDataOfCurrentMatchday(String league) {
|
||||
|
||||
String url = OPENLIGADB_API_URL + "getmatchdata/" + league;
|
||||
|
||||
JSONArray matches = getDataAsJSONArray(url);
|
||||
ArrayList<OpenLigaDBMatch> matchesList = new ArrayList<>();
|
||||
|
||||
for(Object match: matches) {
|
||||
matchesList.add(new OpenLigaDBMatch((JSONObject) match));
|
||||
}
|
||||
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
public OpenLigaDBMatch getMatchDataByMatchId(int id) {
|
||||
String url = OPENLIGADB_API_URL + "getmatchdata/" + id;
|
||||
JSONObject match = getDataAsJSONObject(url);
|
||||
return new OpenLigaDBMatch(match);
|
||||
}
|
||||
|
||||
private JSONArray getDataAsJSONArray(String requestUrl) {
|
||||
|
||||
String rawData = getRawData(requestUrl);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONArray data = null;
|
||||
try {
|
||||
data = (JSONArray) parser.parse(rawData);
|
||||
} catch (ParseException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private JSONObject getDataAsJSONObject(String requestUrl) {
|
||||
|
||||
String rawData = getRawData(requestUrl);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject data = null;
|
||||
try {
|
||||
data = (JSONObject) parser.parse(rawData);
|
||||
} catch (ParseException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private String getRawData(String requestUrl) {
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(requestUrl);
|
||||
|
||||
// add request header
|
||||
request.addHeader("Content-Type", "application/json");
|
||||
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = client.execute(request);
|
||||
} catch (ClientProtocolException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
BufferedReader rd = null;
|
||||
try {
|
||||
rd = new BufferedReader(
|
||||
new InputStreamReader(response.getEntity().getContent())
|
||||
);
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package de.jeyp91.openligadb;
|
||||
|
||||
import de.jeyp91.BaseMatch;
|
||||
import de.jeyp91.tippliga.TLWMatch;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import java.util.regex.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class OpenLigaDBMatch extends BaseMatch {
|
||||
|
||||
private final int RESULT_TYPE_ENDRESULT = 2;
|
||||
|
||||
private Integer season = null;
|
||||
private int matchId;
|
||||
private int leagueId;
|
||||
private boolean matchIsFinished;
|
||||
|
||||
public OpenLigaDBMatch(JSONObject json) {
|
||||
this.matchId = Integer.parseInt(json.get("MatchID").toString());
|
||||
this.season = getSeasonFromLeageName(json.get("LeagueName").toString());
|
||||
this.leagueId = Integer.parseInt(json.get("LeagueId").toString());
|
||||
this.teamIdHome = Integer.parseInt(((JSONObject) json.get("Team1")).get("TeamId").toString());
|
||||
this.teamIdGuest = Integer.parseInt(((JSONObject) json.get("Team2")).get("TeamId").toString());
|
||||
for(int i = 0; i < ((JSONArray) json.get("MatchResults")).size(); i++) {
|
||||
if(Integer.parseInt((((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("ResultTypeID")).toString()) == RESULT_TYPE_ENDRESULT) {
|
||||
this.goalsHome = Integer.parseInt(((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("PointsTeam1").toString());
|
||||
this.goalsGuest = Integer.parseInt(((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("PointsTeam2").toString());
|
||||
}
|
||||
}
|
||||
this.matchday = Integer.parseInt(((JSONObject) json.get("Group")).get("GroupOrderID").toString());
|
||||
this.matchIsFinished = (Boolean) json.get("MatchIsFinished");
|
||||
this.matchDatetime = (String) json.get("MatchDateTime");
|
||||
}
|
||||
|
||||
private int getSeasonFromLeageName(String leagueName) {
|
||||
Pattern p = Pattern.compile("\\d*/\\d*");
|
||||
Matcher m = p.matcher(leagueName);
|
||||
m.find();
|
||||
String seasonString = m.group();
|
||||
p = Pattern.compile("\\d+");
|
||||
m = p.matcher(seasonString);
|
||||
Integer season = null;
|
||||
while(m.find()) {
|
||||
season = Integer.parseInt(m.group());
|
||||
}
|
||||
return season < 100 ? season + 2000 : season;
|
||||
}
|
||||
|
||||
public boolean isSameMatch(TLWMatch compareMatch) {
|
||||
if(this.getSeason()!= compareMatch.getSeason()) {
|
||||
return false;
|
||||
}
|
||||
if(this.getMatchday() != compareMatch.getMatchday()) {
|
||||
return false;
|
||||
}
|
||||
if(this.getTeamIdHome() != compareMatch.getTeamIdHome()) {
|
||||
return false;
|
||||
}
|
||||
if(this.getTeamIdGuest() != compareMatch.getTeamIdGuest()) {
|
||||
return false;
|
||||
}
|
||||
String thisDateTime = this.getMatchDateTime().replace("T", " ");
|
||||
String tempDateTime = compareMatch.getMatchDateTime().replace("T", " ");
|
||||
if(!tempDateTime.equals(thisDateTime)) {
|
||||
return false;
|
||||
}
|
||||
if(this.goalsHome != compareMatch.getGoalsHome() ||
|
||||
this.goalsGuest != compareMatch.getGoalsGuest()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Integer getSeason() {
|
||||
return this.season;
|
||||
}
|
||||
|
||||
public Integer getMatchId() {
|
||||
return this.matchId;
|
||||
}
|
||||
|
||||
public boolean getMatchIsFinished() {
|
||||
return this.matchIsFinished;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user