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