javac#c++last.fm

Fetching album art, as JPEG, PNG etc


Is there any simple Web API which returns album art as an image, just like the Google Static Map API (Google Static Map API? I have heard about Amazon's and Last.fm's APIs for this, but can't find any code for that (other than in web languages like PHP/Perl/Ruby etc). Moreover they are REST APIs. Any help would be appreciated.

Is there a simple URL(GET) queries or are there only REST/XML methods?


Solution

  • It shouldn't be very hard to do by hand. The last.fm api spec for Album.getInfo method is pretty straightforward and the fact that it is a REST api makes it even simpler. Just get the xml response from the server, parse it and get a url of the image. I wrote this code a while ago but it should still be working:

                String request = "http://ws.audioscrobbler.com/2.0/?method=" + method +
                        "&api_key="+apiKey;
                request += "&artist=" + artist.replaceAll(" ", "%20");
                if (method.equals("album.getinfo")) request += "&album=" + album.replaceAll(" ", "%20");
                URL url = new URL(request);
                InputStream is = url.openStream();
                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = db.parse(is);
    
                NodeList nl = doc.getElementsByTagName("image");
                for (int i = 0; i < nl.getLength(); i++) {
                    Node n = nl.item(i);
                    if (n.getAttributes().item(0).getNodeValue().equals(imageSize)) {
                        Node fc = n.getFirstChild();
                        if (fc == null) return null;
                        String imgUrl = fc.getNodeValue();
                        if (imgUrl.trim().length() == 0) return null;
                        return new ImageIcon(new URL(imgUrl));
                    }
                }