Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/FiveTScraperFetcher.java
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | private static String grep(String needle, String haystack) { | ||||
needle).matcher(haystack); | needle).matcher(haystack); | ||||
if (matcher.find()) { | if (matcher.find()) { | ||||
matched = matcher.group(1); | matched = matcher.group(1); | ||||
} | } | ||||
return matched; | return matched; | ||||
} | } | ||||
@Override | @Override | ||||
public Palina ReadArrivalTimesAll(final String stopID, final AtomicReference<result> res) { | public Palina ReadArrivalTimesAll(final String stopID, final AtomicReference<Result> res) { | ||||
Palina p = new Palina(stopID); | Palina p = new Palina(stopID); | ||||
int routeIndex; | int routeIndex; | ||||
String responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = null; | String responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = null; | ||||
try { | try { | ||||
responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = networkTools.getDOM(new URL("http://www.5t.torino.it/5t/trasporto/arrival-times-byline.jsp?action=getTransitsByLine&shortName=" + URLEncoder.encode(stopID, "utf-8")), res); | responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = networkTools.getDOM(new URL("http://www.5t.torino.it/5t/trasporto/arrival-times-byline.jsp?action=getTransitsByLine&shortName=" + URLEncoder.encode(stopID, "utf-8")), res); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
} | } | ||||
if(responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas == null) { | if(responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas == null) { | ||||
// result already set in getDOM() | // result already set in getDOM() | ||||
return p; | return p; | ||||
} | } | ||||
Document doc = Jsoup.parse(responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas); | Document doc = Jsoup.parse(responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas); | ||||
// Tried in rete Edisu (it does Man In The Middle... asd) | // Tried in rete Edisu (it does Man In The Middle... asd) | ||||
Element span = doc.select("span").first(); | Element span = doc.select("span").first(); | ||||
if(span == null) { | if(span == null) { | ||||
res.set(result.SERVER_ERROR); | res.set(Result.SERVER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
String busStopID = grep("^(.+) ", span.html()); | String busStopID = grep("^(.+) ", span.html()); | ||||
if (busStopID == null) { | if (busStopID == null) { | ||||
//Log.e("BusStop", "Empty busStopID from " + span.html()); | //Log.e("BusStop", "Empty busStopID from " + span.html()); | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
return p; | return p; | ||||
} | } | ||||
// this also appears when no stops are found, but that case has already been handled above | // this also appears when no stops are found, but that case has already been handled above | ||||
Element error = doc.select("p.errore").first(); | Element error = doc.select("p.errore").first(); | ||||
if (error != null) { | if (error != null) { | ||||
res.set(result.SERVER_ERROR); | res.set(Result.SERVER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
String busStopName = grep("^.+ (.+)", span.html()); // The first "dot" is the single strange space character in the middle of "39{HERE→} {←HERE}PORTA NUOVA" | String busStopName = grep("^.+ (.+)", span.html()); // The first "dot" is the single strange space character in the middle of "39{HERE→} {←HERE}PORTA NUOVA" | ||||
if (busStopName == null) { | if (busStopName == null) { | ||||
//Log.e("BusStop", "Empty busStopName from " + span.html()); | //Log.e("BusStop", "Empty busStopName from " + span.html()); | ||||
res.set(result.SERVER_ERROR); | res.set(Result.SERVER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
p.setStopName(busStopName.trim()); | p.setStopName(busStopName.trim()); | ||||
// Every table row is a busLine | // Every table row is a busLine | ||||
Elements trs = doc.select("table tr"); | Elements trs = doc.select("table tr"); | ||||
for (Element tr : trs) { | for (Element tr : trs) { | ||||
Element line = tr.select("td.line a").first(); | Element line = tr.select("td.line a").first(); | ||||
if (!line.hasText()) { | if (!line.hasText()) { | ||||
res.set(result.SERVER_ERROR); | res.set(Result.SERVER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
String busLineName = line.text(); | String busLineName = line.text(); | ||||
// this is yet another ID, that has no known use so we can safely ignore it | // this is yet another ID, that has no known use so we can safely ignore it | ||||
// Integer busLineID = string2Integer( | // Integer busLineID = string2Integer( | ||||
// grep( | // grep( | ||||
// "([0-9]+)$", | // "([0-9]+)$", | ||||
// line.attr("href") | // line.attr("href") | ||||
// ) | // ) | ||||
// ); | // ); | ||||
if (busLineName == null) { | if (busLineName == null) { | ||||
res.set(result.SERVER_ERROR); | res.set(Result.SERVER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
// this fetcher doesn't support railways and probably they've removed METRO too, but anyway... | // this fetcher doesn't support railways and probably they've removed METRO too, but anyway... | ||||
if(busLineName.equals("METRO")) { | if(busLineName.equals("METRO")) { | ||||
routeIndex = p.addRoute(busLineName, "", Route.Type.METRO); | routeIndex = p.addRoute(busLineName, "", Route.Type.METRO); | ||||
} else { | } else { | ||||
if(busLineName.length() >= 4) { | if(busLineName.length() >= 4) { | ||||
Show All 26 Lines | // ); | ||||
// Yes... Sometimes there is an EMPTY td ._. | // Yes... Sometimes there is an EMPTY td ._. | ||||
continue; | continue; | ||||
} | } | ||||
p.addPassaggio(time, Passaggio.Source.FiveTScraper, routeIndex); | p.addPassaggio(time, Passaggio.Source.FiveTScraper, routeIndex); | ||||
} | } | ||||
} | } | ||||
p.sortRoutes(); | p.sortRoutes(); | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
return p; | return p; | ||||
} | } | ||||
@Override | @Override | ||||
public Passaggio.Source getSourceForFetcher() { | public Passaggio.Source getSourceForFetcher() { | ||||
return Passaggio.Source.FiveTScraper; | return Passaggio.Source.FiveTScraper; | ||||
} | } | ||||
Show All 36 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register