1 |
torben |
847 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
/* Work in progress |
4 |
|
|
* |
5 |
|
|
*/ |
6 |
|
|
|
7 |
torben |
993 |
import java.net.URL; |
8 |
torben |
847 |
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 |
torben |
993 |
import org.jsoup.Jsoup; |
15 |
|
|
import org.jsoup.nodes.Document; |
16 |
|
|
import org.jsoup.nodes.Element; |
17 |
|
|
import org.jsoup.select.Elements; |
18 |
torben |
847 |
|
19 |
torben |
993 |
|
20 |
|
|
|
21 |
torben |
847 |
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 |
torben |
978 |
public List<DepartureEntry> lookupDepartures(int stationcode, String type, boolean arrival) throws Exception { |
32 |
torben |
847 |
|
33 |
torben |
978 |
List<DepartureEntry> departureList = new ArrayList<DepartureEntry>(); |
34 |
torben |
847 |
|
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 |
torben |
993 |
Document doc = Jsoup.parse(new URL(uri), 2500); |
40 |
|
|
|
41 |
torben |
847 |
|
42 |
|
|
|
43 |
torben |
993 |
Element table = doc.getElementById("hafasSqResults"); |
44 |
torben |
847 |
|
45 |
|
|
if (table != null) { |
46 |
torben |
993 |
Elements tableRows = table.getElementsByTag("tr"); |
47 |
torben |
847 |
|
48 |
torben |
993 |
for (Element currentRow : tableRows) { |
49 |
|
|
String id = currentRow.id(); |
50 |
torben |
847 |
logger.info("tr.id=" + id); |
51 |
torben |
993 |
String rowClass = currentRow.attr("class"); |
52 |
torben |
847 |
if (rowClass != null && rowClass.indexOf("sqToggleDetails") != -1 ) { |
53 |
torben |
993 |
Elements fields = currentRow.getElementsByTag("td"); |
54 |
torben |
847 |
|
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 |
torben |
993 |
logger.severe("" + f + "=" + fields.get(f).attr("class") ); |
61 |
torben |
847 |
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
torben |
978 |
DepartureEntry departure = new DepartureEntry(); |
66 |
torben |
847 |
|
67 |
torben |
993 |
String time = fields.get(0).text(); |
68 |
torben |
847 |
departure.setTime(time); |
69 |
|
|
|
70 |
|
|
int updated = 0; //updated not supported by rejseplanen |
71 |
|
|
departure.setUpdated(updated); |
72 |
|
|
|
73 |
torben |
993 |
String trainNumber = fields.get(1).text(); |
74 |
torben |
847 |
departure.setTrainNumber(trainNumber); |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
//field 2 is the track number |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
String destination = fields.get(3). |
81 |
torben |
993 |
getElementsByTag("div"). |
82 |
torben |
847 |
get(0). |
83 |
torben |
993 |
getElementsByClass("bold"). |
84 |
torben |
847 |
get(0). |
85 |
torben |
993 |
text(); |
86 |
torben |
847 |
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 |
torben |
993 |
|
113 |
torben |
847 |
|
114 |
|
|
return departureList; |
115 |
|
|
} |
116 |
|
|
} |