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

  ViewVC Help
Powered by ViewVC 1.1.20