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

Annotation of /smsdaemon/plugins/TrainInfo.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 323 - (hide annotations) (download)
Wed Sep 16 09:08:40 2009 UTC (14 years, 8 months ago) by torben
File size: 4741 byte(s)
First attempt at making TogPlugin use my xml service 

1 torben 44
2     #include "TrainInfo.h"
3    
4     #include "string_nocase.h"
5 torben 158 #include "Util.h"
6 torben 193 #include "Exceptions.h"
7     #include "Logger.h"
8 torben 44
9 torben 193 #include "HttpClient.h"
10    
11 torben 44 #include <vector>
12     #include <sstream>
13 torben 81 #include <stdexcept>
14 torben 44
15 torben 323 #include <expat.h>
16 torben 81
17 torben 323
18    
19 torben 44 using namespace std;
20    
21 torben 323 vector<TrainInfo> traininfoList;
22     TrainInfo tmpTrainInfo;
23     ostringstream buffer;
24    
25 torben 44 string ConvertUpdateInfo(char ch)
26     {
27     string result;
28 torben 196 switch ( ch)
29 torben 44 {
30 torben 196 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 torben 44 }
45     return result;
46     }
47    
48 torben 323
49    
50    
51     void startElement(void *userData, const char *el, const char **attr)
52 torben 44 {
53 torben 323 buffer.str(""); //clear buffer
54     }
55 torben 44
56 torben 323 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 torben 44
80 torben 323
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 torben 44 }
87    
88 torben 323
89    
90     vector<TrainInfo> GetTrainInfo(int stationID)
91 torben 44 {
92 torben 323 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 done;
110     XML_Parse(parser, trainXml.c_str(), trainXml.size(), done);
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 torben 44 }
127    
128 torben 323 /*
129     //old html screen scaper bit
130 torben 44 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 torben 193 //string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" );
137 torben 196
138 torben 193 string raw_doc;
139     try
140 torben 196 {
141 torben 193 raw_doc = HttpClient::GetString(url);
142 torben 196 }
143 torben 193 catch (httpexception& e)
144     {
145     Logger::logMessage(e.what());
146     }
147 torben 81
148     if (raw_doc == "")
149     {
150     throw std::runtime_error("Connection timeout");
151     }
152    
153 torben 44 raw_doc = Util::str_replace(raw_doc, "\"", "'");
154    
155     string_nocase html = raw_doc.c_str();
156    
157 torben 64 unsigned int pos =0;
158 torben 44
159 torben 64 unsigned int ankomstpos = html.find("id='ankomsttabel'");
160    
161 torben 196 while (1)
162 torben 44 {
163     //start with time
164     pos = html.find("<td class='tid'>", pos);
165    
166     if (pos == string::npos)
167     break;
168    
169 torben 64 if (pos > ankomstpos)
170     break;
171    
172 torben 44 TrainInfo info;
173    
174 torben 218 pos = html.find("<span", pos);
175    
176     int start = pos;
177 torben 44 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 torben 196 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 torben 44 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 torben 323 */

  ViewVC Help
Powered by ViewVC 1.1.20