diff --git a/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java b/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java index 7887239..2c79e27 100644 --- a/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java +++ b/src/main/java/de/jeyp91/tippligaforum/TippligaWebsiteConnector.java @@ -1,28 +1,22 @@ package de.jeyp91.tippligaforum; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.BasicCookieStore; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.*; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; import java.time.Duration; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class TippligaWebsiteConnector { CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL); @@ -31,6 +25,9 @@ public class TippligaWebsiteConnector { private static final Logger logger = LogManager.getLogger(TippligaWebsiteConnector.class); private final HttpClient client; + private final String username = "Julian"; + private final String password = "original"; + public TippligaWebsiteConnector() throws IOException { CookieHandler.setDefault(cm); @@ -39,53 +36,186 @@ public class TippligaWebsiteConnector { .connectTimeout(Duration.ofSeconds(10)) .build(); login(); + adminLogin(); + updateResultsAdmin(); } private void login() throws IOException { - HttpRequest firstReq = HttpRequest.newBuilder() + HttpRequest req = HttpRequest.newBuilder() .uri(URI.create("https://tippliga-wuerzburg.de/ucp.php?mode=login")) .GET().build(); - String firstReqBody = ""; + String reqBody = ""; try { - firstReqBody = client.send(firstReq, HttpResponse.BodyHandlers.ofString()).body(); + TimeUnit.SECONDS.sleep(1); + reqBody = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } - List firstReqCookieList = cookieStore.getCookies(); - Pattern pattern = Pattern.compile("^[\\s\\S]*[\\s\\S]*[\\s\\S]*[\\s\\S]*$"); - Matcher matcher = pattern.matcher(firstReqBody); + Matcher matcher = pattern.matcher(reqBody); matcher.find(); + String creationTime = matcher.group(1); String formToken = matcher.group(2); String sid = matcher.group(3); - List urlParameters = new ArrayList<>(); - urlParameters.add(new BasicNameValuePair("username", "Julian")); - urlParameters.add(new BasicNameValuePair("password", "original")); - urlParameters.add(new BasicNameValuePair("creation_time", creationTime)); - urlParameters.add(new BasicNameValuePair("form_token", formToken)); - urlParameters.add(new BasicNameValuePair("sid", sid)); - urlParameters.add(new BasicNameValuePair("login", "Anmelden")); + Map loginParameters = new HashMap<>(); + loginParameters.put("login", "Anmelden"); + loginParameters.put("redirect", "index.php"); + loginParameters.put("autologin", "on"); + loginParameters.put("viewonline", "on"); + loginParameters.put("username", username); + loginParameters.put("password", password); + loginParameters.put("creation_time", creationTime); + loginParameters.put("form_token", formToken); + loginParameters.put("sid", sid); + + String loginForm = loginParameters.entrySet() + .stream() + .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)) + .collect(Collectors.joining("&")); + + HttpRequest loginReq = HttpRequest.newBuilder() + .uri(URI.create("https://tippliga-wuerzburg.de/ucp.php?mode=login")) + .headers("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(loginForm)) + .build(); - HttpPost loginReq = new HttpPost("https://tippliga-wuerzburg.de/ucp.php?mode=login"); - try { - loginReq.setEntity(new UrlEncodedFormEntity(urlParameters)); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - String form = new String(loginReq.getEntity().getContent().readAllBytes()); String loginReqBody = ""; try { - CloseableHttpClient httpClient = HttpClients.createDefault(); - CloseableHttpResponse response = httpClient.execute(loginReq); - loginReqBody = EntityUtils.toString(response.getEntity()); - } catch (IOException e) { + TimeUnit.SECONDS.sleep(1); + loginReqBody = client.send(loginReq, HttpResponse.BodyHandlers.ofString()).body(); + } catch (IOException | InterruptedException e) { e.printStackTrace(); } List loginReqCookieList = cookieStore.getCookies(); - logger.info(loginReqBody); + } + + private void adminLogin() throws IOException { + String sid = getSidFromCookie(); + String url = "https://tippliga-wuerzburg.de/adm/index.php?sid=" + sid; + HttpRequest req = HttpRequest.newBuilder() + .uri(URI.create(url)) + .headers("referer", "https://tippliga-wuerzburg.de/index.php?sid=" + sid) + .GET().build(); + + String reqBody = ""; + try { + TimeUnit.SECONDS.sleep(1); + reqBody = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + Pattern pattern = Pattern.compile("^[\\s\\S]*[\\s\\S]*[\\s\\S]*[\\s\\S]*[\\s\\S]*$"); + Matcher matcher = pattern.matcher(reqBody); + matcher.find(); + + String creationTime = matcher.group(1); + String formToken = matcher.group(2); + sid = matcher.group(3); + String credential = matcher.group(4); + + Map loginParameters = new HashMap<>(); + loginParameters.put("login", "Anmelden"); + loginParameters.put("redirect", "./../adm/index.php?sid=" + sid); + loginParameters.put("username", username); + loginParameters.put("password_" + credential, password); + loginParameters.put("creation_time", creationTime); + loginParameters.put("form_token", formToken); + loginParameters.put("sid", sid); + loginParameters.put("credential", credential); + + String loginForm = loginParameters.entrySet() + .stream() + .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)) + .collect(Collectors.joining("&")); + + HttpRequest loginReq = HttpRequest.newBuilder() + .uri(URI.create(url)) + .headers("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(loginForm)) + .build(); + + String loginReqBody = ""; + try { + TimeUnit.SECONDS.sleep(1); + HttpResponse loginReqResponse = client.send(loginReq, HttpResponse.BodyHandlers.ofString()); // Response 302 + + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + List loginReqCookieList = cookieStore.getCookies(); + } + + private void updateResultsAdmin() { + String sid = getSidFromCookie(); + String url = "https://tippliga-wuerzburg.de/adm/index.php?i=-football-football-acp-results_module&mode=manage&sid=" + sid; + + HttpRequest req = HttpRequest.newBuilder() + .uri(URI.create(url)) + .GET().build(); + + String reqBody = ""; + try { + TimeUnit.SECONDS.sleep(1); + reqBody = client.send(req, HttpResponse.BodyHandlers.ofString()).body(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + Pattern pattern = Pattern.compile("^[\\s\\S]*[\\s\\S]*[\\s\\S]*$"); + Matcher matcher = pattern.matcher(reqBody); + matcher.find(); + + String creationTime = matcher.group(1); + String formToken = matcher.group(2); + return; + } + + private void updateResults() { + Map resultParameters = new HashMap<>(); + + resultParameters.put("s", "2023"); + resultParameters.put("l", "2"); + resultParameters.put("m", "32"); + resultParameters.put("action", "result"); + resultParameters.put("goalsh384", "4"); + resultParameters.put("oldgoalsh384", "4"); + resultParameters.put("goalsg384", "3"); + resultParameters.put("oldgoalsg384", "4"); + resultParameters.put("goalsh380", ""); + resultParameters.put("oldgoalsh380", ""); + resultParameters.put("goalsg380", ""); + resultParameters.put("oldgoalsg380", ""); + + String resultForm = resultParameters.entrySet() + .stream() + .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)) + .collect(Collectors.joining("&")); + + HttpRequest resultReq = HttpRequest.newBuilder() + .uri(URI.create("https://tippliga-wuerzburg.de/app.php/football/results?s=2023&l=2&m=32&action=result")) + .headers("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(resultForm)) + .build(); + + String resultReqBody = ""; + try { + TimeUnit.SECONDS.sleep(1); + resultReqBody = client.send(resultReq, HttpResponse.BodyHandlers.ofString()).body(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + logger.info(resultReqBody); + } + + private String getSidFromCookie() { + HttpCookie sidCookie = cookieStore.getCookies().get(2); + String sid = sidCookie.getValue(); + return sid; } }