Created
May 14, 2013 17:53
-
-
Save ryanlindsey/5578017 to your computer and use it in GitHub Desktop.
Processing API wrapper for http://music.ryanlindsey.me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.json.*; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| public static class MusicAPI { | |
| // Setup API endpoints | |
| protected String request_url = "http://music.ryanlindsey.me/api"; | |
| protected String api_random_track = "/track/random"; | |
| protected String api_latest_track = "/track/latest"; | |
| // Public Properties | |
| public String trackName; | |
| public String artistName; | |
| public String albumName; | |
| public int trackTime; | |
| public int trackNumber; | |
| public String albumArtUrl; | |
| // Private Properties | |
| private PApplet _parent; | |
| // Constructor | |
| public MusicAPI(PApplet parent, int request_type) { | |
| _parent = parent; | |
| String[] request_types = {"latest", "random"}; | |
| switch (request_type) { | |
| case 0: | |
| handle_request(api_latest_track); | |
| break; | |
| case 1: | |
| handle_request(api_random_track); | |
| break; | |
| default: | |
| println("Please specify an API query"); | |
| break; | |
| } | |
| } | |
| // Public Methods | |
| public PImage loadAlbumArt() { | |
| return _parent.loadImage(this.albumArtUrl); | |
| } | |
| // Private Methods | |
| private void handle_request(String query) { | |
| String[] response = _parent.loadStrings(request_url + query); | |
| JSONObject json = JSONObject.parse(response[0]); | |
| JSONObject song = json.getJSONObject("track"); | |
| this.trackName = song.getString("track_name"); | |
| this.artistName = song.getString("artist_name"); | |
| this.albumName = song.getString("album_name"); | |
| this.trackTime = int(song.getString("track_time")); | |
| this.trackNumber = int(song.getString("track_number")); | |
| this.albumArtUrl = song.getString("album_art_url"); | |
| /*Iterator keys = song.keys(); | |
| while (keys.hasNext()) { | |
| String key = (String)keys.next(); | |
| String val = song.getString(key); | |
| println(key + ": " + val); | |
| }*/ | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment