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

  ViewVC Help
Powered by ViewVC 1.1.20