/[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 991 by torben, Sat Jul 10 11:01:07 2010 UTC revision 992 by torben, Wed Jul 14 08:05:31 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    
4    import java.net.URL;
5  import java.util.Collections;  import java.util.Collections;
 import java.util.List;  
6  import java.util.Map;  import java.util.Map;
7  import java.util.logging.Logger;  import java.util.logging.Logger;
8    
9  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.nodes.Document;
10  import com.gargoylesoftware.htmlunit.WebClient;  import org.jsoup.nodes.Element;
11  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import org.jsoup.select.Elements;
 import com.gargoylesoftware.htmlunit.html.HtmlElement;  
 import com.gargoylesoftware.htmlunit.html.HtmlPage;  
12    
13  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
14  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
# Line 106  public class DepartureFetcher { Line 104  public class DepartureFetcher {
104          public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {
105                                    
106                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
107                                    
             final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );  
             webClient.setTimeout(2500);  
             webClient.setJavaScriptEnabled(false);  
               
108                            
109              String typeString = getTypeString(type);              String typeString = getTypeString(type);
110              String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";              String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
# Line 118  public class DepartureFetcher { Line 112  public class DepartureFetcher {
112              //String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;              //String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
113              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
114    
115              //logger.info("URI: " + uri);              //logger.info("URI: " + uri);          
116              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), 2500);
117              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
118                            
119              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document page = (Document) breaker.invoke(wrapper);
120                            
121              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
122              HtmlElement table = page.getElementById(tableName);              Element table = page.getElementById(tableName);
123                            
124              if (table != null) {              if (table != null) {
125                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");                      Elements tableRows =  table.getElementsByTag("tr");
126                                            
127                      for (HtmlElement currentRow : tableRows) {                      for (Element currentRow : tableRows) {
128                          String rowClass = currentRow.getAttribute("class");                          String rowClass = currentRow.attr("class");
129                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
130                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                                  Elements fields = currentRow.getElementsByTag("td");
131                    
132                                  DepartureEntry departure = new DepartureEntry();                                  DepartureEntry departure = new DepartureEntry();
133                                                                    
134                                  String time = fields.get(0).asText();                                  String time = fields.get(0).text();
135                                  if (time.equals(""))                                  if (time.equals(""))
136                                          time = "0:00"; //Bane.dk bug work-around                                          time = "0:00"; //Bane.dk bug work-around
137                                  departure.setTime(time);                                  departure.setTime(time);
# Line 145  public class DepartureFetcher { Line 139  public class DepartureFetcher {
139                                  int updated = extractUpdated( fields.get(1) );                                  int updated = extractUpdated( fields.get(1) );
140                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
141                                                                    
142                                  String trainNumber = fields.get(2).asText();                                  String trainNumber = fields.get(2).text();
143                                  if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber                                  if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
144                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
145                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
146                                                                    
147                                  String destination = fields.get(3).asText();                                  String destination = fields.get(3).text();
148                                  departure.setDestination(destination);                                  departure.setDestination(destination);
149                                                                    
150                                  String origin = fields.get(4).asText();                                  String origin = fields.get(4).text();
151                                  departure.setOrigin(origin);                                  departure.setOrigin(origin);
152                                                                    
153                                  String location = fields.get(5).asText();                                  String location = fields.get(5).text();
154                                  departure.setLocation(location);                                  departure.setLocation(location);
155                                                                    
156                                  String status = fields.get(6).asText().trim();                                  String status = fields.get(6).text().trim();
157                                  departure.setStatus(status);                                  departure.setStatus(status);
158                                                                    
159                                  String note = extractNote( fields.get(7) );                                  String note = extractNote( fields.get(7) );
# Line 174  public class DepartureFetcher { Line 168  public class DepartureFetcher {
168                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
169              }              }
170                            
171              HtmlElement notifDiv = page.getElementById("station_planlagte_text");              Element notifDiv = page.getElementById("station_planlagte_text");
172              if (notifDiv != null) {              if (notifDiv != null) {
173    
174                  DomNodeList<HtmlElement> tables = notifDiv.getElementsByTagName("table");                  Elements tables = notifDiv.getElementsByTag("table");
175                  for (HtmlElement tab : tables) {                  for (Element tab : tables) {
176    
177                          DomNodeList<HtmlElement> anchors = tab.getElementsByTagName("a");                                        Elements anchors = tab.getElementsByTag("a");          
178                          if (anchors.size() == 2) {                          if (anchors.size() == 2) {
179                                  departureBean.notifications.add(  anchors.get(1).getTextContent() );                                  departureBean.notifications.add(  anchors.get(1).text() );
180                          }                          }
181                  }                  }
182                                    
183              }              }
184                            
185                            
             webClient.closeAllWindows();  
               
186              return departureBean;              return departureBean;
187          }          }
188                    
# Line 263  public class DepartureFetcher { Line 255  public class DepartureFetcher {
255          }*/          }*/
256    
257                    
258          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"          private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
259                  int updated = -1;                  int updated = -1;
260                                    
261                  DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");                  Elements updatedImgs = updatedTd.getElementsByTag("img");
262                  String updatedStr = updatedImgs.get(0).getAttribute("src");                  String updatedStr = updatedImgs.get(0).attr("src");
263                                    
264                  if (updatedStr != null) {                  if (updatedStr != null) {
265                          for (int i=0; i<updatedStr.length(); i++) {                          for (int i=0; i<updatedStr.length(); i++) {
# Line 281  public class DepartureFetcher { Line 273  public class DepartureFetcher {
273                  return updated;                  return updated;
274          }          }
275                    
276          private String extractNote(HtmlElement noteTd) {          private String extractNote(Element noteTd) {
277                  String note = noteTd.asText().trim();                  String note = noteTd.text().trim();
278                    
279                                    
280                  List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");                  Elements elems = noteTd.getElementsByClass("bemtype");
281                  if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')                  if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
282                          note = note.substring(0,note.length() -1 );                          note = note.substring(0,note.length() -1 );
283    
284                  return note;                  return note;
285          }          }
286                    
287          private String extractTrainNumber(HtmlElement trainTd) {          private String extractTrainNumber(Element trainTd) {
288                  HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
289                  String href = anchorElement.getAttribute("href");                  String href = anchorElement.attr("href");
290                                    
291                  int pos = href.lastIndexOf('/');                  int pos = href.lastIndexOf('/');
292                  String number = href.substring(pos+1);                  String number = href.substring(pos+1);

Legend:
Removed from v.991  
changed lines
  Added in v.992

  ViewVC Help
Powered by ViewVC 1.1.20