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

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 305 - (hide annotations) (download)
Thu Sep 10 09:40:27 2009 UTC (14 years, 8 months ago) by torben
File size: 3425 byte(s)
first version of new departure provider (this is supposed to replace the php mess)
1 torben 305 package dk.thoerup.traininfoservice.banedk;
2    
3     import java.util.ArrayList;
4     import java.util.List;
5    
6     import com.gargoylesoftware.htmlunit.ProxyConfig;
7     import com.gargoylesoftware.htmlunit.WebClient;
8     import com.gargoylesoftware.htmlunit.html.DomNodeList;
9     import com.gargoylesoftware.htmlunit.html.HtmlElement;
10     import com.gargoylesoftware.htmlunit.html.HtmlPage;
11    
12     public class DepartureFetcher {
13    
14    
15     static ProxyConfig proxyConfig;
16     static {
17     proxyConfig = new ProxyConfig();
18     proxyConfig.setProxyHost("rafiki.t-hoerup.dk");
19     proxyConfig.setProxyPort(3128);
20     }
21    
22     public List<DepartureBean> lookupDepartures() throws Exception {
23    
24     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
25    
26     final WebClient webClient = new WebClient();
27     webClient.setTimeout(1000);
28     webClient.setProxyConfig(proxyConfig);
29     webClient.setJavaScriptEnabled(false);
30    
31     final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=FJRN&S=BJ");
32    
33     HtmlElement table = page.getElementById("afgangtabel");
34     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
35    
36     for (HtmlElement currentRow : tableRows) {
37     String rowClass = currentRow.getAttribute("class");
38     if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
39     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
40    
41     DepartureBean departure = new DepartureBean();
42    
43     String time = fields.get(0).asText();
44     departure.setTime(time);
45    
46     int updated = extractUpdated( fields.get(1) );
47     departure.setUpdated(updated);
48    
49     String trainNumber = fields.get(2).asText();
50     departure.setTrainNumber(trainNumber);
51    
52     String destination = fields.get(3).asText();
53     departure.setDestination(destination);
54    
55     String origin = fields.get(4).asText();
56     departure.setOrigin(origin);
57    
58     String location = fields.get(5).asText();
59     departure.setLocation(location);
60    
61     String status = fields.get(6).asText();
62     departure.setStatus(status);
63    
64     String note = fields.get(7).asText();
65     departure.setNote(note);
66    
67     departureList.add(departure);
68     }
69     }
70    
71     return departureList;
72     }
73    
74     private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
75     int updated = -1;
76    
77     DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
78     String updatedStr = updatedImgs.get(0).getAttribute("src");
79    
80     if (updatedStr != null) {
81     for (int i=0; i<updatedStr.length(); i++) {
82     char c = updatedStr.charAt(i);
83     if ( Character.isDigit(c)) {
84     updated = Character.digit(c, 10);
85     break;
86     }
87     }
88     }
89     return updated;
90     }
91    
92     //test
93     public static void main(String args[]) throws Exception{
94     DepartureFetcher f = new DepartureFetcher();
95     List<DepartureBean> deps = f.lookupDepartures();
96     for(DepartureBean d : deps) {
97     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
98     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
99     }
100    
101     System.out.println("--------------------------");
102     }
103     }

  ViewVC Help
Powered by ViewVC 1.1.20