/
parser_translink.cpp
343 lines (286 loc) · 10.7 KB
/
parser_translink.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
This file is a part of Fahrplan for maemo 2009-2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "parser_translink.h"
QString encodeStringAsFormData(const QString &array)
{
static const char hexDigits[17] = "0123456789ABCDEF";
// Same safe characters as Netscape for compatibility.
static const char safeCharacters[] = "!\'()*-._~";
QString buffer;
// http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
unsigned length = array.length();
for (unsigned i = 0; i < length; ++i) {
const unsigned char c = array.at(i).toAscii();
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || strchr(safeCharacters, c))
buffer += c;
else if (c == ' ')
buffer += '+';
else if (c == '\n' || (c == '\r' && (i + 1 >= length || array.at(i + 1).toAscii() != '\n')))
buffer += "%0D%0A";
else if (c != '\r') {
buffer += '%';
buffer += hexDigits[c >> 4];
buffer += hexDigits[c & 0xF];
}
}
return buffer;
}
void parserTranslink::tsslErrors(const QList<QSslError> & errors)
{
qDebug() << "SSL errors found";
foreach( const QSslError &error, errors )
{
qDebug() << "SSL Error: " << error.errorString();
}
}
parserTranslink::parserTranslink(QObject *parent)
{
Q_UNUSED(parent);
http = new QHttp(this);
QSslSocket* sslSocket = new QSslSocket(this);
sslSocket->setProtocol(QSsl::SecureProtocols);
sslSocket->setPeerVerifyName("jp.translink.com.au");
http->setSocket(sslSocket);
connect(http, SIGNAL(requestFinished(int,bool)),
this, SLOT(httpRequestFinished(int,bool)));
connect(http, SIGNAL(sslErrors(const QList<QSslError> &)),
this, SLOT(tsslErrors(const QList<QSslError> &)));
}
bool parserTranslink::supportsGps()
{
return true;
}
QStringList parserTranslink::getStationsByName(QString stationName)
{
QByteArray postData = "location=";
postData.append(encodeStringAsFormData(stationName));
qDebug() << "Translink: getStationsByName";
QUrl url("https://jp.translink.com.au/plan-your-journey/location/find-location");
http->setHost(url.host(), QHttp::ConnectionModeHttps, url.port() == -1 ? 0 : url.port());
filebuffer = new QBuffer();
if (!filebuffer->open(QIODevice::WriteOnly))
{
qDebug() << "Can't open Buffer";
}
QHttpRequestHeader header;
header.setRequest("POST", url.path());
header.setValue("Host", url.host());
header.setValue("Content-Type", "application/x-www-form-urlencoded");
header.setValue("Content-Length", QString::number(postData.length()));
currentRequestId = http->request(header, postData, filebuffer);
loop.exec();
filebuffer->close();
QJson::Parser parser;
bool ok;
QStringList result;
QVariantList list = parser.parse(filebuffer->buffer(),&ok).toList();
foreach (QVariant station, list)
{
QVariantMap map = station.toMap();
QString str;
str = map["Description"].toString();
str.append("@");
str.append(map["LocationId"].toString());
result.append(str);
}
return result;
}
QStringList parserTranslink::getTrainRestrictions()
{
QStringList result;
return result;
}
QStringList parserTranslink::getStationsByGPS(qreal latitude, qreal longitude)
{
QByteArray postData = "lat=";
postData.append(QString::number(latitude));
postData.append("&lng=");
postData.append(QString::number(longitude));
postData.append("&includePostcode=true&includeCountry=false");
qDebug() << "Translink: getStationsByGPS";
QUrl url("https://jp.translink.com.au/plan-your-journey/location/reverse-geocode-coords");
http->setHost(url.host(), QHttp::ConnectionModeHttps, url.port() == -1 ? 0 : url.port());
filebuffer = new QBuffer();
if (!filebuffer->open(QIODevice::WriteOnly))
{
qDebug() << "Can't open Buffer";
}
QHttpRequestHeader header;
header.setRequest("POST", url.path());
header.setValue("Host", url.host());
header.setValue("Content-Type", "application/x-www-form-urlencoded");
header.setValue("Content-Length", QString::number(postData.length()));
currentRequestId = http->request(header, postData, filebuffer);
loop.exec();
filebuffer->close();
QByteArray arr = filebuffer->buffer();
QStringList result;
QStringList result2;
if (arr.size() > 0)
{
arr.remove(arr.size() - 1,1);
arr.remove(0,1);
QString str;
str = arr;
str.append("@");
str.append("GP:");
str.append(QString::number(latitude));
str.append(",");
str.append(QString::number(longitude));
result.append(str);
}
return result;
}
ResultInfo parserTranslink::getJourneyData(QString destinationStation, QString arrivalStation, QString viaStation, QDate date, QTime time, int mode, int trainrestrictions)
{
Q_UNUSED(viaStation);
Q_UNUSED(trainrestrictions);
QByteArray postData = "Start=";
postData.append(encodeStringAsFormData(destinationStation.section('@',0,0)));
postData.append("&End=");
postData.append(encodeStringAsFormData(arrivalStation.section('@',0,0)));
postData.append("&startLocationId=");
postData.append(encodeStringAsFormData(destinationStation.section('@',1,1)));
postData.append("&endLocationId=");
postData.append(encodeStringAsFormData(arrivalStation.section('@',1,1)));
postData.append("&SearchDate=");
postData.append(encodeStringAsFormData(date.toString("d/MM/yyyy")));
postData.append("+12\%3A00\%3A00+AM");
postData.append("&TimeSearchMode=");
if (mode == 0)
{
postData.append("ArriveBefore");
}
else
{
postData.append("LeaveAfter");
}
postData.append("&SearchTime=");
postData.append(encodeStringAsFormData(time.toString("h:mmap")));
postData.append("&SearchMinute=");
postData.append(time.toString("m"));
postData.append("&SearchHour=");
int hour = time.hour();
if (hour == 0)
{
hour = 12;
}
if (hour > 12)
{
hour -= 12;
}
postData.append(QString::number(hour));
postData.append("&TimeMeridiem=");
postData.append(time.toString("AP"));
postData.append("&TransportModes%5B%5D=Bus&TransportModes%5B%5D=Train&TransportModes%5B%5D=Ferry&TransportModes%5B%5D=Tram&MaximumWalkingDistance=1500&WalkingSpeed=Normal&ServiceTypes%5B%5D=Regular&ServiceTypes%5B%5D=Express&ServiceTypes%5B%5D=NightLink&ServiceTypes%5B%5D=School&FareTypes%5B%5D=Standard&FareTypes%5B%5D=Prepaid&FareTypes%5B%5D=Free&journeyPlanIsValid=true");
QUrl url("https://jp.translink.com.au/plan-your-journey/journey-planner");
http->setHost(url.host(), QHttp::ConnectionModeHttps, url.port() == -1 ? 0 : url.port());
filebuffer = new QBuffer();
if (!filebuffer->open(QIODevice::WriteOnly))
{
qDebug() << "Can't open Buffer";
}
QHttpRequestHeader header;
header.setRequest("POST", url.path());
header.setValue("Host", url.host());
header.setValue("Content-Type", "application/x-www-form-urlencoded");
header.setValue("Content-Length", QString::number(postData.length()));
currentRequestId = http->request(header, postData, filebuffer);
loop.exec();
filebuffer->close();
QJson::Parser parser;
bool ok;
ResultInfo result;
QVariantMap map = parser.parse(filebuffer->buffer(),&ok).toMap();
QVariantMap options = map["traveloptions"].toMap();
QVariantMap enquiry = options["OriginalEnquiry"].toMap();
result.fromStation = enquiry["Start"].toString();
result.toStation = enquiry["End"].toString();
QString sdate = options["searchDate"].toString();
result.timeInfo = sdate.section(' ',0,0);
QVariantList journeys = options["itineraries"].toList();
foreach (QVariant j, journeys)
{
QVariantMap journey = j.toMap();
ResultItem item;
QStringList trains;
QVariantList legs = journey["legs"].toList();
int changes = 0;
foreach (QVariant l, legs)
{
QVariantMap leg = l.toMap();
if (leg["travelModeName"].toString() != "walk")
{
trains.append(leg["travelModeName"].toString());
changes++;
}
}
trains.removeDuplicates();
item.trainType = trains.join(", ");
int c = changes - 1;
if (c < 0)
{
c = 0;
}
item.changes = QString::number(c);
item.duration = journey["duration"].toString();
QJson::Serializer serializer;
bool ok;
QByteArray jsonleg = serializer.serialize(journey, &ok);
item.detailsUrl = jsonleg;
result.items.append(item);
}
//QFile file("/home/user/post.txt");
//file.open(QIODevice::WriteOnly);
//file.write(filebuffer->buffer());
//file.close();
return result;
}
ResultInfo parserTranslink::getJourneyData(QString queryUrl)
{
Q_UNUSED(queryUrl);
ResultInfo result;
return result;
}
DetailResultInfo parserTranslink::getJourneyDetailsData(QString queryUrl)
{
QJson::Parser parser;
bool ok;
DetailResultInfo result;
QVariantMap journey = parser.parse(queryUrl.toAscii(),&ok).toMap();
result.duration = journey["duration"].toString();
QVariantList legs = journey["legs"].toList();
foreach (QVariant l, legs)
{
DetailResultItem item;
QVariantMap leg = l.toMap();
QString route = "";
if (leg["travelModeName"].toString() == "walk")
{
route = "Walk ";
}
route.append(leg["routeLineLabel"].toString());
item.train = route;
item.fromStation = leg["origin"].toString();
item.toStation = leg["destination"].toString();
item.fromInfo = leg["start"].toString();
item.toInfo = leg["end"].toString();
QString duration = "Duration: ";
duration.append(leg["duration"].toString());
item.info = duration;
result.items.append(item);
}
return result;
}