Make things a little more resilient to weird station abbreviations from the BART API

This commit is contained in:
Doug Keen 2016-04-16 09:47:41 -07:00
parent b1618f734e
commit 9fbd5f90c4
4 changed files with 37 additions and 10 deletions

View File

@ -130,6 +130,23 @@ public enum Station {
} }
} }
public static Station getByApproximateName(String name) {
if (name == null) return null;
final String lowercaseName = name.toLowerCase();
for (Station station : Station.values()) {
if (lowercaseName.startsWith(station.name.toLowerCase())) {
return station;
}
}
for (Station station : Station.values()) {
if (lowercaseName.endsWith(station.name.toLowerCase())) {
return station;
}
}
return Station.SPCL;
}
public Station getInboundTransferStation() { public Station getInboundTransferStation() {
return getByAbbreviation(inboundTransferStation); return getByAbbreviation(inboundTransferStation);
} }

View File

@ -27,7 +27,7 @@ public class EtdContentHandler extends DefaultHandler {
private final static List<String> TAGS = Arrays.asList("date", "time", private final static List<String> TAGS = Arrays.asList("date", "time",
"abbreviation", "minutes", "platform", "direction", "length", "abbreviation", "minutes", "platform", "direction", "length",
"color", "hexcolor", "bikeflag"); "color", "hexcolor", "bikeflag", "destination");
private RealTimeDepartures realTimeDepartures; private RealTimeDepartures realTimeDepartures;
@ -36,7 +36,8 @@ public class EtdContentHandler extends DefaultHandler {
} }
private String date; private String date;
private String currentDestination; private String currentDestinationAbbreviation;
private String currentDestinationName;
private String currentValue; private String currentValue;
private Departure currentDeparture; private Departure currentDeparture;
private boolean isParsingTag; private boolean isParsingTag;
@ -57,8 +58,11 @@ public class EtdContentHandler extends DefaultHandler {
} }
if (localName.equals("estimate")) { if (localName.equals("estimate")) {
currentDeparture = new Departure(); currentDeparture = new Departure();
currentDeparture.setTrainDestination(Station Station destination = Station.getByAbbreviation(currentDestinationAbbreviation);
.getByAbbreviation(currentDestination)); if (destination == null) {
destination = Station.getByApproximateName(currentDestinationName);
}
currentDeparture.setTrainDestination(destination);
currentDeparture.setOrigin(realTimeDepartures.getOrigin()); currentDeparture.setOrigin(realTimeDepartures.getOrigin());
} }
} }
@ -71,7 +75,9 @@ public class EtdContentHandler extends DefaultHandler {
} else if (localName.equals("time")) { } else if (localName.equals("time")) {
realTimeDepartures.setTime(Date.parse(date + " " + currentValue)); realTimeDepartures.setTime(Date.parse(date + " " + currentValue));
} else if (localName.equals("abbreviation")) { } else if (localName.equals("abbreviation")) {
currentDestination = currentValue; currentDestinationAbbreviation = currentValue;
} else if (localName.equals("destination")) {
currentDestinationName = currentValue;
} else if (localName.equals("minutes")) { } else if (localName.equals("minutes")) {
if (StringUtils.isNumeric(currentValue)) { if (StringUtils.isNumeric(currentValue)) {
currentDeparture.setMinutes(Integer.parseInt(currentValue)); currentDeparture.setMinutes(Integer.parseInt(currentValue));
@ -109,10 +115,14 @@ public class EtdContentHandler extends DefaultHandler {
} else if (localName.equals("bikeflag")) { } else if (localName.equals("bikeflag")) {
currentDeparture.setBikeAllowed(currentValue.equalsIgnoreCase("1")); currentDeparture.setBikeAllowed(currentValue.equalsIgnoreCase("1"));
} else if (localName.equals("estimate")) { } else if (localName.equals("estimate")) {
// If we can't infer a real destination because of weird API data,
// just skip it ¯\_()_/¯
if (realTimeDepartures.getDestination() != null) {
realTimeDepartures.addDeparture(currentDeparture); realTimeDepartures.addDeparture(currentDeparture);
}
currentDeparture = null; currentDeparture = null;
} else if (localName.equals("etd")) { } else if (localName.equals("etd")) {
currentDestination = null; currentDestinationAbbreviation = null;
} else if (localName.equals("station")) { } else if (localName.equals("station")) {
realTimeDepartures.finalizeDeparturesList(); realTimeDepartures.finalizeDeparturesList();
} }

View File

@ -4,7 +4,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:1.3.0' classpath 'com.android.tools.build:gradle:2.0.0'
// Used for annotation processing needed by the AndroidAnnotations library // Used for annotation processing needed by the AndroidAnnotations library
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
} }

View File

@ -1,6 +1,6 @@
#Mon Jul 20 14:31:30 PDT 2015 #Sat Apr 16 08:57:22 PDT 2016
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip