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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 375 - (hide annotations) (download)
Wed Sep 30 20:58:37 2009 UTC (14 years, 8 months ago) by torben
Original Path: android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
File size: 5781 byte(s)
Always save a time string, no matter whether bane.dk has one for us :)
1 torben 305 package dk.thoerup.traininfoservice.banedk;
2    
3 torben 307 import java.sql.Connection;
4     import java.sql.ResultSet;
5     import java.sql.Statement;
6 torben 305 import java.util.ArrayList;
7 torben 307 import java.util.Collections;
8 torben 305 import java.util.List;
9 torben 348 import java.util.logging.Logger;
10 torben 305
11 torben 313 import com.gargoylesoftware.htmlunit.ProxyConfig;
12 torben 305 import com.gargoylesoftware.htmlunit.WebClient;
13     import com.gargoylesoftware.htmlunit.html.DomNodeList;
14     import com.gargoylesoftware.htmlunit.html.HtmlElement;
15     import com.gargoylesoftware.htmlunit.html.HtmlPage;
16    
17 torben 307 import dk.thoerup.traininfoservice.DBConnection;
18    
19 torben 305 public class DepartureFetcher {
20 torben 348
21     Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
22 torben 307
23 torben 308
24 torben 307 public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
25     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
26    
27     Connection conn = null;
28     try
29     {
30     conn = DBConnection.getConnection();
31    
32     String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;
33     Statement stmt = conn.createStatement();
34     ResultSet rs = stmt.executeQuery(SQL);
35    
36     if (rs.next()) {
37     String code = rs.getString( 1 );
38     if (! rs.wasNull() ) {
39     List<DepartureBean> list = lookupDepartures(code, "FJRN");
40     departureList.addAll(list);
41     }
42    
43     code = rs.getString(2);
44     if (! rs.wasNull() ) {
45     List<DepartureBean> list = lookupDepartures(code, "S2");
46     departureList.addAll(list);
47     }
48     Collections.sort( departureList );
49    
50     }
51    
52     } finally {
53     if (conn != null && !conn.isClosed() ) {
54     conn.close();
55     }
56     }
57    
58     return departureList;
59 torben 305 }
60    
61 torben 307 public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
62 torben 305
63     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
64    
65     final WebClient webClient = new WebClient();
66     webClient.setTimeout(1000);
67 torben 313 webClient.setJavaScriptEnabled(false);
68    
69 torben 305
70 torben 307 final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);
71 torben 305
72     HtmlElement table = page.getElementById("afgangtabel");
73    
74 torben 342 if (table != null) {
75     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
76    
77     for (HtmlElement currentRow : tableRows) {
78     String rowClass = currentRow.getAttribute("class");
79     if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
80     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
81    
82     DepartureBean departure = new DepartureBean();
83    
84     String time = fields.get(0).asText();
85 torben 375 if (time.equals(""))
86     time = "0:00"; //Bane.dk bug work-around
87 torben 342 departure.setTime(time);
88    
89     int updated = extractUpdated( fields.get(1) );
90     departure.setUpdated(updated);
91    
92     String trainNumber = fields.get(2).asText();
93 torben 349 if (trainNumber.trim().length() == 1)
94     trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
95 torben 342 departure.setTrainNumber(trainNumber);
96    
97     String destination = fields.get(3).asText();
98     departure.setDestination(destination);
99    
100     String origin = fields.get(4).asText();
101     departure.setOrigin(origin);
102    
103     String location = fields.get(5).asText();
104     departure.setLocation(location);
105    
106     String status = fields.get(6).asText();
107     departure.setStatus(status);
108    
109     String note = extractNote( fields.get(7) );
110     departure.setNote(note);
111    
112     departureList.add(departure);
113     }
114     }
115 torben 348 } else {
116     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
117 torben 305 }
118 torben 348
119 torben 305 return departureList;
120     }
121    
122     private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
123     int updated = -1;
124    
125     DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
126     String updatedStr = updatedImgs.get(0).getAttribute("src");
127    
128     if (updatedStr != null) {
129     for (int i=0; i<updatedStr.length(); i++) {
130     char c = updatedStr.charAt(i);
131     if ( Character.isDigit(c)) {
132     updated = Character.digit(c, 10);
133     break;
134     }
135     }
136     }
137     return updated;
138     }
139    
140 torben 313 private String extractNote(HtmlElement noteTd) {
141     String note = noteTd.asText().trim();
142    
143     List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
144     if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
145     note = note.substring(0,note.length() -1 );
146    
147     return note;
148     }
149    
150 torben 349 private String extractTrainNumber(HtmlElement trainTd) {
151     String number = "";
152     HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
153     String href = anchorElement.getAttribute("href");
154     String argstring = href.substring( href.indexOf('?') + 1);
155    
156     String args[] = argstring.split("&");
157     for (String arg : args) {
158     String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
159    
160     if (pair[0].equalsIgnoreCase("TogNr"))
161     number = pair[1];
162     }
163    
164    
165    
166     return number;
167     }
168    
169 torben 305 //test
170     public static void main(String args[]) throws Exception{
171     DepartureFetcher f = new DepartureFetcher();
172 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
173 torben 305 for(DepartureBean d : deps) {
174     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
175     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
176     }
177    
178     System.out.println("--------------------------");
179     }
180     }

  ViewVC Help
Powered by ViewVC 1.1.20