added log4j2, added fetching cookie
This commit is contained in:
parent
21cb991f9a
commit
8e63b089bf
4
logs/basketball-scraper.log
Normal file
4
logs/basketball-scraper.log
Normal file
@ -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
|
||||||
16
pom.xml
16
pom.xml
@ -19,6 +19,22 @@
|
|||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>2.17.3</version>
|
<version>2.17.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.30</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>2.22.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>2.22.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -20,11 +20,13 @@ import com.sun.net.httpserver.HttpExchange;
|
|||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
public class BasketballServer {
|
public class BasketballServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
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);
|
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
scheduler.scheduleAtFixedRate(() -> fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"),
|
scheduler.scheduleAtFixedRate(() -> fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"),
|
||||||
5, 5, TimeUnit.MINUTES);
|
5, 5, TimeUnit.MINUTES);
|
||||||
@ -35,18 +37,43 @@ public class BasketballServer {
|
|||||||
server.setExecutor(Executors.newFixedThreadPool(4));
|
server.setExecutor(Executors.newFixedThreadPool(4));
|
||||||
server.start();
|
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 ---
|
// --- fetch dat pro určité datum ---
|
||||||
private static JsonNode fetchDataForDate(String dateParam) {
|
private static JsonNode fetchDataForDate(String dateParam) {
|
||||||
try {
|
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="
|
String urlString = "https://cz.basketball/?do=customHomepage-getLeagues&date="
|
||||||
+ URLEncoder.encode(dateParam, StandardCharsets.UTF_8) + "&categories=";
|
+ URLEncoder.encode(dateParam, StandardCharsets.UTF_8) + "&categories=";
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
con.setRequestMethod("GET");
|
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));
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
@ -57,11 +84,11 @@ public class BasketballServer {
|
|||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
JsonNode data = mapper.readTree(content.toString());
|
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;
|
return data;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("Error fetching data: ", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +97,7 @@ public class BasketballServer {
|
|||||||
static class ApiHandler implements HttpHandler {
|
static class ApiHandler implements HttpHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange exchange) throws IOException {
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
log.debug("Handling API request: {}", exchange.getRequestURI());
|
||||||
String query = exchange.getRequestURI().getQuery();
|
String query = exchange.getRequestURI().getQuery();
|
||||||
String dateParam = "Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)";
|
String dateParam = "Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)";
|
||||||
|
|
||||||
@ -96,6 +124,7 @@ public class BasketballServer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange exchange) throws IOException {
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
log.debug("Handling Web request: {}", exchange.getRequestURI());
|
||||||
try {
|
try {
|
||||||
String path = exchange.getRequestURI().getPath();
|
String path = exchange.getRequestURI().getPath();
|
||||||
if ("/".equals(path) || path.isEmpty() || path.equals("/index.html")) {
|
if ("/".equals(path) || path.isEmpty() || path.equals("/index.html")) {
|
||||||
@ -115,7 +144,7 @@ public class BasketballServer {
|
|||||||
sendError(exchange, 404, "Not found");
|
sendError(exchange, 404, "Not found");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("Error handling web request: ", e);
|
||||||
sendError(exchange, 500, e.getMessage());
|
sendError(exchange, 500, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/main/resources/log4j2.xml
Normal file
17
src/main/resources/log4j2.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||||
|
</Console>
|
||||||
|
<File name="File" fileName="logs/basketball-scraper.log">
|
||||||
|
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||||
|
</File>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="debug">
|
||||||
|
<AppenderRef ref="Console"/>
|
||||||
|
<AppenderRef ref="File"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
||||||
Loading…
x
Reference in New Issue
Block a user