--- smsdaemon/plugins/TrainInfo.cpp 2008/12/17 23:43:26 193 +++ smsdaemon/plugins/TrainInfo.cpp 2010/04/27 11:06:17 671 @@ -1,9 +1,9 @@ #include "TrainInfo.h" -#include "string_nocase.h" -#include "Util.h" -#include "Exceptions.h" +//#include "string_nocase.h" +//#include "Util.h" +//#include "Exceptions.h" #include "Logger.h" #include "HttpClient.h" @@ -12,52 +12,121 @@ #include #include +#include + + using namespace std; +vector traininfoList; +TrainInfo tmpTrainInfo; +ostringstream buffer; + 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; } -inline string clean(string& str) + + + +void startElement(void *userData, const char *el, const char **attr) { - using namespace Util; + buffer.str(""); //clear buffer +} - str = str_replace(str, " ", " "); +void endElement(void *userData, const char *el) +{ + string name(el); + if (name == "time") { + tmpTrainInfo.time = buffer.str(); + } else if (name == "updated") { + tmpTrainInfo.update = ConvertUpdateInfo( buffer.str().at(0) ); + } else if (name == "trainnumber") { + tmpTrainInfo.type = buffer.str(); + } else if (name == "destination") { + tmpTrainInfo.destination = buffer.str(); + } else if (name == "origin") { + tmpTrainInfo.origin = buffer.str(); + } else if (name == "location") { + tmpTrainInfo.current = buffer.str(); + } else if (name == "status") { + tmpTrainInfo.status = buffer.str(); + } else if (name == "note") { + tmpTrainInfo.note = buffer.str(); + } else if (name == "train") { + traininfoList.push_back(tmpTrainInfo); + } +} - return str_trim(str_characters(str_striptags(str))); + +void charData(void *userData, const XML_Char *s,int len) +{ + for (int i=0; i GetTrainInfo(int stationID) { - 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 ); + traininfoList.clear(); + + ostringstream url; + url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID; + + + XML_Parser parser = NULL; + + try + { + string trainXml = HttpClient::GetString( url.str() ); + + parser = XML_ParserCreate( NULL ); + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, charData); + + + int isFinal = 1; //this + XML_Parse(parser, trainXml.c_str(), trainXml.size(), isFinal); + } + catch (exception &e) + { + Logger::logMessage( e.what() ); + } + catch (... ) + { + Logger::logMessage( "unknown error in GetTrainInfo(int stationID)" ); + } + + if (parser != NULL) + XML_ParserFree(parser); + + + return traininfoList; } +/* +//old html screen scaper bit vector GetTrainInfo(string stationcode, string stationname) { vector result; @@ -65,12 +134,12 @@ 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; try - { + { raw_doc = HttpClient::GetString(url); - } + } catch (httpexception& e) { Logger::logMessage(e.what()); @@ -89,7 +158,7 @@ unsigned int ankomstpos = html.find("id='ankomsttabel'"); - while(1) + while (1) { //start with time pos = html.find("", pos); @@ -102,7 +171,9 @@ TrainInfo info; - int start = pos+16; + pos = html.find("", pos); info.time = html.substr(start, stop-start).c_str(); @@ -132,9 +203,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); @@ -159,5 +230,5 @@ return result; } - +*/