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

Annotation of /smsdaemon/plugins/TrainInfo.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 218 - (hide annotations) (download)
Thu Mar 26 22:10:28 2009 UTC (15 years, 2 months ago) by torben
File size: 3359 byte(s)
Make traininfo work with the latest changes on bane.dk

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 81
16 torben 44 using namespace std;
17    
18     string ConvertUpdateInfo(char ch)
19     {
20     string result;
21 torben 196 switch ( ch)
22 torben 44 {
23 torben 196 case '1':
24     result = "< 3 min";
25     break;
26     case '2':
27     result = "3 - 10 min";
28     break;
29     case '3':
30     result = "> 10 min";
31     break;
32     case '4':
33     result = "No info";
34     break;
35     default:
36     result = "Unknown";
37 torben 44 }
38     return result;
39     }
40    
41     inline string clean(string& str)
42     {
43     using namespace Util;
44    
45     str = str_replace(str, "&nbsp;", " ");
46    
47     return str_trim(str_characters(str_striptags(str)));
48     }
49    
50     void CleanTrainInfo(TrainInfo& info)
51     {
52     info.time = clean( info.time );
53     info.type = clean( info.type );
54     info.destination = clean( info.destination );
55     info.origin = clean( info.origin );
56     info.current = clean( info.current );
57     info.status = clean( info.status );
58     info.note = clean( info.note );
59     }
60    
61     vector<TrainInfo> GetTrainInfo(string stationcode, string stationname)
62     {
63     vector<TrainInfo> result;
64    
65     string url = string("http://www.bane.dk/visStation.asp?W=FJRN&S=") + stationcode + "&artikelId=4275&statnavn=" + stationname;
66    
67 torben 193 //string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" );
68 torben 196
69 torben 193 string raw_doc;
70     try
71 torben 196 {
72 torben 193 raw_doc = HttpClient::GetString(url);
73 torben 196 }
74 torben 193 catch (httpexception& e)
75     {
76     Logger::logMessage(e.what());
77     }
78 torben 81
79     if (raw_doc == "")
80     {
81     throw std::runtime_error("Connection timeout");
82     }
83    
84 torben 44 raw_doc = Util::str_replace(raw_doc, "\"", "'");
85    
86     string_nocase html = raw_doc.c_str();
87    
88 torben 64 unsigned int pos =0;
89 torben 44
90 torben 64 unsigned int ankomstpos = html.find("id='ankomsttabel'");
91    
92 torben 196 while (1)
93 torben 44 {
94     //start with time
95     pos = html.find("<td class='tid'>", pos);
96    
97     if (pos == string::npos)
98     break;
99    
100 torben 64 if (pos > ankomstpos)
101     break;
102    
103 torben 44 TrainInfo info;
104    
105 torben 218 pos = html.find("<span", pos);
106    
107     int start = pos;
108 torben 44 int stop = html.find("</td>", pos);
109    
110     info.time = html.substr(start, stop-start).c_str();
111    
112     //update info
113     pos = html.find("<td class='opdater'>", pos);
114     pos += 54;
115     char update = html.at(pos);
116     info.update = ConvertUpdateInfo(update);
117    
118     //type
119     pos = html.find( "<td class='togtype'>", pos);
120     start = pos + 20;
121     stop = html.find("</td>", pos);
122     info.type = html.substr(start, stop-start).c_str();
123    
124     //destination
125     pos = html.find( "<td class='til'>", pos);
126     start = pos + 16;
127     stop = html.find( "</td>", pos);
128     info.destination = html.substr(start, stop-start).c_str();
129    
130     //origin
131     pos = html.find( "<td class='visikke'>", pos);
132     start = pos + 20;
133     stop = html.find( "</td>", pos);
134     info.origin = html.substr(start, stop-start).c_str();
135    
136     //Current location
137 torben 196 pos++; //since origin and current use the same search pattern, we must increase the position
138     //so we don't get origin twice.
139    
140 torben 44 pos = html.find( "<td class='visikke'>", pos);
141     start = pos + 20;
142     stop = html.find( "</td>", pos);
143     info.current = html.substr(start, stop-start).c_str();
144    
145     //status
146     pos = html.find( "<td class='status'>", pos);
147     start = pos + 19;
148     stop = html.find( "</td>", pos);
149     info.status = html.substr(start, stop-start).c_str();
150    
151     //note
152     pos = html.find( "<td class='bem'>", pos);
153     start = pos + 16;
154     stop = html.find( "</td>", pos);
155     info.note = html.substr(start, stop-start).c_str();
156    
157     CleanTrainInfo(info);
158    
159     result.push_back(info);
160     }
161    
162     return result;
163     }

  ViewVC Help
Powered by ViewVC 1.1.20