获取专辑封面,如 JPEG、PNG 等

发布于 2024-10-08 07:55:33 字数 302 浏览 0 评论 0原文

是否有任何简单的 Web API 可以将专辑封面作为图像返回,就像 Google 静态地图 API (Google 静态地图 API?我听说过 Amazon 和 Last.fm 的 API,但找不到任何代码(除了 PHP/Perl/Ruby 等网络语言)。此外它们是 REST API。 任何帮助将不胜感激。

是否有简单的 URL(GET) 查询或只有 REST/XML 方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-10-15 07:55:33

用手做应该不是很难。 Album.getInfo 方法的 last.fm api 规范非常简单,事实上它是一个 REST api,这使得它变得更加简单。只需从服务器获取 xml 响应,解析它并获取图像的 url。我不久前写了这段代码,但它应该仍然可以工作:

            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));
                }
            }

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));
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文