-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
259 lines (197 loc) · 9.14 KB
/
Main.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.*;
import java.net.*;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
String fromCity = null;
String toCity = null;
String date = null;
int maxPrice = 0;
//âõîäíûå ïàðàìåòðû
try{
fromCity = args[0]; //îòêóäà
toCity = args[1]; //êóäà
date = args[2]; //äàòà
}catch(Exception ex){
System.out.println("Ââåäèòå âñå îáÿçàòåëüíûå ïàðàìåòðû: îòêóäà, êóäà, äàòà!");
}
try {
if(Integer.valueOf(args[3]) > 0)
maxPrice = Integer.valueOf(args[3]);
}catch (Exception ex){
maxPrice = 999999;
}
Main main = new Main();
String codeCityFrom = main.codeCity(fromCity);
String codeCityTo = main.codeCity(toCity);
main.start(fromCity, codeCityFrom, toCity, codeCityTo, date, maxPrice);
}
private void start(String fromCity, String codeCityFrom, String toCity, String codeCityTo, String date, int maxPrice){
//ïàðàìåòðû äëÿ çàïðîñà
fromCity = fromCity.toUpperCase();
toCity = toCity.toUpperCase();
//ïåðâûé çàïðîñ, ïîëó÷àåì rid
Connection.Response res = null;
Document documentFirst = null;
String jsonStringDocumentFirst = null;
String linkFirst = "http://pass.rzd.ru/timetable/public/ru?STRUCTURE_ID=735&layer_id=5371&dir=0&tfl=3&checkSeats=0&st0=" + fromCity + "&code0=" + codeCityFrom + "&st1=" + toCity + "&code1=" + codeCityTo + "&dt0=" + date + "";
try {
res = Jsoup.connect(linkFirst).method(Connection.Method.GET).execute();
documentFirst = res.parse();
jsonStringDocumentFirst = documentFirst.body().text();
} catch (IOException e) {
e.printStackTrace();
}
//îñòàåì èç ïåðâîãî çàïðîñà RID è COOKIE
Object obj = null;
String rid = null;
String sessionId = null;
try {
obj = new JSONParser().parse(jsonStringDocumentFirst);
JSONObject ridJSON = (JSONObject) obj;
rid = String.valueOf(ridJSON.get("rid")); //óíèêàëüíûé êëþ÷ äîñòóïà REQUEST_ID
if (res != null) {
sessionId = res.cookie("JSESSIONID"); //äîñòàåì cookie
}
Thread.sleep(2000);
} catch (ParseException | InterruptedException e) {
e.printStackTrace();
}
//âòîðîé çàïðîñ, ïîëó÷àåì äàííûå
String jsonStringDocumentSecond = null;
String linkSecond = "https://pass.rzd.ru/timetable/public/ru?layer_id=5371&rid=" + rid + "";
Document documentSecond = null;
try {
documentSecond = Jsoup.connect(linkSecond).cookie("JSESSIONID", sessionId).get();
jsonStringDocumentSecond = documentSecond.body().text();
} catch (IOException e) {
e.printStackTrace();
}
//äîñòàåì èç îòâåòà âòîðîãî çàïðîñà íóæíûå äàííûå(áèëåòû, âðåìÿ îòïðàâëåíèÿ è òä)
Object objectJsonSecondDocument = null;
try {
objectJsonSecondDocument = new JSONParser().parse(jsonStringDocumentSecond);
JSONObject getObjectJson = (JSONObject) objectJsonSecondDocument;
JSONArray tp = (JSONArray) getObjectJson.get("tp");
Iterator tpIter = tp.iterator();
while (tpIter.hasNext()) {
JSONObject tpObj = (JSONObject) tpIter.next();
JSONArray list = (JSONArray) tpObj.get("list");
Iterator listIter = list.iterator();
int countTrains = 1;
while (listIter.hasNext()) {
JSONObject listObj = (JSONObject) listIter.next();
JSONArray cars = (JSONArray) listObj.get("cars");
Iterator carsIter = cars.iterator();
boolean start = false;
while (carsIter.hasNext()) {
JSONObject carsObj = (JSONObject) carsIter.next();
if (carsObj.get("typeLoc").equals("Áàãàæíîå êóïå")) continue;
if (Integer.parseInt((String)carsObj.get("tariff")) <= maxPrice) {
if (!start) {
System.out.println("ÏÎÅÇÄ " + countTrains + ":");
System.out.println("Îòïðàâëåíèå: " + listObj.get("time0") + " " + listObj.get("date0"));
System.out.println("Ïðèáûòèå: " + listObj.get("time1") + " " + listObj.get("date1"));
System.out.println(listObj.get("station0") + " - " + listObj.get("station1"));
System.out.println("Îñòàëîñü ñâîáîäíûõ ìåñò: ");
countTrains++;
start = true;
}
System.out.println(carsObj.get("typeLoc") + ": " + carsObj.get("freeSeats") + " (îò " + carsObj.get("tariff") + " ðóá.)");
}
}
if (start)
System.out.println("\n\n");
}
}
} catch (Exception e) {
System.out.println("Íåò áèëåòîâ!");
}
}
private String codeCity(String city) {
String code = null;
String cityRu = city.toUpperCase();
String cityUnicode = null;
try {
cityUnicode = URLEncoder.encode(cityRu.toUpperCase(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
URL url = null;
HttpURLConnection connection = null;
String line = null;
String jsonString = null;
StringBuilder sb = new StringBuilder();
int HttpResult = 0;
Object objectJsonCodeCityDocument = null;
try {
//ïîëó÷àåì êîäû ñòàíöèé ïî íàçâàíèþ ãîðîäà
url = new URL("http://www.rzd.ru/suggester?compactMode=y&stationNamePart=" + cityUnicode + "&lang=ru");
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
//ïðèâîäèì â ôîðìàò JSON
HttpResult = connection.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
while ((line = br.readLine()) != null) {
sb.append(line);
}
jsonString = sb.toString();
br.close();
}
//ïîëó÷àåì íóæíûé íàì êîä èç ìíîæåñòâà
objectJsonCodeCityDocument = new JSONParser().parse(jsonString);
JSONArray getObjectJsonCodeCity = (JSONArray) objectJsonCodeCityDocument;
Iterator codeCityIter = getObjectJsonCodeCity.iterator();
int count = 0;
while (codeCityIter.hasNext()) {
JSONObject cityCodeObj = (JSONObject) codeCityIter.next();
if(cityCodeObj.get("ss")!=null){
try{
if((cityCodeObj.get("n").toString().substring(0, cityRu.length()).equals(cityRu))){
code = String.valueOf(cityCodeObj.get("c"));
count++;
}
}catch (StringIndexOutOfBoundsException ex){
//ignore
}
}
}
if(count==0){
objectJsonCodeCityDocument = new JSONParser().parse(jsonString);
JSONArray newJsonArray = (JSONArray) objectJsonCodeCityDocument;
Iterator newCodeCity = newJsonArray.iterator();
while(newCodeCity.hasNext()){
JSONObject cityCodeObj = (JSONObject) newCodeCity.next();
try{
if((cityCodeObj.get("n").toString().substring(0, cityRu.length()).equals(cityRu))){
code = String.valueOf(cityCodeObj.get("c"));
break;
}
}catch (StringIndexOutOfBoundsException ex){
//ignore
}
}
}
} catch (ParseException | IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return code;
}
}