/[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 308 - (hide annotations) (download)
Thu Sep 10 18:13:52 2009 UTC (14 years, 8 months ago) by torben
File size: 4365 byte(s)
Minor code tweaks
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    
10     import com.gargoylesoftware.htmlunit.WebClient;
11     import com.gargoylesoftware.htmlunit.html.DomNodeList;
12     import com.gargoylesoftware.htmlunit.html.HtmlElement;
13     import com.gargoylesoftware.htmlunit.html.HtmlPage;
14    
15 torben 307 import dk.thoerup.traininfoservice.DBConnection;
16    
17 torben 305 public class DepartureFetcher {
18 torben 307
19 torben 308
20 torben 307 public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
21     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
22    
23     Connection conn = null;
24     try
25     {
26     conn = DBConnection.getConnection();
27    
28     String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;
29     Statement stmt = conn.createStatement();
30     ResultSet rs = stmt.executeQuery(SQL);
31    
32     if (rs.next()) {
33     String code = rs.getString( 1 );
34     if (! rs.wasNull() ) {
35     List<DepartureBean> list = lookupDepartures(code, "FJRN");
36     departureList.addAll(list);
37     }
38    
39     code = rs.getString(2);
40     if (! rs.wasNull() ) {
41     List<DepartureBean> list = lookupDepartures(code, "S2");
42     departureList.addAll(list);
43     }
44     Collections.sort( departureList );
45    
46     }
47    
48     } finally {
49     if (conn != null && !conn.isClosed() ) {
50     conn.close();
51     }
52     }
53    
54     return departureList;
55 torben 305 }
56    
57 torben 307 public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
58 torben 305
59     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
60    
61     final WebClient webClient = new WebClient();
62     webClient.setTimeout(1000);
63     webClient.setJavaScriptEnabled(false);
64    
65 torben 307 final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);
66 torben 305
67     HtmlElement table = page.getElementById("afgangtabel");
68     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
69    
70     for (HtmlElement currentRow : tableRows) {
71     String rowClass = currentRow.getAttribute("class");
72     if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
73     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
74    
75     DepartureBean departure = new DepartureBean();
76    
77     String time = fields.get(0).asText();
78     departure.setTime(time);
79    
80     int updated = extractUpdated( fields.get(1) );
81     departure.setUpdated(updated);
82    
83     String trainNumber = fields.get(2).asText();
84     departure.setTrainNumber(trainNumber);
85    
86     String destination = fields.get(3).asText();
87     departure.setDestination(destination);
88    
89     String origin = fields.get(4).asText();
90     departure.setOrigin(origin);
91    
92     String location = fields.get(5).asText();
93     departure.setLocation(location);
94    
95     String status = fields.get(6).asText();
96     departure.setStatus(status);
97    
98     String note = fields.get(7).asText();
99     departure.setNote(note);
100    
101     departureList.add(departure);
102     }
103     }
104    
105     return departureList;
106     }
107    
108     private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
109     int updated = -1;
110    
111     DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
112     String updatedStr = updatedImgs.get(0).getAttribute("src");
113    
114     if (updatedStr != null) {
115     for (int i=0; i<updatedStr.length(); i++) {
116     char c = updatedStr.charAt(i);
117     if ( Character.isDigit(c)) {
118     updated = Character.digit(c, 10);
119     break;
120     }
121     }
122     }
123     return updated;
124     }
125    
126     //test
127     public static void main(String args[]) throws Exception{
128     DepartureFetcher f = new DepartureFetcher();
129 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
130 torben 305 for(DepartureBean d : deps) {
131     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
132     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
133     }
134    
135     System.out.println("--------------------------");
136     }
137     }

  ViewVC Help
Powered by ViewVC 1.1.20