/[projects]/smsdaemon/plugins/TrainInfo.cpp
ViewVC logotype

Contents of /smsdaemon/plugins/TrainInfo.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 671 - (show annotations) (download)
Tue Apr 27 11:06:17 2010 UTC (14 years ago) by torben
File size: 4765 byte(s)
small error fix
1
2 #include "TrainInfo.h"
3
4 //#include "string_nocase.h"
5 //#include "Util.h"
6 //#include "Exceptions.h"
7 #include "Logger.h"
8
9 #include "HttpClient.h"
10
11 #include <vector>
12 #include <sstream>
13 #include <stdexcept>
14
15 #include <expat.h>
16
17
18
19 using namespace std;
20
21 vector<TrainInfo> traininfoList;
22 TrainInfo tmpTrainInfo;
23 ostringstream buffer;
24
25 string ConvertUpdateInfo(char ch)
26 {
27 string result;
28 switch ( ch)
29 {
30 case '1':
31 result = "< 3 min";
32 break;
33 case '2':
34 result = "3 - 10 min";
35 break;
36 case '3':
37 result = "> 10 min";
38 break;
39 case '4':
40 result = "No info";
41 break;
42 default:
43 result = "Unknown";
44 }
45 return result;
46 }
47
48
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 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
80
81 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
89
90 vector<TrainInfo> GetTrainInfo(int stationID)
91 {
92 traininfoList.clear();
93
94 ostringstream url;
95 url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID;
96
97
98 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)
131 {
132 vector<TrainInfo> result;
133
134 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" );
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, "\"", "'");
154
155 string_nocase html = raw_doc.c_str();
156
157 unsigned int pos =0;
158
159 unsigned int ankomstpos = html.find("id='ankomsttabel'");
160
161 while (1)
162 {
163 //start with time
164 pos = html.find("<td class='tid'>", pos);
165
166 if (pos == string::npos)
167 break;
168
169 if (pos > ankomstpos)
170 break;
171
172 TrainInfo info;
173
174 pos = html.find("<span", pos);
175
176 int start = pos;
177 int stop = html.find("</td>", pos);
178
179 info.time = html.substr(start, stop-start).c_str();
180
181 //update info
182 pos = html.find("<td class='opdater'>", pos);
183 pos += 54;
184 char update = html.at(pos);
185 info.update = ConvertUpdateInfo(update);
186
187 //type
188 pos = html.find( "<td class='togtype'>", pos);
189 start = pos + 20;
190 stop = html.find("</td>", pos);
191 info.type = html.substr(start, stop-start).c_str();
192
193 //destination
194 pos = html.find( "<td class='til'>", pos);
195 start = pos + 16;
196 stop = html.find( "</td>", pos);
197 info.destination = html.substr(start, stop-start).c_str();
198
199 //origin
200 pos = html.find( "<td class='visikke'>", pos);
201 start = pos + 20;
202 stop = html.find( "</td>", pos);
203 info.origin = html.substr(start, stop-start).c_str();
204
205 //Current location
206 pos++; //since origin and current use the same search pattern, we must increase the position
207 //so we don't get origin twice.
208
209 pos = html.find( "<td class='visikke'>", pos);
210 start = pos + 20;
211 stop = html.find( "</td>", pos);
212 info.current = html.substr(start, stop-start).c_str();
213
214 //status
215 pos = html.find( "<td class='status'>", pos);
216 start = pos + 19;
217 stop = html.find( "</td>", pos);
218 info.status = html.substr(start, stop-start).c_str();
219
220 //note
221 pos = html.find( "<td class='bem'>", pos);
222 start = pos + 16;
223 stop = html.find( "</td>", pos);
224 info.note = html.substr(start, stop-start).c_str();
225
226 CleanTrainInfo(info);
227
228 result.push_back(info);
229 }
230
231 return result;
232 }
233 */

  ViewVC Help
Powered by ViewVC 1.1.20