/[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 978 by torben, Sat Jul 10 10:53:44 2010 UTC revision 1061 by torben, Thu Sep 16 14:04:28 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    import dk.thoerup.android.traininfo.common.DepartureEntry;
20    
21    
22    
23  public class RejseplanenFetcher {  public class RejseplanenFetcher {
24                    
# Line 30  public class RejseplanenFetcher { Line 34  public class RejseplanenFetcher {
34                                    
35                  List<DepartureEntry> departureList = new ArrayList<DepartureEntry>();                  List<DepartureEntry> departureList = new ArrayList<DepartureEntry>();
36                                    
             final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );  
             webClient.getCookieManager().setCookiesEnabled( false );  
             webClient.setTimeout(2500);  
             webClient.setJavaScriptEnabled(false);  
   
37                                                        
38                            
39              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";
40                            
41              HtmlPage page = webClient.getPage(uri);              Document doc = Jsoup.parse(new URL(uri), 2500);
42    
43                            
44                            
45              HtmlElement table = page.getElementById("hafasSqResults");              Element table = doc.getElementById("hafasSqResults");
46                            
47              if (table != null) {              if (table != null) {
48                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");                      Elements tableRows =  table.getElementsByTag("tr");
49    
50                      for (HtmlElement currentRow : tableRows) {                      for (Element currentRow : tableRows) {
51                          String id = currentRow.getId();                          String id = currentRow.id();
52                          logger.info("tr.id=" + id);                          logger.info("tr.id=" + id);
53                          String rowClass = currentRow.getAttribute("class");                          String rowClass = currentRow.attr("class");
54                          if (rowClass != null  && rowClass.indexOf("sqToggleDetails") != -1      ) {                          if (rowClass != null  && rowClass.indexOf("sqToggleDetails") != -1      ) {
55                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                                  Elements fields = currentRow.getElementsByTag("td");
56                    
57                                  logger.severe("td count=" + fields.size());                                  logger.severe("td count=" + fields.size());
58                                  if (fields.size() == 0)                                  if (fields.size() == 0)
59                                          continue;                                          continue;
60    
61                                  for (int f=0; f<fields.size();f++) {                                  for (int f=0; f<fields.size();f++) {
62                                          logger.severe("" + f + "=" + fields.get(f).getAttribute("class") );                                          logger.severe("" + f + "=" + fields.get(f).attr("class") );
63                                  }                                  }
64                                                                    
65                                                                    
66                                                                    
67                                  DepartureEntry departure = new DepartureEntry();                                  DepartureEntry departure = new DepartureEntry();
68                                                                    
69                                  String time = fields.get(0).asText();                                    String time = fields.get(0).text();    
70                                  departure.setTime(time);                                  departure.setTime(time);
71                                                                    
72                                  int updated = 0; //updated not supported by rejseplanen                                  int updated = 0; //updated not supported by rejseplanen
73                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
74                                                                    
75                                  String trainNumber = fields.get(1).asText();                                  String trainNumber = fields.get(1).text();
76                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
77                                                                    
78                                                                    
# Line 80  public class RejseplanenFetcher { Line 80  public class RejseplanenFetcher {
80                                                                    
81                                                                    
82                                  String destination = fields.get(3).                                  String destination = fields.get(3).
83                                          getElementsByTagName("div").                                          getElementsByTag("div").
84                                          get(0).                                          get(0).
85                                          getElementsByAttribute("span", "class", "bold").                                          getElementsByClass("bold").
86                                          get(0).                                          get(0).
87                                          asText();                                          text();
88                                  departure.setDestination(destination);                                  departure.setDestination(destination);
89                                                                    
90                                  //origin may take a bit more work                                  //origin may take a bit more work
# Line 111  public class RejseplanenFetcher { Line 111  public class RejseplanenFetcher {
111              } else {              } else {
112                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
113              }              }
114              webClient.closeAllWindows();  
115                            
116              return departureList;              return departureList;
117          }          }

Legend:
Removed from v.978  
changed lines
  Added in v.1061

  ViewVC Help
Powered by ViewVC 1.1.20