/[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 313 by torben, Fri Sep 11 07:13:46 2009 UTC revision 583 by torben, Tue Feb 2 19:18:20 2010 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.Map;
10    import java.util.logging.Logger;
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;
15  import com.gargoylesoftware.htmlunit.html.HtmlPage;  import com.gargoylesoftware.htmlunit.html.HtmlPage;
16    
17    import dk.thoerup.circuitbreaker.CircuitBreaker;
18    import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19  import dk.thoerup.traininfoservice.DBConnection;  import dk.thoerup.traininfoservice.DBConnection;
20    
21  public class DepartureFetcher {  public class DepartureFetcher {
22            
23            Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
24            
25            Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000);
26            
27            private boolean useTempSite;
28            
29            public DepartureFetcher(boolean tempSite) {
30                    useTempSite = tempSite;
31            }
32            
33            
34                    
35            
36            public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
37    
38                    List<DepartureBean> list = cache.get(stationID);
39                    
40                    if (list == null) {
41                            list = lookupDepartures(stationID);
42                            cache.put(stationID, list);
43                    } else {
44                            logger.info("Departure: Cache hit " + stationID); //remove before production
45                    }
46                    return list;
47            }
48                                    
49    
50          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
# Line 56  public class DepartureFetcher { Line 85  public class DepartureFetcher {
85          }          }
86                    
87          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
88                    if (useTempSite == false) {
89                            return lookupDeparturesNormalSite(stationcode, type);
90                    } else {
91                            return lookupDeparturesFromTemporarySite(stationcode, type);
92                    }
93            }
94            
95            public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
96                                    
97                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
98                                    
99              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient();
100              webClient.setTimeout(1000);              webClient.setTimeout(2500);
101              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
102                                                            
103                String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
104                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
105                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
106                            
107              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
108                            
109              HtmlElement table = page.getElementById("afgangtabel");              HtmlElement table = page.getElementById("afgangtabel");
             DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");  
110                            
111              for (HtmlElement currentRow : tableRows) {              if (table != null) {
112                  String rowClass = currentRow.getAttribute("class");                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
113                  if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                      
114                          DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                      for (HtmlElement currentRow : tableRows) {
115                            String rowClass = currentRow.getAttribute("class");
116                          DepartureBean departure = new DepartureBean();                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
117                                                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
118                          String time = fields.get(0).asText();          
119                          departure.setTime(time);                                  DepartureBean departure = new DepartureBean();
120                                                            
121                          int updated = extractUpdated( fields.get(1) );                                  String time = fields.get(0).asText();
122                          departure.setUpdated(updated);                                  if (time.equals(""))
123                                                                    time = "0:00"; //Bane.dk bug work-around
124                          String trainNumber = fields.get(2).asText();                                  departure.setTime(time);
125                          departure.setTrainNumber(trainNumber);                                  
126                                                            int updated = extractUpdated( fields.get(1) );
127                          String destination = fields.get(3).asText();                                  departure.setUpdated(updated);
128                          departure.setDestination(destination);                                  
129                                                            String trainNumber = fields.get(2).asText();
130                          String origin = fields.get(4).asText();                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
131                          departure.setOrigin(origin);                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
132                                                            departure.setTrainNumber(trainNumber);
133                          String location = fields.get(5).asText();                                  
134                          departure.setLocation(location);                                  String destination = fields.get(3).asText();
135                                                            departure.setDestination(destination);
136                          String status = fields.get(6).asText();                                  
137                          departure.setStatus(status);                                  String origin = fields.get(4).asText();
138                                                            departure.setOrigin(origin);
139                          String note = extractNote( fields.get(7) );                                  
140                          departure.setNote(note);                                  String location = fields.get(5).asText();
141                                                            departure.setLocation(location);
142                          departureList.add(departure);                                  
143                  }                                  String status = fields.get(6).asText();
144                                    departure.setStatus(status);
145                                    
146                                    String note = extractNote( fields.get(7) );
147                                    departure.setNote(note);
148                                    
149                                    departureList.add(departure);
150                            }
151                        }
152                } else {
153                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
154              }              }
155                            
156              return departureList;              return departureList;
157          }          }
158                    
159            public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
160                    
161                    List<DepartureBean> departureList = new ArrayList<DepartureBean>();
162                    
163                final WebClient webClient = new WebClient();
164                webClient.setTimeout(2500);
165                webClient.setJavaScriptEnabled(false);
166                
167    
168                String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
169                
170                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
171                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
172                
173                HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
174                
175                HtmlElement table = page.getElementById("traf_afgang");
176                
177                if (table != null) {                        
178                        DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
179                        
180                        boolean isFirst = true;
181                        
182                        for (HtmlElement currentRow : tableRows) {
183                            if (isFirst == true) { //skip table headers
184                                    isFirst = false;
185                                    continue;
186                            }
187                            
188                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
189    
190                            DepartureBean departure = new DepartureBean();
191    
192                            String time = fields.get(0).asText().trim();
193    
194                            if (time.equals(""))
195                                    time = "0:00"; //Bane.dk bug work-around
196                            departure.setTime(time);
197    
198    
199                            String trainNumber = fields.get(1).asText();
200                            departure.setTrainNumber(trainNumber);
201    
202                            String destination = fields.get(2).asText();
203                            departure.setDestination(destination);
204    
205                            String origin = fields.get(3).asText();
206                            departure.setOrigin(origin);
207    
208                            String status = fields.get(4).asText();
209                            departure.setStatus(status);
210    
211                            String note = fields.get(5).asText();
212                            departure.setNote(note);
213    
214                            departureList.add(departure);
215                        }
216                } else {
217                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
218                }
219                
220                return departureList;
221            }
222    
223            
224          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
225                  int updated = -1;                  int updated = -1;
226                                    
# Line 135  public class DepartureFetcher { Line 249  public class DepartureFetcher {
249                  return note;                  return note;
250          }          }
251                    
252            private String extractTrainNumber(HtmlElement trainTd) {
253                    String number = "";
254                    HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
255                    String href = anchorElement.getAttribute("href");
256                    String argstring = href.substring( href.indexOf('?') + 1);
257                    
258                    String args[] = argstring.split("&");
259                    for (String arg : args) {
260                            String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
261                            
262                            if (pair[0].equalsIgnoreCase("TogNr"))
263                                    number = pair[1];
264                    }
265                    
266                    
267                    
268                    return number;
269            }
270            
271          //test          //test
272          public static void main(String args[]) throws Exception{          /*
273            public static void main(String args[]) throws Exception {
274                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();
275                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
276                  for(DepartureBean d : deps) {                  for(DepartureBean d : deps) {
# Line 145  public class DepartureFetcher { Line 279  public class DepartureFetcher {
279                  }                  }
280                                    
281                  System.out.println("--------------------------");                  System.out.println("--------------------------");
282          }          }*/
283  }  }

Legend:
Removed from v.313  
changed lines
  Added in v.583

  ViewVC Help
Powered by ViewVC 1.1.20