1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
/* Work in progress |
4 |
* |
5 |
*/ |
6 |
|
7 |
import java.net.URL; |
8 |
import java.text.SimpleDateFormat; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Date; |
11 |
import java.util.List; |
12 |
import java.util.logging.Logger; |
13 |
|
14 |
import org.jsoup.Jsoup; |
15 |
import org.jsoup.nodes.Document; |
16 |
import org.jsoup.nodes.Element; |
17 |
import org.jsoup.select.Elements; |
18 |
|
19 |
|
20 |
|
21 |
public class RejseplanenFetcher { |
22 |
|
23 |
Logger logger = Logger.getLogger(RejseplanenFetcher.class.getName()); |
24 |
|
25 |
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); |
26 |
|
27 |
public String getNow() { |
28 |
return format.format( new Date() ); |
29 |
} |
30 |
|
31 |
public List<DepartureEntry> lookupDepartures(int stationcode, String type, boolean arrival) throws Exception { |
32 |
|
33 |
List<DepartureEntry> departureList = new ArrayList<DepartureEntry>(); |
34 |
|
35 |
|
36 |
|
37 |
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"; |
38 |
|
39 |
Document doc = Jsoup.parse(new URL(uri), 2500); |
40 |
|
41 |
|
42 |
|
43 |
Element table = doc.getElementById("hafasSqResults"); |
44 |
|
45 |
if (table != null) { |
46 |
Elements tableRows = table.getElementsByTag("tr"); |
47 |
|
48 |
for (Element currentRow : tableRows) { |
49 |
String id = currentRow.id(); |
50 |
logger.info("tr.id=" + id); |
51 |
String rowClass = currentRow.attr("class"); |
52 |
if (rowClass != null && rowClass.indexOf("sqToggleDetails") != -1 ) { |
53 |
Elements fields = currentRow.getElementsByTag("td"); |
54 |
|
55 |
logger.severe("td count=" + fields.size()); |
56 |
if (fields.size() == 0) |
57 |
continue; |
58 |
|
59 |
for (int f=0; f<fields.size();f++) { |
60 |
logger.severe("" + f + "=" + fields.get(f).attr("class") ); |
61 |
} |
62 |
|
63 |
|
64 |
|
65 |
DepartureEntry departure = new DepartureEntry(); |
66 |
|
67 |
String time = fields.get(0).text(); |
68 |
departure.setTime(time); |
69 |
|
70 |
int updated = 0; //updated not supported by rejseplanen |
71 |
departure.setUpdated(updated); |
72 |
|
73 |
String trainNumber = fields.get(1).text(); |
74 |
departure.setTrainNumber(trainNumber); |
75 |
|
76 |
|
77 |
//field 2 is the track number |
78 |
|
79 |
|
80 |
String destination = fields.get(3). |
81 |
getElementsByTag("div"). |
82 |
get(0). |
83 |
getElementsByClass("bold"). |
84 |
get(0). |
85 |
text(); |
86 |
departure.setDestination(destination); |
87 |
|
88 |
//origin may take a bit more work |
89 |
//String origin = fields.get(4).asText(); |
90 |
//departure.setOrigin(origin); |
91 |
|
92 |
/* |
93 |
//rejseplanen doesn't support location |
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 |
departure.setType(type); |
104 |
*/ |
105 |
departureList.add(departure); |
106 |
|
107 |
} |
108 |
} |
109 |
} else { |
110 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
111 |
} |
112 |
|
113 |
|
114 |
return departureList; |
115 |
} |
116 |
} |