1+ package dev .wms .pwrapi .dao .parking ;
2+
3+ import java .io .IOException ;
4+ import java .time .LocalDateTime ;
5+ import java .time .format .DateTimeFormatter ;
6+ import java .util .*;
7+
8+ import com .fasterxml .jackson .databind .DeserializationFeature ;
9+ import com .fasterxml .jackson .databind .ObjectMapper ;
10+
11+ import dev .wms .pwrapi .dto .parking .ParkingWithHistory ;
12+ import dev .wms .pwrapi .dto .parking .deserialization .ParkingWithHistoryArrayElement ;
13+ import dev .wms .pwrapi .dto .parking .deserialization .ParkingWithHistoryResponse ;
14+ import dev .wms .pwrapi .utils .parking .ParkingGeneralUtils ;
15+ import org .springframework .context .annotation .Primary ;
16+ import org .springframework .stereotype .Repository ;
17+
18+ import dev .wms .pwrapi .dto .parking .deserialization .ParkingArrayElement ;
19+ import dev .wms .pwrapi .dto .parking .deserialization .ParkingResponse ;
20+ import dev .wms .pwrapi .dto .parking .Parking ;
21+ import dev .wms .pwrapi .utils .parking .exceptions .WrongResponseCode ;
22+ import okhttp3 .MediaType ;
23+ import okhttp3 .OkHttpClient ;
24+ import okhttp3 .Request ;
25+ import okhttp3 .RequestBody ;
26+ import okhttp3 .Response ;
27+
28+ @ Repository
29+ public class IParkingDAO implements ParkingDAO {
30+
31+
32+ @ Override
33+ public ArrayList <Parking > getProcessedParkingInfo () throws IOException {
34+
35+ ArrayList <Parking > result = new ArrayList <Parking >();
36+
37+ OkHttpClient client = new OkHttpClient ().newBuilder ()
38+ .build ();
39+ MediaType mediaType = MediaType .parse ("application/x-www-form-urlencoded; charset=UTF-8" );
40+ RequestBody body = RequestBody .create (mediaType , "o=get_parks&ts=1652019293233" );
41+ Request request = new Request .Builder ()
42+ .url ("https://iparking.pwr.edu.pl/modules/iparking/scripts/ipk_operations.php" )
43+ .method ("POST" , body )
44+ .addHeader ("sec-ch-ua" , "\" Not A;Brand\" ;v=\" 99\" , \" Chromium\" ;v=\" 100\" , \" Google Chrome\" ;v=\" 100\" " )
45+ .addHeader ("Accept" , "application/json, text/javascript, */*; q=0.01" )
46+ .addHeader ("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" )
47+ .addHeader ("X-Requested-With" , "XMLHttpRequest" )
48+ .addHeader ("sec-ch-ua-mobile" , "?0" )
49+ .addHeader ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" )
50+ .addHeader ("sec-ch-ua-platform" , "\" Windows\" " )
51+ .addHeader ("Sec-Fetch-Site" , "same-origin" )
52+ .addHeader ("Sec-Fetch-Mode" , "cors" )
53+ .addHeader ("Sec-Fetch-Dest" , "empty" )
54+ .addHeader ("Referer" , "https://iparking.pwr.edu.pl/" )
55+ .addHeader ("Origin" , "https://iparking.pwr.edu.pl" )
56+ .build ();
57+ Response response = client .newCall (request ).execute ();
58+
59+ ParkingResponse deserializedResponse = new ObjectMapper ().readValue (response .body ().string (), ParkingResponse .class );
60+
61+ if (deserializedResponse .getSuccess () != 0 ) throw new WrongResponseCode ();
62+
63+ DateTimeFormatter parkingFormatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
64+ for (ParkingArrayElement parking : deserializedResponse .getPlaces ()) {
65+
66+ Parking toAdd = new Parking ().builder ()
67+ .name (ParkingGeneralUtils .determineParking (parking .getParking_id ()))
68+ .lastUpdate (LocalDateTime .parse (parking .getCzas_pomiaru (), parkingFormatter ).toString ())
69+ .leftPlaces (Integer .valueOf (parking .getLiczba_miejsc ()))
70+ .trend (Integer .valueOf (parking .getTrend ()))
71+ .build ();
72+
73+ result .add (toAdd );
74+ }
75+
76+
77+ return result ;
78+ }
79+
80+ @ Override
81+ public List <ParkingWithHistory > getRawParkingData () throws IOException {
82+
83+ OkHttpClient client = new OkHttpClient ().newBuilder ()
84+ .build ();
85+ MediaType mediaType = MediaType .parse ("application/x-www-form-urlencoded; charset=UTF-8" );
86+ RequestBody body = RequestBody .create (mediaType , "o=get_parks&ts=1652019293233" );
87+ Request request = new Request .Builder ()
88+ .url ("https://iparking.pwr.edu.pl/modules/iparking/scripts/ipk_operations.php" )
89+ .method ("POST" , body )
90+ .addHeader ("sec-ch-ua" , "\" Not A;Brand\" ;v=\" 99\" , \" Chromium\" ;v=\" 100\" , \" Google Chrome\" ;v=\" 100\" " )
91+ .addHeader ("Accept" , "application/json, text/javascript, */*; q=0.01" )
92+ .addHeader ("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" )
93+ .addHeader ("X-Requested-With" , "XMLHttpRequest" )
94+ .addHeader ("sec-ch-ua-mobile" , "?0" )
95+ .addHeader ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" )
96+ .addHeader ("sec-ch-ua-platform" , "\" Windows\" " )
97+ .addHeader ("Sec-Fetch-Site" , "same-origin" )
98+ .addHeader ("Sec-Fetch-Mode" , "cors" )
99+ .addHeader ("Sec-Fetch-Dest" , "empty" )
100+ .addHeader ("Referer" , "https://iparking.pwr.edu.pl/" )
101+ .addHeader ("Origin" , "https://iparking.pwr.edu.pl" )
102+ // .addHeader("Cookie", "PHPSESSID=sgn0fqbs1vg9bjotuum1aha957")
103+ .build ();
104+ Response response = client .newCall (request ).execute ();
105+ String stringResponse = response .body ().string ();
106+ System .out .println (stringResponse );
107+ ObjectMapper mapper = new ObjectMapper ();
108+ mapper .configure (DeserializationFeature .ACCEPT_SINGLE_VALUE_AS_ARRAY , true );
109+ ParkingWithHistoryResponse parkingWithHistoryResponses = mapper .readValue (stringResponse , ParkingWithHistoryResponse .class );
110+ List <ParkingWithHistory > result = new ArrayList <>();
111+ DateTimeFormatter parkingFormatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
112+ for (ParkingWithHistoryArrayElement parking : parkingWithHistoryResponses .getPlaces ()){
113+
114+ ParkingWithHistory toAdd = ParkingWithHistory .builder ()
115+ .name (ParkingGeneralUtils .determineParking (parking .getParking_id ()))
116+ .lastUpdate (LocalDateTime .parse (parking .getCzas_pomiaru (), parkingFormatter ).toString ())
117+ .history (parseHistory (parking .getChart ().getX (), parking .getChart ().getData ()).toString ().replace ("{" ,"" ).replace ("}" , "" ))
118+ .build ();
119+ result .add (toAdd );
120+ }
121+ return result ;
122+ }
123+
124+ private Map <String , String > parseHistory (List <String > hours , List <String > state ){
125+ Map <String , String > states = new TreeMap <>();
126+ for (int i = 0 ; i < hours .size (); i ++){
127+ states .put (hours .get (i ), state .get (i ));
128+ }
129+ return states ;
130+ }
131+
132+
133+ }
0 commit comments