1 |
|
|
2 |
#include "TrainInfo.h" |
#include "TrainInfo.h" |
3 |
|
|
4 |
#include "string_nocase.h" |
//#include "string_nocase.h" |
5 |
#include "util.h" |
//#include "Util.h" |
6 |
|
//#include "Exceptions.h" |
7 |
|
#include "Logger.h" |
8 |
|
|
9 |
|
#include "HttpClient.h" |
10 |
|
|
11 |
#include <vector> |
#include <vector> |
12 |
#include <sstream> |
#include <sstream> |
13 |
|
#include <stdexcept> |
14 |
|
|
15 |
|
#include <expat.h> |
16 |
|
|
17 |
|
|
18 |
|
|
19 |
using namespace std; |
using namespace std; |
20 |
|
|
21 |
|
vector<TrainInfo> traininfoList; |
22 |
|
TrainInfo tmpTrainInfo; |
23 |
|
ostringstream buffer; |
24 |
|
|
25 |
string ConvertUpdateInfo(char ch) |
string ConvertUpdateInfo(char ch) |
26 |
{ |
{ |
27 |
string result; |
string result; |
28 |
switch( ch) |
switch ( ch) |
29 |
{ |
{ |
30 |
case '1': |
case '1': |
31 |
result = "< 3 min"; |
result = "< 3 min"; |
32 |
break; |
break; |
33 |
case '2': |
case '2': |
34 |
result = "3 - 10 min"; |
result = "3 - 10 min"; |
35 |
break; |
break; |
36 |
case '3': |
case '3': |
37 |
result = "> 10 min"; |
result = "> 10 min"; |
38 |
break; |
break; |
39 |
case '4': |
case '4': |
40 |
result = "No info"; |
result = "No info"; |
41 |
break; |
break; |
42 |
default: |
default: |
43 |
result = "Unknown"; |
result = "Unknown"; |
44 |
} |
} |
45 |
return result; |
return result; |
46 |
} |
} |
47 |
|
|
48 |
inline string clean(string& str) |
|
49 |
|
|
50 |
|
|
51 |
|
void startElement(void *userData, const char *el, const char **attr) |
52 |
|
{ |
53 |
|
buffer.str(""); //clear buffer |
54 |
|
} |
55 |
|
|
56 |
|
void endElement(void *userData, const char *el) |
57 |
{ |
{ |
58 |
using namespace Util; |
string name(el); |
59 |
|
if (name == "time") { |
60 |
|
tmpTrainInfo.time = buffer.str(); |
61 |
|
} else if (name == "updated") { |
62 |
|
tmpTrainInfo.update = ConvertUpdateInfo( buffer.str().at(0) ); |
63 |
|
} else if (name == "trainnumber") { |
64 |
|
tmpTrainInfo.type = buffer.str(); |
65 |
|
} else if (name == "destination") { |
66 |
|
tmpTrainInfo.destination = buffer.str(); |
67 |
|
} else if (name == "origin") { |
68 |
|
tmpTrainInfo.origin = buffer.str(); |
69 |
|
} else if (name == "location") { |
70 |
|
tmpTrainInfo.current = buffer.str(); |
71 |
|
} else if (name == "status") { |
72 |
|
tmpTrainInfo.status = buffer.str(); |
73 |
|
} else if (name == "note") { |
74 |
|
tmpTrainInfo.note = buffer.str(); |
75 |
|
} else if (name == "train") { |
76 |
|
traininfoList.push_back(tmpTrainInfo); |
77 |
|
} |
78 |
|
} |
79 |
|
|
|
str = str_replace(str, " ", " "); |
|
80 |
|
|
81 |
return str_trim(str_characters(str_striptags(str))); |
void charData(void *userData, const XML_Char *s,int len) |
82 |
|
{ |
83 |
|
for (int i=0; i<len; i++) { |
84 |
|
buffer << s[i] ; |
85 |
|
} |
86 |
} |
} |
87 |
|
|
88 |
void CleanTrainInfo(TrainInfo& info) |
|
89 |
|
|
90 |
|
vector<TrainInfo> GetTrainInfo(int stationID) |
91 |
{ |
{ |
92 |
info.time = clean( info.time ); |
traininfoList.clear(); |
93 |
info.type = clean( info.type ); |
|
94 |
info.destination = clean( info.destination ); |
ostringstream url; |
95 |
info.origin = clean( info.origin ); |
url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID; |
96 |
info.current = clean( info.current ); |
|
97 |
info.status = clean( info.status ); |
|
98 |
info.note = clean( info.note ); |
XML_Parser parser = NULL; |
99 |
|
|
100 |
|
try |
101 |
|
{ |
102 |
|
string trainXml = HttpClient::GetString( url.str() ); |
103 |
|
|
104 |
|
parser = XML_ParserCreate( NULL ); |
105 |
|
XML_SetElementHandler(parser, startElement, endElement); |
106 |
|
XML_SetCharacterDataHandler(parser, charData); |
107 |
|
|
108 |
|
|
109 |
|
int isFinal = 1; //this |
110 |
|
XML_Parse(parser, trainXml.c_str(), trainXml.size(), isFinal); |
111 |
|
} |
112 |
|
catch (exception &e) |
113 |
|
{ |
114 |
|
Logger::logMessage( e.what() ); |
115 |
|
} |
116 |
|
catch (... ) |
117 |
|
{ |
118 |
|
Logger::logMessage( "unknown error in GetTrainInfo(int stationID)" ); |
119 |
|
} |
120 |
|
|
121 |
|
if (parser != NULL) |
122 |
|
XML_ParserFree(parser); |
123 |
|
|
124 |
|
|
125 |
|
return traininfoList; |
126 |
} |
} |
127 |
|
|
128 |
|
/* |
129 |
|
//old html screen scaper bit |
130 |
vector<TrainInfo> GetTrainInfo(string stationcode, string stationname) |
vector<TrainInfo> GetTrainInfo(string stationcode, string stationname) |
131 |
{ |
{ |
132 |
vector<TrainInfo> result; |
vector<TrainInfo> result; |
133 |
|
|
134 |
string url = string("http://www.bane.dk/visStation.asp?W=FJRN&S=") + stationcode + "&artikelId=4275&statnavn=" + stationname; |
string url = string("http://www.bane.dk/visStation.asp?W=FJRN&S=") + stationcode + "&artikelId=4275&statnavn=" + stationname; |
135 |
|
|
136 |
string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" ); |
//string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" ); |
137 |
|
|
138 |
|
string raw_doc; |
139 |
|
try |
140 |
|
{ |
141 |
|
raw_doc = HttpClient::GetString(url); |
142 |
|
} |
143 |
|
catch (httpexception& e) |
144 |
|
{ |
145 |
|
Logger::logMessage(e.what()); |
146 |
|
} |
147 |
|
|
148 |
|
if (raw_doc == "") |
149 |
|
{ |
150 |
|
throw std::runtime_error("Connection timeout"); |
151 |
|
} |
152 |
|
|
153 |
raw_doc = Util::str_replace(raw_doc, "\"", "'"); |
raw_doc = Util::str_replace(raw_doc, "\"", "'"); |
154 |
|
|
155 |
string_nocase html = raw_doc.c_str(); |
string_nocase html = raw_doc.c_str(); |
156 |
|
|
157 |
unsigned int pos = html.find("id='ankomsttabel'"); |
unsigned int pos =0; |
158 |
|
|
159 |
while(1) |
unsigned int ankomstpos = html.find("id='ankomsttabel'"); |
160 |
|
|
161 |
|
while (1) |
162 |
{ |
{ |
163 |
//start with time |
//start with time |
164 |
pos = html.find("<td class='tid'>", pos); |
pos = html.find("<td class='tid'>", pos); |
166 |
if (pos == string::npos) |
if (pos == string::npos) |
167 |
break; |
break; |
168 |
|
|
169 |
|
if (pos > ankomstpos) |
170 |
|
break; |
171 |
|
|
172 |
TrainInfo info; |
TrainInfo info; |
173 |
|
|
174 |
int start = pos+16; |
pos = html.find("<span", pos); |
175 |
|
|
176 |
|
int start = pos; |
177 |
int stop = html.find("</td>", pos); |
int stop = html.find("</td>", pos); |
178 |
|
|
179 |
info.time = html.substr(start, stop-start).c_str(); |
info.time = html.substr(start, stop-start).c_str(); |
203 |
info.origin = html.substr(start, stop-start).c_str(); |
info.origin = html.substr(start, stop-start).c_str(); |
204 |
|
|
205 |
//Current location |
//Current location |
206 |
pos++; //since origin and current use the same search pattern, we must increase the position |
pos++; //since origin and current use the same search pattern, we must increase the position |
207 |
//so we don't get origin twice. |
//so we don't get origin twice. |
208 |
|
|
209 |
pos = html.find( "<td class='visikke'>", pos); |
pos = html.find( "<td class='visikke'>", pos); |
210 |
start = pos + 20; |
start = pos + 20; |
211 |
stop = html.find( "</td>", pos); |
stop = html.find( "</td>", pos); |
230 |
|
|
231 |
return result; |
return result; |
232 |
} |
} |
233 |
|
*/ |
234 |
|
|