示例#1
0
  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");
        });
  }