--- smsdaemon/plugins/TrainInfo.cpp 2008/06/11 08:25:34 44 +++ smsdaemon/plugins/TrainInfo.cpp 2008/12/18 06:53:29 196 @@ -2,32 +2,38 @@ #include "TrainInfo.h" #include "string_nocase.h" -#include "util.h" +#include "Util.h" +#include "Exceptions.h" +#include "Logger.h" + +#include "HttpClient.h" #include #include +#include + using namespace std; string ConvertUpdateInfo(char ch) { string result; - switch( ch) + 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"; + 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; } @@ -58,14 +64,32 @@ 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" ); + //string raw_doc = Util::readUrl(url, "/tmp/sms_tog.tmp" ); + + string raw_doc; + try + { + raw_doc = HttpClient::GetString(url); + } + catch (httpexception& e) + { + Logger::logMessage(e.what()); + } + + 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 = html.find("id='ankomsttabel'"); + unsigned int pos =0; - while(1) + unsigned int ankomstpos = html.find("id='ankomsttabel'"); + + while (1) { //start with time pos = html.find("", pos); @@ -73,6 +97,9 @@ if (pos == string::npos) break; + if (pos > ankomstpos) + break; + TrainInfo info; int start = pos+16; @@ -105,9 +132,9 @@ 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++; //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);