Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java
Show All 31 Lines | |||||
public class FiveTAPIFetcher implements ArrivalsFetcher{ | public class FiveTAPIFetcher implements ArrivalsFetcher{ | ||||
private static final String DEBUG_NAME = "FiveTAPIFetcher"; | private static final String DEBUG_NAME = "FiveTAPIFetcher"; | ||||
private final Map<String,String> defaultHeaders = getDefaultHeaders(); | private final Map<String,String> defaultHeaders = getDefaultHeaders(); | ||||
final static LinkedList<String> apiDays = new LinkedList<>(Arrays.asList("dom","lun","mar","mer","gio","ven","sab")); | final static LinkedList<String> apiDays = new LinkedList<>(Arrays.asList("dom","lun","mar","mer","gio","ven","sab")); | ||||
@Override | @Override | ||||
public Palina ReadArrivalTimesAll(String stopID, AtomicReference<result> res) { | public Palina ReadArrivalTimesAll(String stopID, AtomicReference<Result> res) { | ||||
//set the date for the request as now | //set the date for the request as now | ||||
Palina p = new Palina(stopID); | Palina p = new Palina(stopID); | ||||
//request parameters | //request parameters | ||||
String response = performAPIRequest(QueryType.ARRIVALS,stopID,res); | String response = performAPIRequest(QueryType.ARRIVALS,stopID,res); | ||||
if(response==null) { | if(response==null) { | ||||
if(res.get()==result.SERVER_ERROR_404) { | if(res.get()== Result.SERVER_ERROR_404) { | ||||
Log.w(DEBUG_NAME,"Got 404, either the server failed, or the stop was not found, or the hack is not working anymore"); | Log.w(DEBUG_NAME,"Got 404, either the server failed, or the stop was not found, or the address is wrong"); | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
} | } | ||||
return p; | return p; | ||||
} | } | ||||
try { | |||||
List<Route> routes = parseArrivalsServerResponse(response, res); | List<Route> routes = parseArrivalsServerResponse(response, res); | ||||
if(res.get()==Result.OK) { | |||||
for(Route r: routes){ | for (Route r : routes) { | ||||
p.addRoute(r); | p.addRoute(r); | ||||
} | } | ||||
} catch (JSONException ex){ | |||||
res.set(result.PARSER_ERROR); | |||||
Log.w(DEBUG_NAME, "Couldn't get the JSON repr of:\n"+response); | |||||
return null; | |||||
} | |||||
res.set(result.OK); | |||||
p.sortRoutes(); | p.sortRoutes(); | ||||
} | |||||
return p; | return p; | ||||
} | } | ||||
@Override | @Override | ||||
public Passaggio.Source getSourceForFetcher() { | public Passaggio.Source getSourceForFetcher() { | ||||
return Passaggio.Source.FiveTAPI; | return Passaggio.Source.FiveTAPI; | ||||
} | } | ||||
List<Route> parseArrivalsServerResponse(String JSONresponse, AtomicReference<result> res) throws JSONException{ | List<Route> parseArrivalsServerResponse(String JSONresponse, AtomicReference<Result> res){ | ||||
ArrayList<Route> routes = new ArrayList<>(3); | ArrayList<Route> routes = new ArrayList<>(3); | ||||
/* | /* | ||||
Slight problem: | Slight problem: | ||||
"longName": ==> DESCRIPTION | "longName": ==> DESCRIPTION | ||||
"name": "13N", | "name": "13N", | ||||
"departures": [ | "departures": [ | ||||
{ | { | ||||
"arrivalTimeInt": 1272, | "arrivalTimeInt": 1272, | ||||
"time": "21:12", | "time": "21:12", | ||||
"rt": false | "rt": false | ||||
}] | }] | ||||
"lineType": "URBANO" ==> URBANO can be either bus or tram or METRO | "lineType": "URBANO" ==> URBANO can be either bus or tram or METRO | ||||
*/ | */ | ||||
JSONArray arr; | JSONArray arr; | ||||
try{ | try{ | ||||
arr = new JSONArray(JSONresponse); | arr = new JSONArray(JSONresponse); | ||||
String type; | String type; | ||||
Route.Type routetype; | Route.Type routetype = Route.Type.UNKNOWN; | ||||
for(int i =0; i<arr.length();i++){ | for(int i =0; i<arr.length();i++){ | ||||
JSONObject lineJSON = arr.getJSONObject(i); | JSONObject lineJSON = arr.getJSONObject(i); | ||||
type = lineJSON.getString("lineType"); | |||||
String lineName=FiveTNormalizer.routeDisplayToInternal(lineJSON.getString("name")); | String lineName=FiveTNormalizer.routeDisplayToInternal(lineJSON.getString("name")); | ||||
if(lineJSON.has("lineType")) { | |||||
type = lineJSON.getString("lineType"); | |||||
//set the type of line | //set the type of line | ||||
if(type.equals("EXTRA")) | if(type.equals("EXTRA")) | ||||
routetype = Route.Type.LONG_DISTANCE_BUS; | routetype = Route.Type.LONG_DISTANCE_BUS; | ||||
else routetype = Route.Type.BUS; | else routetype = Route.Type.BUS; | ||||
} | |||||
//Cut out the spaces in the line Name | //Cut out the spaces in the line Name | ||||
//temporary fix | //temporary fix | ||||
//lineName = lineName.replace(" ","").replace("/","B"); | //lineName = lineName.replace(" ","").replace("/","B"); | ||||
//TODO: parse the line description | //TODO: parse the line description | ||||
Route r = new Route(lineName,routetype,lineJSON.getString("longName")); | Route r = new Route(lineName,routetype,lineJSON.getString("longName")); | ||||
//Log.d(DEBUG_NAME,"Creating line with name "+lineJSON.getString("name")+" and description "+lineJSON.getString("longName")); | //Log.d(DEBUG_NAME,"Creating line with name "+lineJSON.getString("name")+" and description "+lineJSON.getString("longName")); | ||||
final JSONArray passagesJSON = lineJSON.getJSONArray("departures"); | final JSONArray passagesJSON = lineJSON.getJSONArray("departures"); | ||||
for(int j=0;j<passagesJSON.length();j++){ | for(int j=0;j<passagesJSON.length();j++){ | ||||
final JSONObject arrival = passagesJSON.getJSONObject(j); | final JSONObject arrival = passagesJSON.getJSONObject(j); | ||||
final String passaggio = Passaggio.createPassaggioGTT(arrival.getString("time"),arrival.getBoolean("rt")); | final String passaggio = Passaggio.createPassaggioGTT(arrival.getString("time"),arrival.getBoolean("rt")); | ||||
r.addPassaggio(passaggio, Passaggio.Source.FiveTAPI); | r.addPassaggio(passaggio, Passaggio.Source.FiveTAPI); | ||||
//Log.d(DEBUG_NAME,"Adding passage with time "+arrival.getString("time")+"\nrealtime="+arrival.getBoolean("rt")); | //Log.d(DEBUG_NAME,"Adding passage with time "+arrival.getString("time")+"\nrealtime="+arrival.getBoolean("rt")); | ||||
} | } | ||||
routes.add(r); | routes.add(r); | ||||
} | } | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return routes; | return routes; | ||||
} | } | ||||
Collections.sort(routes); | Collections.sort(routes); | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
return routes; | return routes; | ||||
} | } | ||||
public List<Route> parseDirectionsFromResponse(String response) throws IllegalArgumentException,JSONException{ | public List<Route> parseDirectionsFromResponse(String response) throws IllegalArgumentException,JSONException{ | ||||
if(response == null || response.length()==0) throw new IllegalArgumentException("Response string is null or void"); | if(response == null || response.length()==0) throw new IllegalArgumentException("Response string is null or void"); | ||||
ArrayList<Route> routes = new ArrayList<>(10); | ArrayList<Route> routes = new ArrayList<>(10); | ||||
JSONArray lines =new JSONArray(response); | JSONArray lines =new JSONArray(response); | ||||
for(int i=0; i<lines.length();i++){ | for(int i=0; i<lines.length();i++){ | ||||
Route.FestiveInfo festivo = Route.FestiveInfo.UNKNOWN; | Route.FestiveInfo festivo = Route.FestiveInfo.UNKNOWN; | ||||
final JSONObject branchJSON = lines.getJSONObject(i); | final JSONObject branchJSON = lines.getJSONObject(i); | ||||
final int branchid = branchJSON.getInt("branch"); | final int branchid = branchJSON.getInt("branch"); | ||||
String description = branchJSON.getString("description"); | String description = branchJSON.getString("description"); | ||||
if(description.contains(" fittizi")){ | if(description.contains(" fittizi")){ | ||||
//this means that the branch is fake | //this means that the branch is fake | ||||
continue; | continue; | ||||
} | } | ||||
String direction = branchJSON.getString("direction"); | String direction = branchJSON.getString("direction"); | ||||
String stops = branchJSON.getJSONObject("branchDetail").getString("stops"); | |||||
String lineName = branchJSON.getString("lineName"); | String lineName = branchJSON.getString("lineName"); | ||||
Route.Type t = Route.Type.UNKNOWN; | Route.Type t = Route.Type.UNKNOWN; | ||||
//parsing description | //parsing description | ||||
String[] exploded = description.split(","); | String[] exploded = description.split(","); | ||||
description = exploded[exploded.length-1]; //the real description | description = exploded[exploded.length-1]; //the real description | ||||
int[] serviceDays = {}; | int[] serviceDays = {}; | ||||
if(exploded.length > 1) { | if(exploded.length > 1) { | ||||
String secondo = exploded[exploded.length-2]; | String secondo = exploded[exploded.length-2]; | ||||
Show All 30 Lines | public List<Route> parseDirectionsFromResponse(String response) throws IllegalArgumentException,JSONException{ | ||||
} | } | ||||
} else //only one piece | } else //only one piece | ||||
if(description.contains("festivo")){ | if(description.contains("festivo")){ | ||||
festivo = Route.FestiveInfo.FESTIVO; | festivo = Route.FestiveInfo.FESTIVO; | ||||
} else if(description.contains("feriale")){ | } else if(description.contains("feriale")){ | ||||
festivo = Route.FestiveInfo.FERIALE; | festivo = Route.FestiveInfo.FERIALE; | ||||
} | } | ||||
if(t == Route.Type.UNKNOWN &&(lineName.trim().equals("10")|| lineName.trim().equals("15"))) t= Route.Type.TRAM; | if(t == Route.Type.UNKNOWN &&(lineName.trim().equals("10")|| lineName.trim().equals("15"))) t= Route.Type.TRAM; | ||||
if(direction.contains("-")){ | //check for the presence of parenthesis | ||||
String preParenthesis, postParenthesis; | |||||
boolean hasParenth = false; | |||||
if (description.contains("(")){ | |||||
hasParenth =true; | |||||
preParenthesis = description.split("\\(")[0]; | |||||
postParenthesis = description.split("\\(")[1]; | |||||
} else { | |||||
preParenthesis = description; | |||||
postParenthesis = ""; | |||||
} | |||||
if(preParenthesis.contains("-")){ | |||||
//Sometimes the actual filtered direction still remains the full line (including both extremes) | //Sometimes the actual filtered direction still remains the full line (including both extremes) | ||||
direction = direction.split("-")[1]; | preParenthesis = preParenthesis.split("-")[1]; | ||||
} | } | ||||
Route r = new Route(lineName.trim(),direction.trim(),t,new ArrayList<>()); | final String directionFinal = hasParenth? preParenthesis.trim() + " (" + postParenthesis : preParenthesis; | ||||
Route r = new Route(lineName.trim(),directionFinal.trim(),t,new ArrayList<>()); | |||||
if(serviceDays.length>0) r.serviceDays = serviceDays; | if(serviceDays.length>0) r.serviceDays = serviceDays; | ||||
r.festivo = festivo; | r.festivo = festivo; | ||||
r.branchid = branchid; | r.branchid = branchid; | ||||
r.description = description.trim(); | r.description = description.trim(); | ||||
//check if we have the stop list | |||||
if (branchJSON.has("branchDetail")) { | |||||
final String stops = branchJSON.getJSONObject("branchDetail").getString("stops"); | |||||
r.setStopsList(Arrays.asList(stops.split(","))); | r.setStopsList(Arrays.asList(stops.split(","))); | ||||
} | |||||
routes.add(r); | routes.add(r); | ||||
} | } | ||||
return routes; | return routes; | ||||
} | } | ||||
public List<Route> getDirectionsForStop(String stopID, AtomicReference<result> res) { | public List<Route> getDirectionsForStop(String stopID, AtomicReference<Result> res) { | ||||
String response = performAPIRequest(QueryType.DETAILS,stopID,res); | String response = performAPIRequest(QueryType.DETAILS,stopID,res); | ||||
List<Route> routes; | List<Route> routes; | ||||
try{ | try{ | ||||
routes = parseDirectionsFromResponse(response); | routes = parseDirectionsFromResponse(response); | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
} catch (JSONException | IllegalArgumentException e) { | } catch (JSONException | IllegalArgumentException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
routes = null; | routes = null; | ||||
} | } | ||||
return routes; | return routes; | ||||
} | } | ||||
public ArrayList<Stop> getAllStopsFromGTT(AtomicReference<result> res){ | public ArrayList<Stop> getAllStopsFromGTT(AtomicReference<Result> res){ | ||||
String response = performAPIRequest(QueryType.STOPS_ALL,null,res); | String response = performAPIRequest(QueryType.STOPS_ALL,null,res); | ||||
if(response==null) return null; | if(response==null) return null; | ||||
ArrayList<Stop> stopslist; | ArrayList<Stop> stopslist; | ||||
try{ | try{ | ||||
JSONObject responseJSON = new JSONObject(response); | JSONObject responseJSON = new JSONObject(response); | ||||
JSONArray stops = responseJSON.getJSONArray("stops"); | JSONArray stops = responseJSON.getJSONArray("stops"); | ||||
stopslist = new ArrayList<>(stops.length()); | stopslist = new ArrayList<>(stops.length()); | ||||
Show All 24 Lines | public ArrayList<Stop> getAllStopsFromGTT(AtomicReference<Result> res){ | ||||
Stop s = new Stop(currentStop.getString("id"), | Stop s = new Stop(currentStop.getString("id"), | ||||
currentStop.getString("name"),null,location,t,Arrays.asList(lines), | currentStop.getString("name"),null,location,t,Arrays.asList(lines), | ||||
Double.parseDouble(currentStop.getString("lat")), | Double.parseDouble(currentStop.getString("lat")), | ||||
Double.parseDouble(currentStop.getString("lng"))); | Double.parseDouble(currentStop.getString("lng"))); | ||||
if(placeName!=null) | if(placeName!=null) | ||||
s.setAbsurdGTTPlaceName(placeName); | s.setAbsurdGTTPlaceName(placeName); | ||||
stopslist.add(s); | stopslist.add(s); | ||||
} | } | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return null; | return null; | ||||
} | } | ||||
return stopslist; | return stopslist; | ||||
} | } | ||||
@Nullable | @Nullable | ||||
public ArrayList<Route> getAllLinesFromGTT(AtomicReference<result> res){ | public ArrayList<Route> getAllLinesFromGTT(AtomicReference<Result> res){ | ||||
String resp = performAPIRequest(QueryType.LINES,null,res); | String resp = performAPIRequest(QueryType.LINES,null,res); | ||||
if(resp==null) { | if(resp==null) { | ||||
return null; | return null; | ||||
} | } | ||||
ArrayList<Route> routes = null; | ArrayList<Route> routes = null; | ||||
try { | try { | ||||
Show All 15 Lines | public ArrayList<Route> getAllLinesFromGTT(AtomicReference<Result> res){ | ||||
default: | default: | ||||
t = Route.Type.UNKNOWN; | t = Route.Type.UNKNOWN; | ||||
} | } | ||||
String name = lineJ.getString("name"); | String name = lineJ.getString("name"); | ||||
routes.add(new Route(name,t,lineJ.getString("longName"))); | routes.add(new Route(name,t,lineJ.getString("longName"))); | ||||
} | } | ||||
//finish | //finish | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return routes; | return routes; | ||||
} | } | ||||
return routes; | return routes; | ||||
} | } | ||||
/** | /** | ||||
* Useful to get all the headers for the GTT server | * Useful to get all the headers for the GTT server | ||||
Show All 10 Lines | public class FiveTAPIFetcher implements ArrivalsFetcher{ | ||||
/** | /** | ||||
* Create and perform the network request. This method adds parameters and returns the result | * Create and perform the network request. This method adds parameters and returns the result | ||||
* @param t type of request to be performed | * @param t type of request to be performed | ||||
* @param stopID optional parameter, stop ID which you need for passages and branches | * @param stopID optional parameter, stop ID which you need for passages and branches | ||||
* @param res result container | * @param res result container | ||||
* @return a String which contains the result of the query, to be parsed | * @return a String which contains the result of the query, to be parsed | ||||
*/ | */ | ||||
@Nullable | @Nullable | ||||
public static String performAPIRequest(QueryType t,@Nullable String stopID, AtomicReference<result> res){ | public static String performAPIRequest(QueryType t,@Nullable String stopID, AtomicReference<Result> res){ | ||||
URL u; | URL u; | ||||
Map<String,String> param; | Map<String,String> param; | ||||
try { | try { | ||||
String address = getURLForOperation(t,stopID); | String address = getURLForOperation(t,stopID); | ||||
//Log.d(DEBUG_NAME,"The address to query is: "+address); | //Log.d(DEBUG_NAME,"The address to query is: "+address); | ||||
param = getDefaultHeaders(); | param = getDefaultHeaders(); | ||||
u = new URL(address); | u = new URL(address); | ||||
} catch (UnsupportedEncodingException |MalformedURLException e) { | } catch (UnsupportedEncodingException |MalformedURLException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return null; | return null; | ||||
} | } | ||||
String response = networkTools.queryURL(u,res,param); | String response = networkTools.queryURL(u,res,param); | ||||
return response; | return response; | ||||
} | } | ||||
/** | /** | ||||
Show All 38 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