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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20