This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkingDAOImpl.java
More file actions
93 lines (77 loc) · 3.6 KB
/
ParkingDAOImpl.java
File metadata and controls
93 lines (77 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package dev.wms.pwrapi.dao.parking;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dev.wms.pwrapi.dto.parking.ParkingWithHistory;
import dev.wms.pwrapi.utils.http.HttpUtils;
import org.jetbrains.annotations.NotNull;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Repository;
import dev.wms.pwrapi.dto.parking.Parking;
import okhttp3.OkHttpClient;
@Repository
public class ParkingDAOImpl implements ParkingDAO {
public static final String PARKING_WRONSKIEGO = "Parking Wrońskiego";
public static final String C_13 = "C13";
public static final String D_20 = "D20";
public static final String GEOCENTRUM = "Geocentrum";
public static final String ARCHITEKTURA = "Architektura";
@Override
public List<Parking> getProcessedParkingInfo() throws IOException{
return parseProcessed(fetchParkingWebsite());
}
@Override
public List<ParkingWithHistory> getRawParkingData() throws IOException{
return parseWithDetails(fetchParkingWebsite());
}
private Document fetchParkingWebsite() throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
return HttpUtils.makeRequestWithClientAndGetDocument(client, "https://skd.pwr.edu.pl/");
}
private List<Parking> parseProcessed(Element page){
List<Parking> result = new ArrayList<>();
Matcher matcher = Pattern.compile("\"type\":\"put\",\"key\":\"text\",\"feat\":7,\"value\":\"\\d+").matcher(page.html());
matcher.find();
result.add(new Parking(PARKING_WRONSKIEGO, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking(C_13, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking(D_20, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking(GEOCENTRUM, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking(ARCHITEKTURA, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
return result;
}
private List<ParkingWithHistory> parseWithDetails(Element page){
List<ParkingWithHistory> result = new ArrayList<>();
Matcher matcher = Pattern.compile("(?<=\\\\\"data\\\\\":\\[)(.*?)(?=\\])").matcher(page.html());
matcher.find();
result.add(new ParkingWithHistory(PARKING_WRONSKIEGO, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory(C_13, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory(D_20, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory(GEOCENTRUM, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory(ARCHITEKTURA, getMeasurmentTime(), sanitizeArray(matcher.group())));
return result;
}
private String sanitizeArray(String array){
return "[" + array + "]";
}
@NotNull
private String getMeasurmentTime() {
return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
private int getPlacesFromResponse(Matcher matcher) {
return Integer.parseInt(matcher.group().split("value\":\"")[1]);
}
}