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