From 8e63b089bf1d3ef7a01f78f9bf000ebb8bc12e6f Mon Sep 17 00:00:00 2001 From: Radek Davidek Date: Sun, 2 Nov 2025 16:02:02 +0100 Subject: [PATCH] added log4j2, added fetching cookie --- logs/basketball-scraper.log | 4 ++ pom.xml | 16 +++++++ .../java/cz/kamma/czb/BasketballServer.java | 43 ++++++++++++++++--- src/main/resources/log4j2.xml | 17 ++++++++ 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 logs/basketball-scraper.log create mode 100644 src/main/resources/log4j2.xml diff --git a/logs/basketball-scraper.log b/logs/basketball-scraper.log new file mode 100644 index 0000000..02546a8 --- /dev/null +++ b/logs/basketball-scraper.log @@ -0,0 +1,4 @@ +2025-11-02 16:00:08.102 [main] INFO cz.kamma.czb.BasketballServer - Starting Basketball Server... +2025-11-02 16:00:23.654 [main] INFO cz.kamma.czb.BasketballServer - Starting Basketball Server... +2025-11-02 16:00:23.910 [main] INFO cz.kamma.czb.BasketballServer - Server running at http://localhost:8000/ +2025-11-02 16:00:32.440 [pool-3-thread-2] INFO cz.kamma.czb.BasketballServer - Data fetched successfully for date: 2025-11-02 diff --git a/pom.xml b/pom.xml index b8eaf1b..56cad36 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,22 @@ jackson-databind 2.17.3 + + org.projectlombok + lombok + 1.18.30 + provided + + + org.apache.logging.log4j + log4j-api + 2.22.0 + + + org.apache.logging.log4j + log4j-core + 2.22.0 + diff --git a/src/main/java/cz/kamma/czb/BasketballServer.java b/src/main/java/cz/kamma/czb/BasketballServer.java index 9f2e55e..6cf5949 100644 --- a/src/main/java/cz/kamma/czb/BasketballServer.java +++ b/src/main/java/cz/kamma/czb/BasketballServer.java @@ -20,11 +20,13 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; +import lombok.extern.log4j.Log4j2; + +@Log4j2 public class BasketballServer { public static void main(String[] args) throws Exception { - fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"); // inicialní fetch - + log.info("Starting Basketball Server..."); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.scheduleAtFixedRate(() -> fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"), 5, 5, TimeUnit.MINUTES); @@ -35,18 +37,43 @@ public class BasketballServer { server.setExecutor(Executors.newFixedThreadPool(4)); server.start(); - System.out.println("Server running at http://localhost:8000/"); + log.info("Server running at http://localhost:8000/"); } // --- fetch dat pro určité datum --- private static JsonNode fetchDataForDate(String dateParam) { try { + // First request to get cookies + log.debug("Fetching initial cookies from cz.basketball"); + URL initialUrl = new URL("https://cz.basketball"); + HttpURLConnection initialCon = (HttpURLConnection) initialUrl.openConnection(); + initialCon.setRequestMethod("GET"); + + // Get cookies from response headers + String cookies = ""; + for (int i = 1; ; i++) { + String headerName = initialCon.getHeaderFieldKey(i); + String headerValue = initialCon.getHeaderField(i); + + if (headerName == null && headerValue == null) { + break; + } + if ("Set-Cookie".equalsIgnoreCase(headerName)) { + if (!cookies.isEmpty()) { + cookies += "; "; + } + cookies += headerValue.split(";")[0]; + } + } + initialCon.disconnect(); + + // Main request with cookies String urlString = "https://cz.basketball/?do=customHomepage-getLeagues&date=" + URLEncoder.encode(dateParam, StandardCharsets.UTF_8) + "&categories="; URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); - con.setRequestProperty("Cookie", "cc_cookie={\"level\":[\"necessary\",\"analytics\",\"targeting\",\"social\"],\"revision\":0,\"data\":null,\"rfc_cookie\":false}; _ga_YVJ6WB27SJ=GS2.2.s1746868725$o1$g1$t1746868780$j0$l0$h0; _ga_QHKEFEZRL5=GS2.1.s1761514243$o7$g1$t1761514385$j60$l0$h0; _nss=1; PHPSESSID=0dmq2ps6c0dv5bhdg5ukjjl6e7; _gid=GA1.2.1121240707.1762001487; _gat_UA-12082319-2=1; _ga=GA1.2.1277704385.1746363814; _ga_JYB7G0MLMT=GS2.1.s1762001486$o30$g1$t1762001571$j60$l0$h0"); // nahraď platnou cookie + con.setRequestProperty("Cookie", cookies); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); StringBuilder content = new StringBuilder(); @@ -57,11 +84,11 @@ public class BasketballServer { ObjectMapper mapper = new ObjectMapper(); JsonNode data = mapper.readTree(content.toString()); - System.out.println("Data fetched successfully for date: " + dateParam); + log.info("Data fetched successfully for date: {}", dateParam); return data; } catch (Exception e) { - e.printStackTrace(); + log.error("Error fetching data: ", e); return null; } } @@ -70,6 +97,7 @@ public class BasketballServer { static class ApiHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { + log.debug("Handling API request: {}", exchange.getRequestURI()); String query = exchange.getRequestURI().getQuery(); String dateParam = "Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"; @@ -96,6 +124,7 @@ public class BasketballServer { @Override public void handle(HttpExchange exchange) throws IOException { + log.debug("Handling Web request: {}", exchange.getRequestURI()); try { String path = exchange.getRequestURI().getPath(); if ("/".equals(path) || path.isEmpty() || path.equals("/index.html")) { @@ -115,7 +144,7 @@ public class BasketballServer { sendError(exchange, 404, "Not found"); } } catch (Exception e) { - e.printStackTrace(); + log.error("Error handling web request: ", e); sendError(exchange, 500, e.getMessage()); } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..1fa01f6 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file