package be.jaud; import org.json.JSONArray; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; public class OebbCheck { public static final Logger LOG = LoggerFactory.getLogger(OebbCheck.class); public OebbCheck(){ } /* Returns the Delay in Minutes for a given StationID, Departure Time, final stop and Train type(i.e. REX 3, S 2....) Returns -1 if no connection was found Returns 0 for no delay >0 is the delay in Minutes 9999 means the Train has been cancelled */ public int getStatus(String Station, String Time, String FinalStop, String TrainType, String maxjourneys) { JSONArray journeys = null; try { journeys = getData(Station, Time, maxjourneys); } catch (URISyntaxException e) { LOG.error("URISyntaxException: {}", e.toString()); } catch (IOException e) { LOG.error("IOException: {}", e.toString()); } if(journeys != null) { for (Object journey : journeys) { JSONObject jour = (JSONObject) journey; if (jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)) { if (jour.get("rt") instanceof JSONObject data) { if (data.get("status").equals("Ausfall")) { return 9999; } else { return data.getInt("dlm"); } } else { return 0; } } } return -1; } return -1; } private static JSONArray getData(String Station, String Time, String maxjourneys) throws IOException, URISyntaxException { URL url = new URI("https://fahrplan.oebb.at/bin/stboard.exe/dn?L=vs_scotty.vs_liveticker&evaId=" + Station + "&boardType=dep&time="+ Time +"&productsFilter=1011110111011&additionalTime=0&maxJourneys=" + maxjourneys + "&outputMode=tickerDataOnly&start=yes&selectDate=today").toURL(); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); String sub = content.substring(14); JSONObject jo = new JSONObject(sub); return jo.getJSONArray("journey"); } }