Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/GTTJSONFetcher.java
Show All 28 Lines | |||||
import java.net.URL; | import java.net.URL; | ||||
import java.net.URLEncoder; | import java.net.URLEncoder; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
public class GTTJSONFetcher implements ArrivalsFetcher { | public class GTTJSONFetcher implements ArrivalsFetcher { | ||||
private final String DEBUG_TAG = "GTTJSONFetcher-BusTO"; | private final String DEBUG_TAG = "GTTJSONFetcher-BusTO"; | ||||
@Override @NonNull | @Override @NonNull | ||||
public Palina ReadArrivalTimesAll(String stopID, AtomicReference<result> res) { | public Palina ReadArrivalTimesAll(String stopID, AtomicReference<Result> res) { | ||||
URL url; | URL url; | ||||
Palina p = new Palina(stopID); | Palina p = new Palina(stopID); | ||||
String routename; | String routename; | ||||
String bacino; | String bacino; | ||||
String content; | String content; | ||||
JSONArray json; | JSONArray json; | ||||
int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale... | int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale... | ||||
JSONObject thisroute; | JSONObject thisroute; | ||||
JSONArray passaggi; | JSONArray passaggi; | ||||
try { | try { | ||||
url = new URL("https://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransitiOld&palina=" + URLEncoder.encode(stopID, "utf-8") + "&bacino=U&realtime=true&get_param=value"); | url = new URL("https://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransitiOld&palina=" + URLEncoder.encode(stopID, "utf-8") + "&bacino=U&realtime=true&get_param=value"); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
HashMap<String, String> headers = new HashMap<>(); | HashMap<String, String> headers = new HashMap<>(); | ||||
//headers.put("Referer","https://www.gtt.to.it/cms/percorari/urbano?view=percorsi&bacino=U&linea=15&Regol=GE"); | //headers.put("Referer","https://www.gtt.to.it/cms/percorari/urbano?view=percorsi&bacino=U&linea=15&Regol=GE"); | ||||
headers.put("Host", "www.gtt.to.it"); | headers.put("Host", "www.gtt.to.it"); | ||||
content = networkTools.queryURL(url, res, headers); | content = networkTools.queryURL(url, res, headers); | ||||
if(content == null) { | if(content == null) { | ||||
Log.w("GTTJSONFetcher", "NULL CONTENT"); | Log.w("GTTJSONFetcher", "NULL CONTENT"); | ||||
return p; | return p; | ||||
} | } | ||||
try { | try { | ||||
json = new JSONArray(content); | json = new JSONArray(content); | ||||
} catch(JSONException e) { | } catch(JSONException e) { | ||||
Log.w(DEBUG_TAG, "Error parsing JSON: \n"+content); | Log.w(DEBUG_TAG, "Error parsing JSON: \n"+content); | ||||
Log.w(DEBUG_TAG, e); | Log.w(DEBUG_TAG, e); | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
try { | try { | ||||
// returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops! | // returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops! | ||||
json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. | json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. | ||||
} catch(JSONException e) { | } catch(JSONException e) { | ||||
Log.w(DEBUG_TAG, "No existing lines"); | Log.w(DEBUG_TAG, "No existing lines"); | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
return p; | return p; | ||||
} | } | ||||
howManyRoutes = json.length(); | howManyRoutes = json.length(); | ||||
if(howManyRoutes == 0) { | if(howManyRoutes == 0) { | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
return p; | return p; | ||||
} | } | ||||
try { | try { | ||||
for(i = 0; i < howManyRoutes; i++) { | for(i = 0; i < howManyRoutes; i++) { | ||||
thisroute = json.getJSONObject(i); | thisroute = json.getJSONObject(i); | ||||
routename = thisroute.getString("Linea"); | routename = thisroute.getString("Linea"); | ||||
try { | try { | ||||
Show All 16 Lines | public Palina ReadArrivalTimesAll(String stopID, AtomicReference<Result> res) { | ||||
passaggi = thisroute.getJSONArray("PassaggiPR"); // now the non-real-time ones | passaggi = thisroute.getJSONArray("PassaggiPR"); // now the non-real-time ones | ||||
howManyPassaggi = passaggi.length(); | howManyPassaggi = passaggi.length(); | ||||
for(j = 0; j < howManyPassaggi; j++) { | for(j = 0; j < howManyPassaggi; j++) { | ||||
p.addPassaggio(passaggi.getString(j), Passaggio.Source.GTTJSON, pos); | p.addPassaggio(passaggi.getString(j), Passaggio.Source.GTTJSON, pos); | ||||
} | } | ||||
} | } | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
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.GTTJSON; | return Passaggio.Source.GTTJSON; | ||||
} | } | ||||
} | } |
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