7 |
import java.util.Collections; |
import java.util.Collections; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
|
10 |
|
import com.gargoylesoftware.htmlunit.ProxyConfig; |
11 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
12 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
13 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
17 |
|
|
18 |
public class DepartureFetcher { |
public class DepartureFetcher { |
19 |
|
|
20 |
@SuppressWarnings("unchecked") |
|
21 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
22 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
23 |
|
|
61 |
|
|
62 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
63 |
webClient.setTimeout(1000); |
webClient.setTimeout(1000); |
64 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
65 |
|
|
66 |
|
|
67 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
68 |
|
|
69 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
|
70 |
|
|
71 |
for (HtmlElement currentRow : tableRows) { |
if (table != null) { |
72 |
String rowClass = currentRow.getAttribute("class"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
73 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
|
74 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
for (HtmlElement currentRow : tableRows) { |
75 |
|
String rowClass = currentRow.getAttribute("class"); |
76 |
DepartureBean departure = new DepartureBean(); |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
77 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
78 |
String time = fields.get(0).asText(); |
|
79 |
departure.setTime(time); |
DepartureBean departure = new DepartureBean(); |
80 |
|
|
81 |
int updated = extractUpdated( fields.get(1) ); |
String time = fields.get(0).asText(); |
82 |
departure.setUpdated(updated); |
departure.setTime(time); |
83 |
|
|
84 |
String trainNumber = fields.get(2).asText(); |
int updated = extractUpdated( fields.get(1) ); |
85 |
departure.setTrainNumber(trainNumber); |
departure.setUpdated(updated); |
86 |
|
|
87 |
String destination = fields.get(3).asText(); |
String trainNumber = fields.get(2).asText(); |
88 |
departure.setDestination(destination); |
departure.setTrainNumber(trainNumber); |
89 |
|
|
90 |
String origin = fields.get(4).asText(); |
String destination = fields.get(3).asText(); |
91 |
departure.setOrigin(origin); |
departure.setDestination(destination); |
92 |
|
|
93 |
String location = fields.get(5).asText(); |
String origin = fields.get(4).asText(); |
94 |
departure.setLocation(location); |
departure.setOrigin(origin); |
95 |
|
|
96 |
String status = fields.get(6).asText(); |
String location = fields.get(5).asText(); |
97 |
departure.setStatus(status); |
departure.setLocation(location); |
98 |
|
|
99 |
String note = fields.get(7).asText(); |
String status = fields.get(6).asText(); |
100 |
departure.setNote(note); |
departure.setStatus(status); |
101 |
|
|
102 |
departureList.add(departure); |
String note = extractNote( fields.get(7) ); |
103 |
} |
departure.setNote(note); |
104 |
|
|
105 |
|
departureList.add(departure); |
106 |
|
} |
107 |
|
} |
108 |
} |
} |
|
|
|
109 |
return departureList; |
return departureList; |
110 |
} |
} |
111 |
|
|
127 |
return updated; |
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 |
//test |
141 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
142 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |