1 |
torben |
350 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import java.io.IOException; |
5 |
|
|
import java.net.URL; |
6 |
|
|
import java.util.ArrayList; |
7 |
|
|
import java.util.List; |
8 |
|
|
import java.util.logging.Logger; |
9 |
|
|
|
10 |
torben |
387 |
import com.gargoylesoftware.htmlunit.Page; |
11 |
|
|
import com.gargoylesoftware.htmlunit.RefreshHandler; |
12 |
|
|
import com.gargoylesoftware.htmlunit.WebClient; |
13 |
|
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
14 |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
15 |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
16 |
torben |
350 |
|
17 |
|
|
public class TimetableFetcher { |
18 |
|
|
|
19 |
|
|
class NullRefreshHandler implements RefreshHandler { |
20 |
|
|
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
} |
24 |
torben |
387 |
|
25 |
|
|
TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000); |
26 |
torben |
350 |
|
27 |
torben |
387 |
|
28 |
torben |
350 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
29 |
torben |
387 |
|
30 |
|
|
|
31 |
|
|
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
32 |
|
|
String key = trainID+type; |
33 |
|
|
List<TimetableBean> list = cache.get(key); |
34 |
|
|
|
35 |
|
|
if (list == null) { |
36 |
|
|
logger.warning("Timetable: Cache miss " + trainID); //remove before production |
37 |
|
|
list = lookupTimetable(trainID,type); |
38 |
|
|
cache.put(key, list); |
39 |
|
|
} else { |
40 |
|
|
logger.warning("Timetable: Cache hit " + trainID); //remove before production |
41 |
|
|
} |
42 |
|
|
return list; |
43 |
|
|
} |
44 |
torben |
350 |
|
45 |
torben |
387 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
46 |
torben |
350 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
47 |
|
|
|
48 |
|
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
final WebClient webClient = new WebClient(); |
52 |
torben |
386 |
webClient.setTimeout(2500); |
53 |
torben |
350 |
webClient.setJavaScriptEnabled(false); |
54 |
|
|
webClient.setRefreshHandler( new NullRefreshHandler() ); |
55 |
|
|
webClient.setCssEnabled(false); |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
final HtmlPage page = webClient.getPage(url); |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
boolean currentStation = false; |
62 |
|
|
boolean currentStationSaved = false; |
63 |
|
|
|
64 |
|
|
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
65 |
|
|
if (tables.size() == 1) { |
66 |
|
|
HtmlElement timetable = tables.get(0); |
67 |
|
|
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
68 |
|
|
|
69 |
|
|
for (int i=0; i<rows.size(); i++) { |
70 |
|
|
if (i==0) //First row is column headers |
71 |
|
|
continue; |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
HtmlElement row = rows.get(i); |
75 |
|
|
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
76 |
|
|
|
77 |
|
|
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
78 |
|
|
currentStation = true; |
79 |
|
|
continue; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
TimetableBean bean = new TimetableBean(); |
83 |
|
|
bean.setStation( fields.get(0).asText() ); |
84 |
|
|
bean.setArrival( fields.get(1).asText() ); |
85 |
|
|
bean.setDeparture( fields.get(2).asText() ); |
86 |
|
|
|
87 |
|
|
if (currentStation == true && currentStationSaved == false ) { |
88 |
|
|
bean.setCurrent(currentStation); |
89 |
|
|
currentStationSaved = true; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
timetableList.add(bean); |
93 |
|
|
|
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
} else { |
97 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
return timetableList; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
} |