Last.fmのAPIと「XML::Simple」

やりたいこと

Last.fmAPI(http://www.audioscrobbler.net/data/webservices/)と XML::Simple モジュールを使って、"ragtime"というタグ名からTopArtist(人気のあるアーティスト)を抜き出す。

#!/usr/local/bin/perl
use strict;
use warnings;
use LWP::Simple;
use XML::Simple;

my $tag = "ragtime";
my $url = "http://ws.audioscrobbler.com/1.0/tag/$tag/topartists.xml";

my $ls = LWP::Simple::get($url) or die $!;
my $xs = XML::Simple->new;
my $ref = $xs->XMLin($ls);

# ハッシュから、"アーティスト名", "Count(タグをつけたユーザ数)", "URL" を表示
for (keys %{$ref->{artist}}) {
   warn "Artist => $_\n",
        "URL    => $ref->{artist}->{$_}->{url}\n",
        "Count  => $ref->{artist}->{$_}->{count}\n\n";
}

XMLの出力 (http://ws.audioscrobbler.com/1.0/tag/ragtime/topartists.xml)

<tag tag="ragtime" count="686"><artist name="Scott Joplin" count="79" streamable="yes">
  <mbid>aec8a328-d2e8-4780-b2ea-318c7f8d6f75</mbid>
  <url>http://www.last.fm/music/Scott+Joplin</url>
  <thumbnail>http://userserve-ak.last.fm/serve/50/412356.jpg</thumbnail>
  <image>http://userserve-ak.last.fm/serve/160/412356.jpg</image>
 </artist><artist name="The Ditty Bops" count="11" streamable="yes">
  <mbid>822c67fc-ab63-42ad-b27a-66039e37fe00</mbid>
  <url>http://www.last.fm/music/The+Ditty+Bops</url>
  <thumbnail>http://userserve-ak.last.fm/serve/50/63625.jpg</thumbnail>
  <image>http://userserve-ak.last.fm/serve/160/63625.jpg</image>
 </artist>
-〜以下、省略〜

$ref のDump出力

artist:
 Winifred Atwell:
  count: 2
  image: http://userserve-ak.last.fm/serve/160/103319.jpg
  mbid: b182700d-c0e2-4e3a-8373-e57a76333227
  streamable: yes
  thumbnail: http://userserve-ak.last.fm/serve/50/103319.jpg
  url: http://www.last.fm/music/Winifred+Atwell
 Zez Confrey:
  count: 2
  image: http://cdn.last.fm/depth/catalogue/noimage/noartist_140px.jpg
  mbid: 29fa57fe-26de-4249-9e85-5d41f760b252
  streamable: yes
  thumbnail: http://cdn.last.fm/depth/catalogue/noimage/noartist_50px.jpg
  url: http://www.last.fm/music/Zez+Confrey
 〜以下、省略〜
count: 686
tag: ragtime

スクリプトの出力結果

Artist => William Albright
URL => http://www.last.fm/music/William+Albright
Count => 4


Artist => Bowman, Euday L.
URL => http://www.last.fm/music/Bowman%2C+Euday+L.
Count => 1
〜以下、省略〜

もっとやりたいこと

  • CountでソートしてTop10表示とか
  • XML::Simple は遅いらしいので、違うモジュールも試してみたい