/[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 313 - (show annotations) (download)
Fri Sep 11 07:13:46 2009 UTC (14 years, 8 months ago) by torben
File size: 4759 byte(s)
Remove that frickin information 'i' from the note
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 DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
71
72 for (HtmlElement currentRow : tableRows) {
73 String rowClass = currentRow.getAttribute("class");
74 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
75 DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
76
77 DepartureBean departure = new DepartureBean();
78
79 String time = fields.get(0).asText();
80 departure.setTime(time);
81
82 int updated = extractUpdated( fields.get(1) );
83 departure.setUpdated(updated);
84
85 String trainNumber = fields.get(2).asText();
86 departure.setTrainNumber(trainNumber);
87
88 String destination = fields.get(3).asText();
89 departure.setDestination(destination);
90
91 String origin = fields.get(4).asText();
92 departure.setOrigin(origin);
93
94 String location = fields.get(5).asText();
95 departure.setLocation(location);
96
97 String status = fields.get(6).asText();
98 departure.setStatus(status);
99
100 String note = extractNote( fields.get(7) );
101 departure.setNote(note);
102
103 departureList.add(departure);
104 }
105 }
106
107 return departureList;
108 }
109
110 private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
111 int updated = -1;
112
113 DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
114 String updatedStr = updatedImgs.get(0).getAttribute("src");
115
116 if (updatedStr != null) {
117 for (int i=0; i<updatedStr.length(); i++) {
118 char c = updatedStr.charAt(i);
119 if ( Character.isDigit(c)) {
120 updated = Character.digit(c, 10);
121 break;
122 }
123 }
124 }
125 return updated;
126 }
127
128 private String extractNote(HtmlElement noteTd) {
129 String note = noteTd.asText().trim();
130
131 List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
132 if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
133 note = note.substring(0,note.length() -1 );
134
135 return note;
136 }
137
138 //test
139 public static void main(String args[]) throws Exception{
140 DepartureFetcher f = new DepartureFetcher();
141 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
142 for(DepartureBean d : deps) {
143 System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
144 d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
145 }
146
147 System.out.println("--------------------------");
148 }
149 }

  ViewVC Help
Powered by ViewVC 1.1.20