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