public JSONArray buildSpotifyJsonObject(ArrayList<Trip> list) { JSONArray array = new JSONArray(); try { for (Trip trip : list) { JSONArray tracks = new JSONArray(); for (Track track : trip.getTracks()) { tracks.put( new JSONObject() .put("name", track.getName()) .put("artist", track.getArtist()) .put("duration", track.getDuration()) .put("url", track.getUrl())); } array.put( new JSONObject() .put("origin", trip.getOrigin()) .put("destination", trip.getDest()) .put("id", trip.getId()) .put("duration", trip.getDurationMs()) .put("originDate", trip.getOriginDate()) .put("destinationDate", trip.getDestDate()) .put("originTime", trip.getOriginTime()) .put("destinationTime", trip.getDestTime()) .put("playlist", tracks)); } } catch (Exception E) { } System.out.println(array); return array; }
public JSONArray buildJsonObject(ArrayList<Trip> list) { JSONArray array = new JSONArray(); try { for (Trip trip : list) { JSONArray tracks = new JSONArray(); array.put( new JSONObject() .put("origin", trip.getOrigin()) .put("destination", trip.getDest()) .put("id", trip.getId()) .put("duration", trip.getDurationMs()) .put("originDate", trip.getOriginDate()) .put("destinationDate", trip.getDestDate()) .put("originTime", trip.getOriginTime()) .put("destinationTime", trip.getDestTime()) .put("playlist", tracks)); } } catch (Exception E) { } System.out.println(array); return array; }
private void parseTrips(BufferedReader reader) throws IOException { String line = reader.readLine(); // skip column names HashMap<String, Trip> processedTrips = new HashMap<>(); // route_id,service_id,trip_id,trip_headsign,direction_id,block_id,shape_id,wheelchair_accessible,trips_bkk_ref Pattern pattern = Pattern.compile("^([^,]+),[^,]*,([^,]+),(?:\"([^\"]+)\"|([^,]+)),(0|1),[^,]*,([^,]+),.*"); while ((line = reader.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (!matcher.matches()) { write(line); continue; } Integer routeId = routes.get(matcher.group(1)); Route route = routeId != null ? Route.findRoute(routeId) : null; if (route != null) { String direction = matcher.group(5); String quotedHeadsign = matcher.group(3); String headSign = quotedHeadsign != null ? quotedHeadsign : matcher.group(4); String tripKey = headSign + ":" + direction + ":" + matcher.group(6) + ":" + routeId; Trip trip = processedTrips.get(tripKey); if (trip == null) { trip = new Trip(); trip.setHeadSign(headSign); trip.setIsReturn("1".equals(direction)); trip.setRoute(route); processedTrips.put(tripKey, trip); trip.persist(); } trips.put(matcher.group(2), trip.getId()); } } }
public static void main(String[] args) throws ClassNotFoundException { String username = "******"; // Ändra username här // Ändra Auth tillhörande ditt spotify-account String OAuthToken = "BQB3W-ODBi1wq3gQWYwByWrRzshSd_LxRDBWBmI9L1VIh9MVGJeDm9vDYib2T1aebBiR9ghPbqqU8aI9oyeI3PWR-sggQtNfOjjNNXA3bo9AQxlIb3zNVJ24ZcmXGrPQ6pp3epObUrxC9lLohP39w-B8TQlGunisPxiwQrg9C-2K_K_dC5-3Dq9-wgdohH61RzXv32zwiNjNLcmrVFdARzgjNjFdY2Wo1Jiu"; WebService ws = new WebService(); JsonReader jr = new JsonReader(OAuthToken); setPort(8081); get( "/trip", (req, res) -> { System.out.println("-- Trip called --"); res.header("Access-Control-Allow-Origin", "*"); String from = req.queryParams("from"); String to = req.queryParams("to"); String key = "4651be79-347f-45e5-8eac-2062d752068c"; ArrayList<Trip> trips = jr.getTrips( "https://api.resrobot.se/trip?key=" + key + "&originId=" + from + "&destId=" + to + "&format=json"); System.out.println(ws.buildJsonObject(trips)); return ws.buildJsonObject(trips); }); get( "/location", (req, res) -> { System.out.println("-- Station called --"); res.header("Access-Control-Allow-Origin", "*"); String name = req.queryParams("name"); String key = "4651be79-347f-45e5-8eac-2062d752068c"; return jr.getLocations( "https://api.resrobot.se/location.name.json?key=" + key + "&input=" + name); }); get( "/trip/playlist", (req, res) -> { res.header("Access-Control-Allow-Origin", "*"); String from = req.queryParams("from"); String to = req.queryParams("to"); String x = req.queryParams("id"); int identi = Integer.parseInt(x); String key = "4651be79-347f-45e5-8eac-2062d752068c"; ArrayList<Trip> trips = jr.getSpotifyTrips( "https://api.resrobot.se/trip?key=" + key + "&originId=" + from + "&destId=" + to + "&format=json"); JSONObject jsonPlaylist = jr.createPlaylist( "https://api.spotify.com/v1/users/" + username + "/playlists", "Spellista " + x); String url = ""; for (Trip trip : trips) { if (Integer.parseInt(trip.getId()) == identi) { for (Track track : trip.getTracks()) { if (url.length() < 1) { url += track.getUrl(); } else { url += "," + track.getUrl(); } } } } String playlistID = jsonPlaylist.getString("id"); jr.addTracks( "https://api.spotify.com/v1/users/" + username + "/playlists/" + playlistID + "/tracks?uris=" + url); return jsonPlaylist.getJSONObject("external_urls").getString("spotify"); }); get( "/playlist", (req, res) -> { res.header("Access-Control-Allow-Origin", "*"); String x = req.queryParams("ms"); String playlistName = req.queryParams("name"); int duration = Integer.parseInt(x); ArrayList<Track> tracks = jr.getSpotifyTracks(duration); JSONObject jsonPlaylist = jr.createPlaylist( "https://api.spotify.com/v1/users/" + username + "/playlists", playlistName); String url = ""; for (Track track : tracks) { if (url.length() < 1) { url += track.getUrl(); } else { url += "," + track.getUrl(); } } String playlistID = jsonPlaylist.getString("id"); jr.addTracks( "https://api.spotify.com/v1/users/" + username + "/playlists/" + playlistID + "/tracks?uris=" + url); return jsonPlaylist.getJSONObject("external_urls").getString("spotify"); }); }