1 |
torben |
847 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
/* Work in progress |
4 |
|
|
* |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import java.text.SimpleDateFormat; |
8 |
|
|
import java.util.ArrayList; |
9 |
|
|
import java.util.Date; |
10 |
|
|
import java.util.List; |
11 |
|
|
import java.util.logging.Logger; |
12 |
|
|
|
13 |
|
|
import com.gargoylesoftware.htmlunit.BrowserVersion; |
14 |
|
|
import com.gargoylesoftware.htmlunit.WebClient; |
15 |
|
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
16 |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
17 |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
18 |
|
|
|
19 |
|
|
public class RejseplanenFetcher { |
20 |
|
|
|
21 |
|
|
Logger logger = Logger.getLogger(RejseplanenFetcher.class.getName()); |
22 |
|
|
|
23 |
|
|
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); |
24 |
|
|
|
25 |
|
|
public String getNow() { |
26 |
|
|
return format.format( new Date() ); |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
public List<DepartureBean> lookupDepartures(int stationcode, String type, boolean arrival) throws Exception { |
30 |
|
|
|
31 |
|
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
32 |
|
|
|
33 |
|
|
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
34 |
|
|
webClient.getCookieManager().setCookiesEnabled( false ); |
35 |
|
|
webClient.setTimeout(2500); |
36 |
|
|
webClient.setJavaScriptEnabled(false); |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
String uri = "http://www.rejseplanen.dk/bin/stboard.exe/mn?ml=m&input=" + stationcode + "&boardType=dep&time=" + getNow() + "&productsFilter=11111000001&selectDate=today&maxJourneys=200&start=yes"; |
41 |
|
|
|
42 |
|
|
HtmlPage page = webClient.getPage(uri); |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
HtmlElement table = page.getElementById("hafasSqResults"); |
46 |
|
|
|
47 |
|
|
if (table != null) { |
48 |
|
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
49 |
|
|
|
50 |
|
|
for (HtmlElement currentRow : tableRows) { |
51 |
|
|
String id = currentRow.getId(); |
52 |
|
|
logger.info("tr.id=" + id); |
53 |
|
|
String rowClass = currentRow.getAttribute("class"); |
54 |
|
|
if (rowClass != null && rowClass.indexOf("sqToggleDetails") != -1 ) { |
55 |
|
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
56 |
|
|
|
57 |
|
|
logger.severe("td count=" + fields.size()); |
58 |
|
|
if (fields.size() == 0) |
59 |
|
|
continue; |
60 |
|
|
|
61 |
|
|
for (int f=0; f<fields.size();f++) { |
62 |
|
|
logger.severe("" + f + "=" + fields.get(f).getAttribute("class") ); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
DepartureBean departure = new DepartureBean(); |
68 |
|
|
|
69 |
|
|
String time = fields.get(0).asText(); |
70 |
|
|
departure.setTime(time); |
71 |
|
|
|
72 |
|
|
int updated = 0; //updated not supported by rejseplanen |
73 |
|
|
departure.setUpdated(updated); |
74 |
|
|
|
75 |
|
|
String trainNumber = fields.get(1).asText(); |
76 |
|
|
departure.setTrainNumber(trainNumber); |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
//field 2 is the track number |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
String destination = fields.get(3). |
83 |
|
|
getElementsByTagName("div"). |
84 |
|
|
get(0). |
85 |
|
|
getElementsByAttribute("span", "class", "bold"). |
86 |
|
|
get(0). |
87 |
|
|
asText(); |
88 |
|
|
departure.setDestination(destination); |
89 |
|
|
|
90 |
|
|
//origin may take a bit more work |
91 |
|
|
//String origin = fields.get(4).asText(); |
92 |
|
|
//departure.setOrigin(origin); |
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
//rejseplanen doesn't support location |
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 |
|
|
departure.setType(type); |
106 |
|
|
*/ |
107 |
|
|
departureList.add(departure); |
108 |
|
|
|
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
} else { |
112 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
113 |
|
|
} |
114 |
|
|
webClient.closeAllWindows(); |
115 |
|
|
|
116 |
|
|
return departureList; |
117 |
|
|
} |
118 |
|
|
} |