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

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

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

revision 992 by torben, Sat Jul 10 10:53:44 2010 UTC revision 993 by torben, Wed Jul 14 09:49:56 2010 UTC
# Line 4  package dk.thoerup.traininfoservice.bane Line 4  package dk.thoerup.traininfoservice.bane
4    *    *
5    */    */
6    
7    import java.net.URL;
8  import java.text.SimpleDateFormat;  import java.text.SimpleDateFormat;
9  import java.util.ArrayList;  import java.util.ArrayList;
10  import java.util.Date;  import java.util.Date;
11  import java.util.List;  import java.util.List;
12  import java.util.logging.Logger;  import java.util.logging.Logger;
13    
14  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.Jsoup;
15  import com.gargoylesoftware.htmlunit.WebClient;  import org.jsoup.nodes.Document;
16  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import org.jsoup.nodes.Element;
17  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import org.jsoup.select.Elements;
18  import com.gargoylesoftware.htmlunit.html.HtmlPage;  
19    
20    
21  public class RejseplanenFetcher {  public class RejseplanenFetcher {
22                    
# Line 30  public class RejseplanenFetcher { Line 32  public class RejseplanenFetcher {
32                                    
33                  List<DepartureEntry> departureList = new ArrayList<DepartureEntry>();                  List<DepartureEntry> departureList = new ArrayList<DepartureEntry>();
34                                    
             final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );  
             webClient.getCookieManager().setCookiesEnabled( false );  
             webClient.setTimeout(2500);  
             webClient.setJavaScriptEnabled(false);  
   
35                                                        
36                            
37              String uri = "http://www.rejseplanen.dk/bin/stboard.exe/mn?ml=m&input=" + stationcode + "&boardType=dep&time=" + getNow() + "&productsFilter=11111000001&selectDate=today&maxJourneys=200&start=yes";              String uri = "http://www.rejseplanen.dk/bin/stboard.exe/mn?ml=m&input=" + stationcode + "&boardType=dep&time=" + getNow() + "&productsFilter=11111000001&selectDate=today&maxJourneys=200&start=yes";
38                            
39              HtmlPage page = webClient.getPage(uri);              Document doc = Jsoup.parse(new URL(uri), 2500);
40    
41                            
42                            
43              HtmlElement table = page.getElementById("hafasSqResults");              Element table = doc.getElementById("hafasSqResults");
44                            
45              if (table != null) {              if (table != null) {
46                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");                      Elements tableRows =  table.getElementsByTag("tr");
47    
48                      for (HtmlElement currentRow : tableRows) {                      for (Element currentRow : tableRows) {
49                          String id = currentRow.getId();                          String id = currentRow.id();
50                          logger.info("tr.id=" + id);                          logger.info("tr.id=" + id);
51                          String rowClass = currentRow.getAttribute("class");                          String rowClass = currentRow.attr("class");
52                          if (rowClass != null  && rowClass.indexOf("sqToggleDetails") != -1      ) {                          if (rowClass != null  && rowClass.indexOf("sqToggleDetails") != -1      ) {
53                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                                  Elements fields = currentRow.getElementsByTag("td");
54                    
55                                  logger.severe("td count=" + fields.size());                                  logger.severe("td count=" + fields.size());
56                                  if (fields.size() == 0)                                  if (fields.size() == 0)
57                                          continue;                                          continue;
58    
59                                  for (int f=0; f<fields.size();f++) {                                  for (int f=0; f<fields.size();f++) {
60                                          logger.severe("" + f + "=" + fields.get(f).getAttribute("class") );                                          logger.severe("" + f + "=" + fields.get(f).attr("class") );
61                                  }                                  }
62                                                                    
63                                                                    
64                                                                    
65                                  DepartureEntry departure = new DepartureEntry();                                  DepartureEntry departure = new DepartureEntry();
66                                                                    
67                                  String time = fields.get(0).asText();                                    String time = fields.get(0).text();    
68                                  departure.setTime(time);                                  departure.setTime(time);
69                                                                    
70                                  int updated = 0; //updated not supported by rejseplanen                                  int updated = 0; //updated not supported by rejseplanen
71                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
72                                                                    
73                                  String trainNumber = fields.get(1).asText();                                  String trainNumber = fields.get(1).text();
74                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
75                                                                    
76                                                                    
# Line 80  public class RejseplanenFetcher { Line 78  public class RejseplanenFetcher {
78                                                                    
79                                                                    
80                                  String destination = fields.get(3).                                  String destination = fields.get(3).
81                                          getElementsByTagName("div").                                          getElementsByTag("div").
82                                          get(0).                                          get(0).
83                                          getElementsByAttribute("span", "class", "bold").                                          getElementsByClass("bold").
84                                          get(0).                                          get(0).
85                                          asText();                                          text();
86                                  departure.setDestination(destination);                                  departure.setDestination(destination);
87                                                                    
88                                  //origin may take a bit more work                                  //origin may take a bit more work
# Line 111  public class RejseplanenFetcher { Line 109  public class RejseplanenFetcher {
109              } else {              } else {
110                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
111              }              }
112              webClient.closeAllWindows();  
113                            
114              return departureList;              return departureList;
115          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20