1 |
|
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 |
unsigned int pos = html.find("id='ankomsttabel'"); |
67 |
|
68 |
while(1) |
69 |
{ |
70 |
//start with time |
71 |
pos = html.find("<td class='tid'>", pos); |
72 |
|
73 |
if (pos == string::npos) |
74 |
break; |
75 |
|
76 |
TrainInfo info; |
77 |
|
78 |
int start = pos+16; |
79 |
int stop = html.find("</td>", pos); |
80 |
|
81 |
info.time = html.substr(start, stop-start).c_str(); |
82 |
|
83 |
//update info |
84 |
pos = html.find("<td class='opdater'>", pos); |
85 |
pos += 54; |
86 |
char update = html.at(pos); |
87 |
info.update = ConvertUpdateInfo(update); |
88 |
|
89 |
//type |
90 |
pos = html.find( "<td class='togtype'>", pos); |
91 |
start = pos + 20; |
92 |
stop = html.find("</td>", pos); |
93 |
info.type = html.substr(start, stop-start).c_str(); |
94 |
|
95 |
//destination |
96 |
pos = html.find( "<td class='til'>", pos); |
97 |
start = pos + 16; |
98 |
stop = html.find( "</td>", pos); |
99 |
info.destination = html.substr(start, stop-start).c_str(); |
100 |
|
101 |
//origin |
102 |
pos = html.find( "<td class='visikke'>", pos); |
103 |
start = pos + 20; |
104 |
stop = html.find( "</td>", pos); |
105 |
info.origin = html.substr(start, stop-start).c_str(); |
106 |
|
107 |
//Current location |
108 |
pos++; //since origin and current use the same search pattern, we must increase the position |
109 |
//so we don't get origin twice. |
110 |
|
111 |
pos = html.find( "<td class='visikke'>", pos); |
112 |
start = pos + 20; |
113 |
stop = html.find( "</td>", pos); |
114 |
info.current = html.substr(start, stop-start).c_str(); |
115 |
|
116 |
//status |
117 |
pos = html.find( "<td class='status'>", pos); |
118 |
start = pos + 19; |
119 |
stop = html.find( "</td>", pos); |
120 |
info.status = html.substr(start, stop-start).c_str(); |
121 |
|
122 |
//note |
123 |
pos = html.find( "<td class='bem'>", pos); |
124 |
start = pos + 16; |
125 |
stop = html.find( "</td>", pos); |
126 |
info.note = html.substr(start, stop-start).c_str(); |
127 |
|
128 |
CleanTrainInfo(info); |
129 |
|
130 |
result.push_back(info); |
131 |
} |
132 |
|
133 |
return result; |
134 |
} |