/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 992 - (show annotations) (download)
Wed Jul 14 08:05:31 2010 UTC (13 years, 10 months ago) by torben
File size: 5030 byte(s)
Experiment: switch to jsoup instead of htmlunit
1 package dk.thoerup.traininfoservice.banedk;
2
3
4
5 import java.net.URL;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13 import org.jsoup.nodes.Document;
14 import org.jsoup.nodes.Element;
15 import org.jsoup.select.Elements;
16
17 import dk.thoerup.circuitbreaker.CircuitBreaker;
18 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19 import dk.thoerup.traininfoservice.StationDAO;
20 import dk.thoerup.traininfoservice.Statistics;
21
22 public class TimetableFetcher {
23
24
25 Map<String, List<TimetableBean>> cache;
26 Map<String, Integer> stationCache;
27
28 StationDAO stationDao = new StationDAO();
29
30
31 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
32
33 private boolean useTempSite;
34
35 public TimetableFetcher(boolean tmpSite, int cacheTimeout) {
36 useTempSite = tmpSite;
37
38 cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);
39 stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
40 }
41
42
43 List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
44 String key = trainID+type;
45 List<TimetableBean> list = cache.get(key);
46
47 if (list == null) {
48 list = lookupTimetable(trainID,type);
49 cache.put(key, list);
50 } else {
51 Statistics.getInstance().incrementTimetableCacheHits();
52 logger.info("Timetable: Cache hit " + trainID);
53 }
54 return list;
55 }
56
57 List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
58 if (useTempSite == false ){
59 return lookupTimetableRealSite(trainID, type);
60 } else {
61 return new ArrayList<TimetableBean>(); // no timetable data on temp site
62 }
63 }
64
65 int getStationId(String name) {
66 Integer id = stationCache.get(name);
67
68 if (id == null) {
69 try {
70 id = stationDao.getIdByName(name);
71 stationCache.put(name, id);
72 } catch (SQLException e) {
73 logger.log(Level.SEVERE, "getStationId failed", e);
74 id = -1;
75 }
76 }
77
78 return id;
79 }
80
81 List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {
82 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
83
84 //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
85 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;
86
87
88 JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , 2500);
89 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
90
91 Document doc = (Document) breaker.invoke(wrapper);
92
93
94 boolean currentStation = false;
95 boolean currentStationSaved = false;
96
97 Elements tables = doc.getElementsByClass("Rute");
98
99 if (tables.size() == 1) {
100 Element timetable = tables.get(0);
101 Elements rows = timetable.getElementsByTag("tr");
102
103 for (int i=0; i<rows.size(); i++) {
104 if (i==0) //First row is column headers
105 continue;
106
107
108 Element row = rows.get(i);
109 Elements fields = row.getElementsByTag("td");
110
111
112 if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
113 currentStation = true;
114 continue;
115 }
116
117 TimetableBean bean = new TimetableBean();
118
119 String station = fields.get(0).text() ;
120 if (station.equals("København"))
121 station = "København H"; //correct inconsistency in naming
122
123 bean.setStation( station );
124 bean.setArrival( fields.get(1).text() );
125 bean.setDeparture( fields.get(2).text() );
126
127 boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
128 bean.setCancelled(cancelled);
129
130 if (currentStation == true && currentStationSaved == false ) {
131 bean.setCurrent(currentStation);
132 currentStationSaved = true;
133 }
134
135 bean.setStationId( getStationId( station ));
136
137 timetableList.add(bean);
138 }
139
140 //TODO: There is an off-by-one error in this cancelled parser thingie
141 final String cancelledString = "Aflyst";
142 for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels
143 final int lastIdx = (timetableList.size() - 1);
144
145 TimetableBean current = timetableList.get(i);
146 if (current.isCancelled()) {
147 if (i == 0) {
148 current.setDeparture(cancelledString);
149 } else if (i == lastIdx) {
150 current.setArrival(cancelledString);
151 } else if (i>0 && i<lastIdx) {
152 TimetableBean next = timetableList.get(i+1);
153 TimetableBean prev = timetableList.get(i-1);
154
155 if (next.isCancelled())
156 current.setDeparture(cancelledString);
157 if (prev.isCancelled())
158 current.setArrival(cancelledString);
159 }
160 }
161 }
162
163 } else {
164 logger.warning("No time table found, trainID=" + trainID + " type=" + type);
165 }
166
167
168 return timetableList;
169 }
170
171 }

  ViewVC Help
Powered by ViewVC 1.1.20