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

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1038 by torben, Sun Sep 12 18:41:17 2010 UTC revision 1188 by torben, Mon Nov 8 12:39:27 2010 UTC
# Line 11  import org.jsoup.nodes.Document; Line 11  import org.jsoup.nodes.Document;
11  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
12  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
13    
14    import dk.thoerup.android.traininfo.common.DepartureBean;
15    import dk.thoerup.android.traininfo.common.DepartureEntry;
16    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
 import dk.thoerup.traininfoservice.StationBean;  
19  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.traininfoservice.StationDAO;
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    
# Line 63  public class DepartureFetcher { Line 65  public class DepartureFetcher {
65                                    
66                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
67                                    
68                  StationBean station = stationDao.getById(stationID);                  StationEntry station = stationDao.getById(stationID);
69                                    
70                  departureBean.stationName = station.getName();                  departureBean.stationName = station.getName();
71                                    
72                  if (station.getRegional() != null) {                  if (station.getRegional() != null) {
73                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
74                          departureBean.departureEntries.addAll( tempBean.departureEntries );                          departureBean.entries.addAll( tempBean.entries );
75                          departureBean.notifications.addAll(tempBean.notifications);                          departureBean.notifications.addAll(tempBean.notifications);
76                  }                  }
77                                    
78                  if (station.getStrain() != null) {                  if (station.getStrain() != null) {
79                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
80                          departureBean.departureEntries.addAll( tempBean.departureEntries );                          departureBean.entries.addAll( tempBean.entries );
81                          departureBean.notifications.addAll(tempBean.notifications);                          departureBean.notifications.addAll(tempBean.notifications);
82                  }                                }              
83                                    
84                  if (departureBean.departureEntries.size() == 0) {                  if (departureBean.entries.size() == 0) {
85                          logger.info("No departures found for station " + stationID);                          logger.info("No departures found for station " + stationID);
86                  }                  }
87                                    
88                  Collections.sort( departureBean.departureEntries );                  Collections.sort( departureBean.entries );
89    
90                                    
91                  return departureBean;                  return departureBean;
# Line 131  public class DepartureFetcher { Line 133  public class DepartureFetcher {
133    
134              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                      String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";        
135                            
136              //logger.info("URI: " + uri);                        logger.fine("URI: " + uri);    
137              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
138              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
139                            
# Line 143  public class DepartureFetcher { Line 145  public class DepartureFetcher {
145              if (table != null) {              if (table != null) {
146                      Elements tableRows =  table.getElementsByTag("tr");                      Elements tableRows =  table.getElementsByTag("tr");
147                                            
148                      boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);                      //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
149                      boolean passedTidsstreg = false;                      //boolean passedTidsstreg = false;
150                                            
151                      for (Element currentRow : tableRows) {                      for (Element currentRow : tableRows) {
152                          String rowClass = currentRow.attr("class");                          String rowClass = currentRow.attr("class");
153                                                    /*
154                          if (tidsstregExists == true && passedTidsstreg == false) {                          if (tidsstregExists == true && passedTidsstreg == false) {
155                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
156                                          passedTidsstreg = true;                                          passedTidsstreg = true;
157                                  } else {                                  } else {
158                                          continue;                                          continue;
159                                  }                                  }
160                          }                          }*/
161                                                    
162                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
163                                                                    
# Line 173  public class DepartureFetcher { Line 175  public class DepartureFetcher {
175                                                                    
176                                  String trainNumber = fields.get(2).text();                                  String trainNumber = fields.get(2).text();
177                                  if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber                                  if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
178                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
179                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
180                                                                    
181                                  String destination = fields.get(3).text();                                  String destination = fields.get(3).text();
# Line 193  public class DepartureFetcher { Line 195  public class DepartureFetcher {
195                                                                    
196                                  departure.setType(typeString);                                  departure.setType(typeString);
197                                                                    
198                                  departureBean.departureEntries.add( departure );                                  departureBean.entries.add( departure );
199                          }                          }
200                      }                      }
201              } else {              } else {
# Line 220  public class DepartureFetcher { Line 222  public class DepartureFetcher {
222                    
223                    
224                    
225          String cleanText(String input) {          public static String cleanText(String input) {
226                  //apparently JSoup interprets some of space characters on www.bane.dk as 0xA0                  //apparently JSoup translates   characters on www.bane.dk to 0xA0
227                  return input.replace((char) 0xA0, (char)0x20).trim();                  return input.replace((char) 0xA0, (char)0x20).trim();
228          }          }
229                    
# Line 231  public class DepartureFetcher { Line 233  public class DepartureFetcher {
233                                    
234                  String type = getTypeStringWww(trainType);                  String type = getTypeStringWww(trainType);
235                                    
236                    stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
237                    
238                                                                                            
239              String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;              String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
240                logger.fine("URI:" + uri);
241                
242    
243              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
244              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
245                            
# Line 241  public class DepartureFetcher { Line 248  public class DepartureFetcher {
248              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
249              Element table = page.getElementById(tableName);              Element table = page.getElementById(tableName);
250                            
251    
252                
253              if (table != null) {              if (table != null) {
254                      Elements tableRows =  table.getElementsByTag("tr");                      Elements tableRows =  table.getElementsByTag("tr");
255                                            
256                        //boolean passedTidsstreg = false;
257                        //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
258                        
259                      for (Element currentRow : tableRows) {                      for (Element currentRow : tableRows) {
260                          String rowClass = currentRow.attr("class");                          String rowClass = currentRow.attr("class");
261                            /*
262                            if (tidsstregExists == true && passedTidsstreg == false) {
263                                    if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
264                                            passedTidsstreg = true;
265                                    } else {
266                                            continue;
267                                    }
268                            }*/
269                            
270                            
271                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
272                                  Elements fields = currentRow.getElementsByTag("td");                                  Elements fields = currentRow.getElementsByTag("td");
273                    
# Line 263  public class DepartureFetcher { Line 285  public class DepartureFetcher {
285                                                                    
286                                  String trainNumber = cleanText( fields.get(2).text() );                                  String trainNumber = cleanText( fields.get(2).text() );
287                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
288                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
289                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
290                                                                    
291                                  String destination = cleanText( fields.get(3).text() );                                  String destination = cleanText( fields.get(3).text() );
# Line 283  public class DepartureFetcher { Line 305  public class DepartureFetcher {
305                                                                    
306                                  departure.setType(type);                                  departure.setType(type);
307                                                                    
308                                  departureBean.departureEntries.add(departure);                                  departureBean.entries.add(departure);
309                                                                    
310                                                                    
311                          }                          }
# Line 326  public class DepartureFetcher { Line 348  public class DepartureFetcher {
348                  return note.trim();                  return note.trim();
349          }          }
350                    
351          private String extractTrainNumber(Element trainTd) {          private String extractTrainNumberAzure(Element trainTd) {
352                  Element anchorElement = trainTd.getElementsByTag("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
353                  String href = anchorElement.attr("href");                  String href = anchorElement.attr("href");
354                                    
# Line 336  public class DepartureFetcher { Line 358  public class DepartureFetcher {
358                  return number;                  return number;
359          }          }
360                    
361            private String extractTrainNumberWww(Element trainTd) {
362                    String number = "";
363                    Element anchorElement = trainTd.getElementsByTag("a").get(0);
364                    String href = anchorElement.attr("href");
365                    String argstring = href.substring( href.indexOf('?') + 1);
366                    
367                    String args[] = argstring.split("&");
368                    for (String arg : args) {
369                            String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
370                            
371                            if (pair[0].equalsIgnoreCase("TogNr"))
372                                    number = pair[1];
373                    }
374                    
375                    
376                    return number;
377            }
378    
379            
380          //test          //test
381          /*          /*
382          public static void main(String args[]) throws Exception {          public static void main(String args[]) throws Exception {

Legend:
Removed from v.1038  
changed lines
  Added in v.1188

  ViewVC Help
Powered by ViewVC 1.1.20