diff --git a/src/it/reyboz/bustorino/backend/FiveTStopsFetcher.java b/src/it/reyboz/bustorino/backend/FiveTStopsFetcher.java index 3e574ab..2171196 100644 --- a/src/it/reyboz/bustorino/backend/FiveTStopsFetcher.java +++ b/src/it/reyboz/bustorino/backend/FiveTStopsFetcher.java @@ -1,114 +1,115 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** * Once was asynchronous BusStop[] fetcher from a query, code mostly taken from * AsyncWgetBusStopSuggestions (by Valerio Bozzolan) * * @see FiveTScraperFetcher */ public class FiveTStopsFetcher implements StopsFinderByName { @Override public List FindByName(String name, AtomicReference res) { - // TODO: limit? - ArrayList busStops = new ArrayList<>(15); + // API apparently limited to 20 results + ArrayList busStops = new ArrayList<>(20); String stopID; String stopName; String stopLocation; //Stop busStop; if(name.length() < 3) { res.set(result.QUERY_TOO_SHORT); return busStops; } String responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas; URL u; try { u = new URL("http://www.5t.torino.it/5t/trasporto/stop-lookup.jsp?action=search&stopShortName=" + URLEncoder.encode(name, "utf-8")); } catch(Exception e) { res.set(Fetcher.result.PARSER_ERROR); return busStops; } responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = networkTools.getDOM(u, res); if (responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas == null) { // result already set in getDOM() return busStops; } Document doc = Jsoup.parse(responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas); // Find bus stops Elements lis = doc.getElementsByTag("li"); for(Element li : lis) { Elements spans = li.getElementsByTag("span"); // busStopID try { stopID = FiveTNormalizer.FiveTNormalizeRoute(spans.eq(0).text()); } catch(Exception e) { //Log.e("Suggestions", "Empty busStopID"); stopID = ""; } // busStopName try { stopName = spans.eq(1).text(); } catch(Exception e) { //Log.e("Suggestions", "Empty busStopName"); stopName = ""; } // busStopLocation try { stopLocation = (spans.eq(2).text()); } catch(Exception e) { //Log.e("Suggestions", "Empty busStopLocation"); stopLocation = null; } busStops.add(new Stop(stopName, stopID, stopLocation, null)); } if(busStops.size() == 0) { res.set(result.EMPTY_RESULT_SET); } else { res.set(result.OK); } Collections.sort(busStops); + // TODO: remove duplicates? (see GTTStopsFetcher) return busStops; } } diff --git a/src/it/reyboz/bustorino/backend/Passaggio.java b/src/it/reyboz/bustorino/backend/Passaggio.java index e4dadd4..75d4bca 100644 --- a/src/it/reyboz/bustorino/backend/Passaggio.java +++ b/src/it/reyboz/bustorino/backend/Passaggio.java @@ -1,89 +1,105 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import android.support.annotation.NonNull; -public final class Passaggio implements Comparable { - public final String hh; - public final String mm; - public final boolean isInRealTime; +public final class Passaggio /*implements Comparable*/ { +// public final String hh; +// public final String mm; +// public final boolean isInRealTime; + private final String passaggio; /** - * Constructs a time (passaggio) for the timetable. + * Useless constructor. * * @param TimeGTT time in GTT format (e.g. "11:22*"), already trimmed from whitespace. - * @throws IllegalArgumentException if nothing reasonable can be extracted from the string */ public Passaggio(@NonNull String TimeGTT) { - String[] parts = TimeGTT.split(":"); - if(parts.length != 2) { - //throw new IllegalArgumentException("The string " + TimeGTT + " doesn't follow the sacred format of time according to GTT!"); - this.hh = "??"; - this.mm = "??"; - this.isInRealTime = false; - return; - } - this.hh = parts[0]; - if(parts[1].endsWith("*")) { - this.mm = parts[1].substring(0, parts[1].length() - 1); - this.isInRealTime = true; - } else { - this.mm = parts[1]; - this.isInRealTime = false; - } - } - - @Override public int compareTo(@NonNull Passaggio altro) { - int diff; - - diff = networkTools.failsafeParseInt(altro.hh) - networkTools.failsafeParseInt(this.hh); - - // an attempt to correctly sortRoutes times around midnight (e.g. 23.59 should come before 00.01) - if(diff > 12) { // TODO: see if this works in practice - diff = -(24 - diff); - } else if(diff < -12) { - diff = -(diff + 24); - } - - diff *= 60; - - diff += networkTools.failsafeParseInt(altro.mm) - networkTools.failsafeParseInt(this.mm); - - // we should take into account if one is in real time and the other isn't, shouldn't we? - if(altro.isInRealTime) { - ++diff; - } - if(this.isInRealTime) { - --diff; - } - - return diff; + this.passaggio = TimeGTT; } @Override public String toString() { - String resultString = (this.hh).concat(":").concat(this.mm); - if(this.isInRealTime) { - return resultString.concat("*"); - } else { - return resultString; - } + return this.passaggio; } + + +// /** +// * Constructs a time (passaggio) for the timetable. +// * +// * @param TimeGTT time in GTT format (e.g. "11:22*"), already trimmed from whitespace. +// * @throws IllegalArgumentException if nothing reasonable can be extracted from the string +// */ +// public Passaggio(@NonNull String TimeGTT) { +// String[] parts = TimeGTT.split(":"); +// if(parts.length != 2) { +// //throw new IllegalArgumentException("The string " + TimeGTT + " doesn't follow the sacred format of time according to GTT!"); +// this.hh = "??"; +// this.mm = "??"; +// this.isInRealTime = false; +// return; +// } +// this.hh = parts[0]; +// if(parts[1].endsWith("*")) { +// this.mm = parts[1].substring(0, parts[1].length() - 1); +// this.isInRealTime = true; +// } else { +// this.mm = parts[1]; +// this.isInRealTime = false; +// } +// } +// +// @Override public int compareTo(@NonNull Passaggio altro) { +// int diff; +// +// diff = networkTools.failsafeParseInt(altro.hh) - networkTools.failsafeParseInt(this.hh); +// +// // an attempt to correctly sortRoutes times around midnight (e.g. 23.59 should come before 00.01) +// if(diff > 12) { // untested +// diff = -(24 - diff); +// } else if(diff < -12) { +// diff = -(diff + 24); +// } +// +// diff *= 60; +// +// diff += networkTools.failsafeParseInt(altro.mm) - networkTools.failsafeParseInt(this.mm); +// +// // we should take into account if one is in real time and the other isn't, shouldn't we? +// if(altro.isInRealTime) { +// ++diff; +// } +// if(this.isInRealTime) { +// --diff; +// } +// +// return diff; +// } +// +// @Override +// public String toString() { +// String resultString = (this.hh).concat(":").concat(this.mm); +// if(this.isInRealTime) { +// return resultString.concat("*"); +// } else { +// return resultString; +// } +// } } \ No newline at end of file