02 January 2014

(Perl) 音楽の曲名・アーティスト名からアルバムデータを検索する

MusicBrainzオンラインサービスを使って、曲名・アーティスト名より、その曲が含まれるアルバムを全て表示する。

#!/usr/bin/perl
 
use warnings;
use strict;
 
use WebService::MusicBrainz::Track;
use WebService::MusicBrainz::Release;
#use Data::Dumper;
 
my $ws_track = WebService::MusicBrainz::Track->new();
my $response = $ws_track->search({ TITLE => 'Soak Up The Sun', ARTIST => 'Sheryl Crow' });
my @tracklist = $response->track_list();
foreach my $track (@tracklist){
    # 検索された1曲のデータ
    print "\n".$track->artist()->name().', '.$track->title()."\n";
    my $releases = $track->release_list()->releases();
    foreach my $release (@$releases){
        # 対象曲の含まれるアルバムのデータ
        if(!defined($release->type()) && !defined($release->title())){ next; }
        if(!defined($release->type())){ print '  *****> '.$release->title(); }
        else{ print "  ".$release->type().'> '.$release->title(); }
        print ", ".($release->track_list()->offset()+1)."/".($release->track_list()->count()+1);
        print "\n";
#        print Data::Dumper->Dumper(\$release);
        {
            # 該当アルバムのデータを検索し、含まれる全ての曲を表示する
            my $ws_album = WebService::MusicBrainz::Release->new();
            my $album_response = $ws_album->search({ MBID => $release->id(), INC => 'tracks'  });
            my $album_tracks = $album_response->release()->track_list()->tracks();
            foreach my $album_track (@$album_tracks)
            {
                print "    ".$album_track->title()."\n";
            }
        }
    }
}

実行結果の例

Sheryl Crow, Soak Up the Sun
  Album> C'mon, C'mon, 2/15
    Steve McQueen
    Soak Up the Sun
    You're an Original
    Safe and Sound
    C'mon C'mon
    It's So Easy
    Over You
    Lucky Kid
    Diamond Road
    It's Only Love
    Abilene
    Hole in My Pocket
    Weather Channel
    Missing
 
Sheryl Crow, Soak Up the Sun
  Album> C'mon, C'mon, 2/14
    Steve McQueen
    Soak Up the Sun
    You're an Original
    Safe and Sound
    C'mon C'mon
    It's So Easy
    Over You
    Lucky Kid
    Diamond Road
    It's Only Love
    Abilene
    Hole in My Pocket
    Weather Channel
  Compilation> Bravo: The Hits 2002, Part 1, 13/21
    Dreamer
    Something About Us (Latin radio edit)
    Perdono
    Moi... Lolita (single version)
    Hot in Herre
    I'm Gonna Be Alright (Track Masters remix)
    When You Look at Me
    Hero
    What If
    Paid My Dues
    Nasty Girl
    In This World (single version)
    Soak Up the Sun
    Stillness of Heart (edit)
    It's Ok!
    Foolish
    I'm Not a Girl, Not Yet a Woman
    Girlfriend