|
| 1 | +package org.rpi.web.longpolling; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.DataOutputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.StringReader; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLEncoder; |
| 9 | + |
| 10 | +import org.apache.log4j.Logger; |
| 11 | + |
| 12 | +import java.net.HttpURLConnection; |
| 13 | + |
| 14 | +import javax.json.Json; |
| 15 | +import javax.json.JsonArray; |
| 16 | +import javax.json.JsonObject; |
| 17 | +import javax.json.JsonReader; |
| 18 | + |
| 19 | +public class LastFMUtils { |
| 20 | + |
| 21 | + private String lastfrm_api_key = "003dc9a812e87f5058f53439dd26038e"; |
| 22 | + private Logger log = Logger.getLogger(LastFMUtils.class); |
| 23 | + |
| 24 | + public ArtistInfo searchArtistByName(String artist) { |
| 25 | + ArtistInfo info = new ArtistInfo(); |
| 26 | + |
| 27 | + try { |
| 28 | + // artist = java.net.URLEncoder.encode(artist, "UTF-8"); |
| 29 | + String USER_AGENT = "Mozilla/5.0"; |
| 30 | + String url = "http://ws.audioscrobbler.com/2.0/?"; |
| 31 | + // String parameters = |
| 32 | + // "method=artist.getinfo&artist=%1$s&api_key=%2$s&format=json"; |
| 33 | + String parameters = "method=artist.getinfo&artist=%1$s&api_key=%2$s&format=json"; |
| 34 | + // String parameters = |
| 35 | + // "apikey=8e8ff105f5380c9f4d75e4d1518cf50750167cf5&application=SmartHome&event=%1$s&description=%2$s&priority=%3$s"; |
| 36 | + parameters = String.format(parameters, URLEncoder.encode(artist, "UTF-8"), lastfrm_api_key); |
| 37 | + |
| 38 | + URL obj = new URL(url + parameters); |
| 39 | + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
| 40 | + |
| 41 | + // add reuqest header |
| 42 | + con.setRequestMethod("POST"); |
| 43 | + con.setRequestProperty("User-Agent", USER_AGENT); |
| 44 | + con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); |
| 45 | + |
| 46 | + // String urlParameters = |
| 47 | + // "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; |
| 48 | + |
| 49 | + // Send post request |
| 50 | + con.setDoOutput(true); |
| 51 | + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); |
| 52 | + // wr.writeBytes(urlParameters); |
| 53 | + wr.flush(); |
| 54 | + wr.close(); |
| 55 | + |
| 56 | + int responseCode = con.getResponseCode(); |
| 57 | + log.debug("Sending 'POST' request to URL : " + url); |
| 58 | + // log.debug("Post parameters : " + urlParameters); |
| 59 | + log.debug("Response Code : " + responseCode); |
| 60 | + |
| 61 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
| 62 | + String inputLine; |
| 63 | + StringBuffer response = new StringBuffer(); |
| 64 | + |
| 65 | + while ((inputLine = in.readLine()) != null) { |
| 66 | + response.append(inputLine); |
| 67 | + } |
| 68 | + in.close(); |
| 69 | + log.debug(response.toString()); |
| 70 | + // info.setBiography(response.toString()); |
| 71 | + info = getBioInfo(response.toString()); |
| 72 | + } catch (Exception e) { |
| 73 | + log.error(" ", e); |
| 74 | + } |
| 75 | + |
| 76 | + return info; |
| 77 | + } |
| 78 | + |
| 79 | + private ArtistInfo getBioInfo(String info) { |
| 80 | + ArtistInfo artistInfo = new ArtistInfo(); |
| 81 | + JsonReader reader = Json.createReader(new StringReader(info)); |
| 82 | + JsonObject jsonObject = reader.readObject(); |
| 83 | + reader.close(); |
| 84 | + if (jsonObject.containsKey("artist")) { |
| 85 | + JsonObject artist = jsonObject.getJsonObject("artist"); |
| 86 | + if (artist.containsKey("bio")) { |
| 87 | + JsonObject bio = artist.getJsonObject("bio"); |
| 88 | + //log.debug("Bio: " + bio); |
| 89 | + |
| 90 | + if (bio.containsKey("content")) { |
| 91 | + String content = bio.getString("content"); |
| 92 | + |
| 93 | + if (!content.equals("")) { |
| 94 | + artistInfo.setBiography(content); |
| 95 | + } else { |
| 96 | + if (bio.containsKey("summary")) { |
| 97 | + String summary = bio.getString("summary"); |
| 98 | + artistInfo.setBiography(summary); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + if (artist.containsKey("image")) { |
| 104 | + JsonArray images = artist.getJsonArray("image"); |
| 105 | + if (images != null) { |
| 106 | + for (Object o : images) { |
| 107 | + if (o instanceof JsonObject) { |
| 108 | + JsonObject image = (JsonObject) o; |
| 109 | + if (image.containsKey("size")) { |
| 110 | + String size = image.getString("size"); |
| 111 | + String url = image.getString("#text"); |
| 112 | + if (!url.equals("")) { |
| 113 | + artistInfo.setImageURL(url); |
| 114 | + if (size.equalsIgnoreCase("LARGE")) { |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + //log.debug("Images"); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + /* |
| 126 | + * for (JsonValue jsonValue : body) { if (jsonValue.getValueType() |
| 127 | + * == ValueType.OBJECT) { JsonObject object = (JsonObject) |
| 128 | + * jsonValue; if (object.containsKey("artist")) { |
| 129 | + * log.debug("ContainsArtist"); } if (object.containsKey("name")) { |
| 130 | + * //name = object.getString("name"); } if |
| 131 | + * (object.containsKey("type")) { //type = object.getString("type"); |
| 132 | + * } if (object.containsKey("time")) { //time = |
| 133 | + * object.getString("time"); } if (object.containsKey("volume")) { |
| 134 | + * //volume = object.getInt("volume"); } if |
| 135 | + * (object.containsKey("channel")) { //channel = |
| 136 | + * object.getString("channel"); } if (object.containsKey("shuffle")) |
| 137 | + * { //shuffle = object.getBoolean("shuffle"); } |
| 138 | + * |
| 139 | + * } } |
| 140 | + */ |
| 141 | + |
| 142 | + } |
| 143 | + return artistInfo; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments