/[projects]/smsdaemon/plugins/TrainInfo.cpp
ViewVC logotype

Diff of /smsdaemon/plugins/TrainInfo.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 322 by torben, Thu Mar 26 22:10:28 2009 UTC revision 323 by torben, Wed Sep 16 09:08:40 2009 UTC
# Line 12  Line 12 
12  #include <sstream>  #include <sstream>
13  #include <stdexcept>  #include <stdexcept>
14    
15    #include <expat.h>
16    
17    
18    
19  using namespace std;  using namespace std;
20    
21    vector<TrainInfo> traininfoList;
22    TrainInfo tmpTrainInfo;
23    ostringstream buffer;
24    
25  string ConvertUpdateInfo(char ch)  string ConvertUpdateInfo(char ch)
26  {  {
27          string result;          string result;
# Line 38  string ConvertUpdateInfo(char ch) Line 45  string ConvertUpdateInfo(char ch)
45          return result;          return result;
46  }  }
47    
48  inline string clean(string& str)  
49    
50    
51    void startElement(void *userData, const char *el, const char **attr)
52  {  {
53          using namespace Util;          buffer.str(""); //clear buffer
54    }
55    
56    void endElement(void *userData, const char *el)
57    {
58            string name(el);
59            if (name == "time") {
60                    tmpTrainInfo.time = buffer.str();
61            }  else if (name == "updated") {
62                    tmpTrainInfo.update = ConvertUpdateInfo( buffer.str().at(0) );
63            } else if (name == "trainnumber") {
64                    tmpTrainInfo.type = buffer.str();
65            } else if (name == "destination") {
66                    tmpTrainInfo.destination = buffer.str();
67            } else if (name == "origin") {
68                    tmpTrainInfo.origin = buffer.str();
69            } else if (name == "location") {
70                    tmpTrainInfo.current = buffer.str();
71            } else if (name == "status") {
72                    tmpTrainInfo.status = buffer.str();
73            } else if (name == "note") {
74                    tmpTrainInfo.note = buffer.str();
75            } else if (name == "train") {
76                    traininfoList.push_back(tmpTrainInfo);
77            }
78    }
79    
         str = str_replace(str, "&nbsp;", " ");  
80    
81          return str_trim(str_characters(str_striptags(str)));  void charData(void *userData, const XML_Char *s,int len)
82    {
83            for (int i=0; i<len; i++) {
84                    buffer << s[i] ;
85            }
86  }  }
87    
88  void CleanTrainInfo(TrainInfo& info)  
89    
90    vector<TrainInfo> GetTrainInfo(int stationID)
91  {  {
92          info.time = clean( info.time );          traininfoList.clear();
93          info.type = clean( info.type );  
94          info.destination = clean( info.destination );          ostringstream url;
95          info.origin = clean( info.origin );          url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID;
96          info.current = clean( info.current );  
97          info.status = clean( info.status );  
98          info.note = clean( info.note );          XML_Parser parser = NULL;
99    
100            try
101            {
102                    string trainXml = HttpClient::GetString( url.str() );
103    
104                    parser = XML_ParserCreate( NULL );
105                    XML_SetElementHandler(parser, startElement, endElement);
106                    XML_SetCharacterDataHandler(parser, charData);
107    
108    
109                    int done;
110                    XML_Parse(parser, trainXml.c_str(), trainXml.size(), done);
111            }
112            catch (exception &e)
113            {
114                    Logger::logMessage( e.what() );
115            }
116            catch (... )
117            {
118                    Logger::logMessage( "unknown error in GetTrainInfo(int stationID)" );
119            }
120    
121            if (parser != NULL)
122                    XML_ParserFree(parser);
123    
124    
125            return traininfoList;
126  }  }
127    
128    /*
129    //old html screen scaper bit
130  vector<TrainInfo> GetTrainInfo(string stationcode, string stationname)  vector<TrainInfo> GetTrainInfo(string stationcode, string stationname)
131  {  {
132          vector<TrainInfo> result;          vector<TrainInfo> result;
# Line 161  vector<TrainInfo> GetTrainInfo(string st Line 230  vector<TrainInfo> GetTrainInfo(string st
230    
231          return result;          return result;
232  }  }
233    */
234    

Legend:
Removed from v.322  
changed lines
  Added in v.323

  ViewVC Help
Powered by ViewVC 1.1.20