#include "TrainInfo.h" #include "string_nocase.h" #include "Util.h" #include #include #include using namespace std; string ConvertUpdateInfo(char ch) { string result; switch( ch) { case '1': result = "< 3 min"; break; case '2': result = "3 - 10 min"; break; case '3': result = "> 10 min"; break; case '4': result = "No info"; break; default: result = "Unknown"; } return result; } inline string clean(string& str) { using namespace Util; str = str_replace(str, " ", " "); return str_trim(str_characters(str_striptags(str))); } void CleanTrainInfo(TrainInfo& info) { info.time = clean( info.time ); info.type = clean( info.type ); info.destination = clean( info.destination ); info.origin = clean( info.origin ); info.current = clean( info.current ); info.status = clean( info.status ); info.note = clean( info.note ); } vector GetTrainInfo(string stationcode, string stationname) { vector result; string url = string("http://www.bane.dk/visStation.asp?W=FJRN&S=") + stationcode + "&artikelId=4275&statnavn=" + stationname; string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" ); if (raw_doc == "") { throw std::runtime_error("Connection timeout"); } raw_doc = Util::str_replace(raw_doc, "\"", "'"); string_nocase html = raw_doc.c_str(); unsigned int pos =0; unsigned int ankomstpos = html.find("id='ankomsttabel'"); while(1) { //start with time pos = html.find("", pos); if (pos == string::npos) break; if (pos > ankomstpos) break; TrainInfo info; int start = pos+16; int stop = html.find("", pos); info.time = html.substr(start, stop-start).c_str(); //update info pos = html.find("", pos); pos += 54; char update = html.at(pos); info.update = ConvertUpdateInfo(update); //type pos = html.find( "", pos); start = pos + 20; stop = html.find("", pos); info.type = html.substr(start, stop-start).c_str(); //destination pos = html.find( "", pos); start = pos + 16; stop = html.find( "", pos); info.destination = html.substr(start, stop-start).c_str(); //origin pos = html.find( "", pos); start = pos + 20; stop = html.find( "", pos); info.origin = html.substr(start, stop-start).c_str(); //Current location pos++; //since origin and current use the same search pattern, we must increase the position //so we don't get origin twice. pos = html.find( "", pos); start = pos + 20; stop = html.find( "", pos); info.current = html.substr(start, stop-start).c_str(); //status pos = html.find( "", pos); start = pos + 19; stop = html.find( "", pos); info.status = html.substr(start, stop-start).c_str(); //note pos = html.find( "", pos); start = pos + 16; stop = html.find( "", pos); info.note = html.substr(start, stop-start).c_str(); CleanTrainInfo(info); result.push_back(info); } return result; }