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 |
1061 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
20 |
torben |
993 |
|
21 |
|
|
|
22 |
torben |
1061 |
|
23 |
torben |
847 |
public class RejseplanenFetcher { |
24 |
|
|
|
25 |
|
|
Logger logger = Logger.getLogger(RejseplanenFetcher.class.getName()); |
26 |
|
|
|
27 |
|
|
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); |
28 |
|
|
|
29 |
|
|
public String getNow() { |
30 |
|
|
return format.format( new Date() ); |
31 |
|
|
} |
32 |
|
|
|
33 |
torben |
978 |
public List<DepartureEntry> lookupDepartures(int stationcode, String type, boolean arrival) throws Exception { |
34 |
torben |
847 |
|
35 |
torben |
978 |
List<DepartureEntry> departureList = new ArrayList<DepartureEntry>(); |
36 |
torben |
847 |
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
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"; |
40 |
|
|
|
41 |
torben |
993 |
Document doc = Jsoup.parse(new URL(uri), 2500); |
42 |
|
|
|
43 |
torben |
847 |
|
44 |
|
|
|
45 |
torben |
993 |
Element table = doc.getElementById("hafasSqResults"); |
46 |
torben |
847 |
|
47 |
|
|
if (table != null) { |
48 |
torben |
993 |
Elements tableRows = table.getElementsByTag("tr"); |
49 |
torben |
847 |
|
50 |
torben |
993 |
for (Element currentRow : tableRows) { |
51 |
|
|
String id = currentRow.id(); |
52 |
torben |
847 |
logger.info("tr.id=" + id); |
53 |
torben |
993 |
String rowClass = currentRow.attr("class"); |
54 |
torben |
847 |
if (rowClass != null && rowClass.indexOf("sqToggleDetails") != -1 ) { |
55 |
torben |
993 |
Elements fields = currentRow.getElementsByTag("td"); |
56 |
torben |
847 |
|
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 |
torben |
993 |
logger.severe("" + f + "=" + fields.get(f).attr("class") ); |
63 |
torben |
847 |
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
torben |
978 |
DepartureEntry departure = new DepartureEntry(); |
68 |
torben |
847 |
|
69 |
torben |
993 |
String time = fields.get(0).text(); |
70 |
torben |
847 |
departure.setTime(time); |
71 |
|
|
|
72 |
|
|
int updated = 0; //updated not supported by rejseplanen |
73 |
|
|
departure.setUpdated(updated); |
74 |
|
|
|
75 |
torben |
993 |
String trainNumber = fields.get(1).text(); |
76 |
torben |
847 |
departure.setTrainNumber(trainNumber); |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
//field 2 is the track number |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
String destination = fields.get(3). |
83 |
torben |
993 |
getElementsByTag("div"). |
84 |
torben |
847 |
get(0). |
85 |
torben |
993 |
getElementsByClass("bold"). |
86 |
torben |
847 |
get(0). |
87 |
torben |
993 |
text(); |
88 |
torben |
847 |
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 |
torben |
993 |
|
115 |
torben |
847 |
|
116 |
|
|
return departureList; |
117 |
|
|
} |
118 |
|
|
} |