Remove Gist

This commit is contained in:
2020-11-08 23:04:32 +01:00
parent 633b633e2b
commit 4119163c4c
48 changed files with 2792 additions and 668 deletions
@@ -0,0 +1,73 @@
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 JSONArray matches;
private final String country;
private final String leagueName;
public MatchesListCreator(int 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();
populateMatches(matchesAPIFootball);
}
private void populateMatches(JSONArray matchesAPIFootball) {
JSONArray matches = new JSONArray();
for(Object matchAPIFootballObject : matchesAPIFootball) {
JSONObject matchAPIFootball = (JSONObject) matchAPIFootballObject;
JSONObject match = new JSONObject();
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);
match.put("matchday", matchday);
match.put("matchtime", matchtime);
match.put("type", "SingleMatch");
match.put("matchLeague", leagueId);
match.put("matchId", fixtureId);
match.put("showTable", false);
matches.add(match);
}
this.matches = matches;
}
public JSONArray getMatches() {
return this.matches;
}
public String getMatchesBeautiful() {
JsonElement jelement = JsonParser.parseString(this.matches.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jelement);
}
public String getCountry() {
return country;
}
public String getLeagueName() {
return leagueName;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -0,0 +1,246 @@
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.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;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TippligaSQLConnector {
Connection con;
private static final Logger logger = LogManager.getLogger(TippligaSQLConnector.class);
private static TippligaSQLConnector tlwSqlCon = null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException ex) {
logger.error("Unable to load MySQL Driver");
StatusHolder.setError();
}
}
private TippligaSQLConnector() {
String jdbcUrlLocalhost = "jdbc:mysql://localhost/d0144ddb" +
"?user=root" +
"&password=" +
"&useUnicode=true" +
"&useJDBCCompliantTimezoneShift=true" +
"&useLegacyDatetimeCode=false" +
"&serverTimezone=UTC" +
"&allowMultiQueries=true";
String jdbcUrlProd = "jdbc:mysql://dd16124.kasserver.com/d0144ddb" +
"?user=d0144ddb" +
"&password=TippligaWuerzb_1" +
"&useUnicode=true" +
"&useJDBCCompliantTimezoneShift=true" +
"&useLegacyDatetimeCode=false" +
"&serverTimezone=UTC" +
"&allowMultiQueries=true";
try {
con = DriverManager.getConnection(jdbcUrlProd);
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
}
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 + ";";
ArrayList<TLWTeam> teams = new ArrayList<>();
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return teams;
}
public ArrayList<TLWTeam> getTeams(String id) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `team_id` = " + id + " ORDER BY `season` DESC, `league` ASC;";
ArrayList<TLWTeam> teams = new ArrayList<>();
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return teams;
}
private ArrayList<TLWMatch> getMatches(String queryString) {
ArrayList<TLWMatch> matches = new ArrayList<>();
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
matches.add(new TLWMatch(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return matches;
}
public ArrayList<TLWMatch> getMatches(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_matches` WHERE `season` = " + season + " AND `league` = " + league + " ORDER BY match_no ASC;";
return getMatches(queryString);
}
public ArrayList<TLWMatch> getMatches(String season, String league, String matchday) {
String queryString = "SELECT * FROM `phpbb_footb_matches` WHERE `season` = " + season + " AND `league` = " + league + " AND `matchday` = " + matchday + " ORDER BY match_no ASC;";
return getMatches(queryString);
}
public ArrayList<TLWMatchday> getMatchdays(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_matchdays` WHERE `season` = " + season + " AND `league` = " + league + ";";
ArrayList<TLWMatchday> matchdays = new ArrayList<>();
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
matchdays.add(new TLWMatchday(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return matchdays;
}
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){
Statement stmt;
ResultSet rset = null;
try {
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
} catch (SQLException e) {
logger.error(e.getStackTrace());
}
return rset;
}
public void executeUpdate (String queryString){
Statement stmt;
try {
stmt = con.createStatement();
stmt.executeUpdate(queryString);
} catch (SQLException e) {
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;
}
}