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

Annotation of /smsdaemon/plugins/TrainInfo.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (hide annotations) (download)
Wed Dec 17 23:43:26 2008 UTC (15 years, 5 months ago) by torben
File size: 3357 byte(s)
Util.*, HttpClient.*: 
	make a new Http client based on libcurl and deprecate Util::readUrl
	
WeatherPlugin.cpp, TrainInfo.cpp:
	Make use of HttpClient
	
UrlTriggerPlugin.*:
	Make a new plugin which can trigger an external webapp. and return the response back to the requester




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     switch( ch)
22     {
23     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     }
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    
69     string raw_doc;
70     try
71     {
72     raw_doc = HttpClient::GetString(url);
73     }
74     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 44 while(1)
93     {
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     int start = pos+16;
106     int stop = html.find("</td>", pos);
107    
108     info.time = html.substr(start, stop-start).c_str();
109    
110     //update info
111     pos = html.find("<td class='opdater'>", pos);
112     pos += 54;
113     char update = html.at(pos);
114     info.update = ConvertUpdateInfo(update);
115    
116     //type
117     pos = html.find( "<td class='togtype'>", pos);
118     start = pos + 20;
119     stop = html.find("</td>", pos);
120     info.type = html.substr(start, stop-start).c_str();
121    
122     //destination
123     pos = html.find( "<td class='til'>", pos);
124     start = pos + 16;
125     stop = html.find( "</td>", pos);
126     info.destination = html.substr(start, stop-start).c_str();
127    
128     //origin
129     pos = html.find( "<td class='visikke'>", pos);
130     start = pos + 20;
131     stop = html.find( "</td>", pos);
132     info.origin = html.substr(start, stop-start).c_str();
133    
134     //Current location
135     pos++; //since origin and current use the same search pattern, we must increase the position
136     //so we don't get origin twice.
137    
138     pos = html.find( "<td class='visikke'>", pos);
139     start = pos + 20;
140     stop = html.find( "</td>", pos);
141     info.current = html.substr(start, stop-start).c_str();
142    
143     //status
144     pos = html.find( "<td class='status'>", pos);
145     start = pos + 19;
146     stop = html.find( "</td>", pos);
147     info.status = html.substr(start, stop-start).c_str();
148    
149     //note
150     pos = html.find( "<td class='bem'>", pos);
151     start = pos + 16;
152     stop = html.find( "</td>", pos);
153     info.note = html.substr(start, stop-start).c_str();
154    
155     CleanTrainInfo(info);
156    
157     result.push_back(info);
158     }
159    
160     return result;
161     }

  ViewVC Help
Powered by ViewVC 1.1.20