/[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 1037 by torben, Sat Sep 11 07:43:09 2010 UTC revision 1046 by torben, Tue Sep 14 05:33:30 2010 UTC
# Line 173  public class DepartureFetcher { Line 173  public class DepartureFetcher {
173                                                                    
174                                  String trainNumber = fields.get(2).text();                                  String trainNumber = fields.get(2).text();
175                                  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
176                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
177                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
178                                                                    
179                                  String destination = fields.get(3).text();                                  String destination = fields.get(3).text();
# Line 218  public class DepartureFetcher { Line 218  public class DepartureFetcher {
218              return departureBean;              return departureBean;
219          }          }
220                    
221            
222            
223            public static String cleanText(String input) {
224                    //apparently JSoup translates   characters on www.bane.dk to 0xA0
225                    return input.replace((char) 0xA0, (char)0x20).trim();
226            }
227            
228          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
229                                    
230                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
231                                    
232                  String type = getTypeStringWww(trainType);                  String type = getTypeStringWww(trainType);
233                                    
234                    stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
235                    
236                                                                                            
237              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;
238                logger.info("URI:" + uri);
239              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
240              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
241                            
# Line 234  public class DepartureFetcher { Line 244  public class DepartureFetcher {
244              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
245              Element table = page.getElementById(tableName);              Element table = page.getElementById(tableName);
246                            
247    
248                
249              if (table != null) {              if (table != null) {
250                      Elements tableRows =  table.getElementsByTag("tr");                      Elements tableRows =  table.getElementsByTag("tr");
251                                            
252                        boolean passedTidsstreg = false;
253                        boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
254                        
255                      for (Element currentRow : tableRows) {                      for (Element currentRow : tableRows) {
256                          String rowClass = currentRow.attr("class");                          String rowClass = currentRow.attr("class");
257                            
258                            if (tidsstregExists == true && passedTidsstreg == false) {
259                                    if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
260                                            passedTidsstreg = true;
261                                    } else {
262                                            continue;
263                                    }
264                            }
265                            
266                            
267                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
268                                  Elements fields = currentRow.getElementsByTag("td");                                  Elements fields = currentRow.getElementsByTag("td");
269                    
# Line 246  public class DepartureFetcher { Line 271  public class DepartureFetcher {
271                                                                    
272    
273                                                                    
274                                  String time = fields.get(0).getAllElements().get(2).text();                                  String time = cleanText( fields.get(0).getAllElements().get(2).text() );
275                                  if (time.equals(""))                                  if (time.equals(""))
276                                          time = "0:00"; //Bane.dk bug work-around                                          time = "0:00"; //Bane.dk bug work-around
277                                  departure.setTime(time);                                  departure.setTime(time);
# Line 254  public class DepartureFetcher { Line 279  public class DepartureFetcher {
279                                  int updated = extractUpdated( fields.get(1) );                                  int updated = extractUpdated( fields.get(1) );
280                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
281                                                                    
282                                  String trainNumber = fields.get(2).text();                                  String trainNumber = cleanText( fields.get(2).text() );
283                                  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
284                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
285                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
286                                                                    
287                                  String destination = fields.get(3).text();                                  String destination = cleanText( fields.get(3).text() );
288                                  departure.setDestination(destination);                                  departure.setDestination(destination);
289                                                                    
290                                  String origin = fields.get(4).text();                                  String origin = cleanText( fields.get(4).text() );
291                                  departure.setOrigin(origin);                                  departure.setOrigin(origin);
292                                                                    
293                                  String location = fields.get(5).text();                                  String location = cleanText( fields.get(5).text() );
294                                  departure.setLocation(location);                                  departure.setLocation(location);
295                                                                    
296                                  String status = fields.get(6).text().trim();                                  String status = cleanText( fields.get(6).text() );
297                                  departure.setStatus(status);                                  departure.setStatus(status);
298                                                                    
299                                  String note = extractNote( fields.get(7) );                                  String note = cleanText( extractNote( fields.get(7) ) );
300                                  departure.setNote(note);                                  departure.setNote(note);
301                                                                    
302                                  departure.setType(type);                                  departure.setType(type);
# Line 316  public class DepartureFetcher { Line 341  public class DepartureFetcher {
341                  if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')                  if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
342                          note = note.substring(0,note.length() -1 );                          note = note.substring(0,note.length() -1 );
343    
344                  return note;                  return note.trim();
345          }          }
346                    
347          private String extractTrainNumber(Element trainTd) {          private String extractTrainNumberAzure(Element trainTd) {
348                  Element anchorElement = trainTd.getElementsByTag("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
349                  String href = anchorElement.attr("href");                  String href = anchorElement.attr("href");
350                                    
# Line 329  public class DepartureFetcher { Line 354  public class DepartureFetcher {
354                  return number;                  return number;
355          }          }
356                    
357            private String extractTrainNumberWww(Element trainTd) {
358                    String number = "";
359                    Element anchorElement = trainTd.getElementsByTag("a").get(0);
360                    String href = anchorElement.attr("href");
361                    String argstring = href.substring( href.indexOf('?') + 1);
362                    
363                    String args[] = argstring.split("&");
364                    for (String arg : args) {
365                            String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
366                            
367                            if (pair[0].equalsIgnoreCase("TogNr"))
368                                    number = pair[1];
369                    }
370                    
371                    
372                    return number;
373            }
374    
375            
376          //test          //test
377          /*          /*
378          public static void main(String args[]) throws Exception {          public static void main(String args[]) throws Exception {

Legend:
Removed from v.1037  
changed lines
  Added in v.1046

  ViewVC Help
Powered by ViewVC 1.1.20