/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

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

revision 307 by torben, Thu Sep 10 18:11:53 2009 UTC revision 386 by torben, Fri Oct 2 13:44:31 2009 UTC
# Line 6  import java.sql.Statement; Line 6  import java.sql.Statement;
6  import java.util.ArrayList;  import java.util.ArrayList;
7  import java.util.Collections;  import java.util.Collections;
8  import java.util.List;  import java.util.List;
9    import java.util.logging.Logger;
10    
11    import com.gargoylesoftware.htmlunit.ProxyConfig;
12  import com.gargoylesoftware.htmlunit.WebClient;  import com.gargoylesoftware.htmlunit.WebClient;
13  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import com.gargoylesoftware.htmlunit.html.DomNodeList;
14  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import com.gargoylesoftware.htmlunit.html.HtmlElement;
# Line 15  import com.gargoylesoftware.htmlunit.htm Line 17  import com.gargoylesoftware.htmlunit.htm
17  import dk.thoerup.traininfoservice.DBConnection;  import dk.thoerup.traininfoservice.DBConnection;
18    
19  public class DepartureFetcher {  public class DepartureFetcher {
20            
21            Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
22                                    
23          @SuppressWarnings("unchecked")  
24          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
25                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
26                                    
# Line 59  public class DepartureFetcher { Line 63  public class DepartureFetcher {
63                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
64                                    
65              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient();
66              webClient.setTimeout(1000);              webClient.setTimeout(2500);
67              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
68                                
69                            
70              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);
71                            
72              HtmlElement table = page.getElementById("afgangtabel");              HtmlElement table = page.getElementById("afgangtabel");
             DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");  
73                            
74              for (HtmlElement currentRow : tableRows) {              if (table != null) {
75                  String rowClass = currentRow.getAttribute("class");                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
76                  if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                      
77                          DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                      for (HtmlElement currentRow : tableRows) {
78                            String rowClass = currentRow.getAttribute("class");
79                          DepartureBean departure = new DepartureBean();                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
80                                                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
81                          String time = fields.get(0).asText();          
82                          departure.setTime(time);                                  DepartureBean departure = new DepartureBean();
83                                                            
84                          int updated = extractUpdated( fields.get(1) );                                  String time = fields.get(0).asText();
85                          departure.setUpdated(updated);                                  if (time.equals(""))
86                                                                    time = "0:00"; //Bane.dk bug work-around
87                          String trainNumber = fields.get(2).asText();                                  departure.setTime(time);
88                          departure.setTrainNumber(trainNumber);                                  
89                                                            int updated = extractUpdated( fields.get(1) );
90                          String destination = fields.get(3).asText();                                  departure.setUpdated(updated);
91                          departure.setDestination(destination);                                  
92                                                            String trainNumber = fields.get(2).asText();
93                          String origin = fields.get(4).asText();                                  if (trainNumber.trim().length() == 1)
94                          departure.setOrigin(origin);                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
95                                                            departure.setTrainNumber(trainNumber);
96                          String location = fields.get(5).asText();                                  
97                          departure.setLocation(location);                                  String destination = fields.get(3).asText();
98                                                            departure.setDestination(destination);
99                          String status = fields.get(6).asText();                                  
100                          departure.setStatus(status);                                  String origin = fields.get(4).asText();
101                                                            departure.setOrigin(origin);
102                          String note = fields.get(7).asText();                                  
103                          departure.setNote(note);                                  String location = fields.get(5).asText();
104                                                            departure.setLocation(location);
105                          departureList.add(departure);                                  
106                  }                                  String status = fields.get(6).asText();
107                                    departure.setStatus(status);
108                                    
109                                    String note = extractNote( fields.get(7) );
110                                    departure.setNote(note);
111                                    
112                                    departureList.add(departure);
113                            }
114                        }
115                } else {
116                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
117              }              }
118                            
119              return departureList;              return departureList;
# Line 123  public class DepartureFetcher { Line 137  public class DepartureFetcher {
137                  return updated;                  return updated;
138          }          }
139                    
140            private String extractNote(HtmlElement noteTd) {
141                    String note = noteTd.asText().trim();
142                    
143                    List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
144                    if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
145                            note = note.substring(0,note.length() -1 );
146    
147                    return note;
148            }
149            
150            private String extractTrainNumber(HtmlElement trainTd) {
151                    String number = "";
152                    HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
153                    String href = anchorElement.getAttribute("href");
154                    String argstring = href.substring( href.indexOf('?') + 1);
155                    
156                    String args[] = argstring.split("&");
157                    for (String arg : args) {
158                            String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
159                            
160                            if (pair[0].equalsIgnoreCase("TogNr"))
161                                    number = pair[1];
162                    }
163                    
164                    
165                    
166                    return number;
167            }
168            
169          //test          //test
170          public static void main(String args[]) throws Exception{          public static void main(String args[]) throws Exception{
171                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();

Legend:
Removed from v.307  
changed lines
  Added in v.386

  ViewVC Help
Powered by ViewVC 1.1.20