1 |
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 |
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 |
|
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 |
|
25 |
TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000); |
26 |
|
27 |
|
28 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
29 |
|
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 |
list = lookupTimetable(trainID,type); |
37 |
cache.put(key, list); |
38 |
} else { |
39 |
logger.info("Timetable: Cache hit " + trainID); |
40 |
} |
41 |
return list; |
42 |
} |
43 |
|
44 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
45 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
46 |
|
47 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
48 |
|
49 |
|
50 |
final WebClient webClient = new WebClient(); |
51 |
webClient.setTimeout(2500); |
52 |
webClient.setJavaScriptEnabled(false); |
53 |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
54 |
webClient.setCssEnabled(false); |
55 |
|
56 |
|
57 |
final HtmlPage page = webClient.getPage(url); |
58 |
|
59 |
|
60 |
boolean currentStation = false; |
61 |
boolean currentStationSaved = false; |
62 |
|
63 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
64 |
if (tables.size() == 1) { |
65 |
HtmlElement timetable = tables.get(0); |
66 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
67 |
|
68 |
for (int i=0; i<rows.size(); i++) { |
69 |
if (i==0) //First row is column headers |
70 |
continue; |
71 |
|
72 |
|
73 |
HtmlElement row = rows.get(i); |
74 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
75 |
|
76 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
77 |
currentStation = true; |
78 |
continue; |
79 |
} |
80 |
|
81 |
TimetableBean bean = new TimetableBean(); |
82 |
bean.setStation( fields.get(0).asText() ); |
83 |
bean.setArrival( fields.get(1).asText() ); |
84 |
bean.setDeparture( fields.get(2).asText() ); |
85 |
|
86 |
if (currentStation == true && currentStationSaved == false ) { |
87 |
bean.setCurrent(currentStation); |
88 |
currentStationSaved = true; |
89 |
} |
90 |
|
91 |
timetableList.add(bean); |
92 |
|
93 |
} |
94 |
|
95 |
} else { |
96 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
97 |
} |
98 |
|
99 |
return timetableList; |
100 |
} |
101 |
|
102 |
} |