/[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 387 by torben, Fri Oct 2 15:06:08 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    
12  import com.gargoylesoftware.htmlunit.WebClient;  import com.gargoylesoftware.htmlunit.WebClient;
13  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import com.gargoylesoftware.htmlunit.html.DomNodeList;
# 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            TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000);
24            
25                    
26            
27            public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
28    
29                    List<DepartureBean> list = cache.get(stationID);
30                    
31                    if (list == null) {
32                            logger.warning("Departure: Cache miss " + stationID); //remove before production
33                            list = lookupDepartures(stationID);
34                            cache.put(stationID, list);
35                    } else {
36                            logger.warning("Departure: Cache hit " + stationID); //remove before production
37                    }
38                    return list;
39            }
40                                    
41          @SuppressWarnings("unchecked")  
42          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
43                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
44                                    
# Line 59  public class DepartureFetcher { Line 81  public class DepartureFetcher {
81                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
82                                    
83              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient();
84              webClient.setTimeout(1000);              webClient.setTimeout(2500);
85              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
86                                
87                            
88              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);
89                            
90              HtmlElement table = page.getElementById("afgangtabel");              HtmlElement table = page.getElementById("afgangtabel");
             DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");  
91                            
92              for (HtmlElement currentRow : tableRows) {              if (table != null) {
93                  String rowClass = currentRow.getAttribute("class");                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
94                  if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                      
95                          DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                      for (HtmlElement currentRow : tableRows) {
96                            String rowClass = currentRow.getAttribute("class");
97                          DepartureBean departure = new DepartureBean();                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
98                                                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
99                          String time = fields.get(0).asText();          
100                          departure.setTime(time);                                  DepartureBean departure = new DepartureBean();
101                                                            
102                          int updated = extractUpdated( fields.get(1) );                                  String time = fields.get(0).asText();
103                          departure.setUpdated(updated);                                  if (time.equals(""))
104                                                                    time = "0:00"; //Bane.dk bug work-around
105                          String trainNumber = fields.get(2).asText();                                  departure.setTime(time);
106                          departure.setTrainNumber(trainNumber);                                  
107                                                            int updated = extractUpdated( fields.get(1) );
108                          String destination = fields.get(3).asText();                                  departure.setUpdated(updated);
109                          departure.setDestination(destination);                                  
110                                                            String trainNumber = fields.get(2).asText();
111                          String origin = fields.get(4).asText();                                  if (trainNumber.trim().length() == 1)
112                          departure.setOrigin(origin);                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
113                                                            departure.setTrainNumber(trainNumber);
114                          String location = fields.get(5).asText();                                  
115                          departure.setLocation(location);                                  String destination = fields.get(3).asText();
116                                                            departure.setDestination(destination);
117                          String status = fields.get(6).asText();                                  
118                          departure.setStatus(status);                                  String origin = fields.get(4).asText();
119                                                            departure.setOrigin(origin);
120                          String note = fields.get(7).asText();                                  
121                          departure.setNote(note);                                  String location = fields.get(5).asText();
122                                                            departure.setLocation(location);
123                          departureList.add(departure);                                  
124                  }                                  String status = fields.get(6).asText();
125                                    departure.setStatus(status);
126                                    
127                                    String note = extractNote( fields.get(7) );
128                                    departure.setNote(note);
129                                    
130                                    departureList.add(departure);
131                            }
132                        }
133                } else {
134                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
135              }              }
136                            
137              return departureList;              return departureList;
# Line 123  public class DepartureFetcher { Line 155  public class DepartureFetcher {
155                  return updated;                  return updated;
156          }          }
157                    
158            private String extractNote(HtmlElement noteTd) {
159                    String note = noteTd.asText().trim();
160                    
161                    List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
162                    if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
163                            note = note.substring(0,note.length() -1 );
164    
165                    return note;
166            }
167            
168            private String extractTrainNumber(HtmlElement trainTd) {
169                    String number = "";
170                    HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
171                    String href = anchorElement.getAttribute("href");
172                    String argstring = href.substring( href.indexOf('?') + 1);
173                    
174                    String args[] = argstring.split("&");
175                    for (String arg : args) {
176                            String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
177                            
178                            if (pair[0].equalsIgnoreCase("TogNr"))
179                                    number = pair[1];
180                    }
181                    
182                    
183                    
184                    return number;
185            }
186            
187          //test          //test
188          public static void main(String args[]) throws Exception{          public static void main(String args[]) throws Exception{
189                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();

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

  ViewVC Help
Powered by ViewVC 1.1.20