source: flacrip/trunk/lib/MusicBrainz.pm @ 13

Last change on this file since 13 was 12, checked in by peter, 11 years ago
  • flactrack aborts if its call to get_musicbrainz_info() returns undef
  • added a select_release() function to MusicBrainz.pm that has the user interactively select the release; causes get_musicbrainz_info() to return undef if the user decides to cancel
  • removed a bunch of old, commented out code from MusicBrainz.pm
File size: 4.8 KB
Line 
1package MusicBrainz;
2
3use strict;
4use warnings;
5
6our @ISA    = qw{Exporter};
7our @EXPORT = qw{get_musicbrainz_info lookup_release};
8
9#use WebService::MusicBrainz;
10use LWP;
11use XML::XPath;
12use XML::XPath::XMLParser;
13
14sub lookup_release {
15    my ($discid) = @_;
16    my $ua = LWP::UserAgent->new;
17
18    #my $uri = URI->new('http://musicbrainz.org/ws/1/release/');
19    #$uri->query_form(type => 'xml', discid => $discid);
20    my $uri = URI->new("http://musicbrainz.org/ws/2/discid/$discid");
21    $uri->query_form(inc => 'artists+labels+recordings+release-groups+artist-credits');
22
23    my $res = $ua->get($uri);
24    # pause for a second, so we don't run afoul of the MusicBrainz API TOS
25    sleep 1;
26
27    warn $res->status_line, "\n" if $res->code != 200;
28    return if $res->code >= 400;
29    #TODO: if we get a 5xx error, retry?
30
31    return $res->decoded_content;
32}
33
34sub select_release {
35    my ($xpath, $discid) = @_;
36
37    # get the release; if there is more than one, take the first one
38    my $release_count = $xpath->findvalue('count(//release)');
39    my @releases = $xpath->findnodes('//release');
40    my $base = 'http://musicbrainz.org/release/';
41
42    my $i = 1;
43    # present the user with an interactive menu to pick/confirm the correct release ID
44    warn "$release_count release(s) found matching $discid\n";
45    for my $release (@releases) {
46        warn sprintf "%2d) $base%s %s %s (%s)\n", 
47            $i++,
48            $xpath->findvalue('@id', $release)->value,
49            $xpath->findvalue('.//label-info/label/name', $release)->value,
50            $xpath->findvalue('.//label-info/catalog-number', $release)->value,
51            $xpath->findvalue('barcode', $release)->value;
52    }
53
54    my $selection = 0;
55
56    while ($selection < 1 || $selection > $release_count) {
57        print STDERR "Select a release (1-$release_count): ";
58        $selection = <STDIN>;
59        chomp $selection;
60        return if $selection =~ /^q/i;
61    }
62
63    return $releases[$selection - 1];
64}
65
66sub get_musicbrainz_info {
67    my ($discid) = @_;
68    my %info;
69
70    #TODO: deprecate the old MBZ tag name
71    $info{MBZ_DISCID} = $discid;
72    $info{MUSICBRAINZ_DISCID} = $discid;
73
74    my $xpath = XML::XPath->new();
75    my $xml = lookup_release($discid) || return;
76   
77    $xpath->set_xml($xml);
78
79    # use the VorbisComment names from here http://musicbrainz.org/doc/MusicBrainz_Picard/Tags/Mapping
80
81    #my $release = $releases[0];
82    my $release = select_release($xpath, $discid);
83    return unless $release;
84
85    $info{MUSICBRAINZ_ALBUMID} = $xpath->findvalue('@id', $release)->value;
86    $info{ALBUM}               = $xpath->findvalue('title', $release)->value;
87    @info{qw{ALBUMARTIST ALBUMARTISTSORT}} = get_artist_credits($xpath, $release);
88    $info{DATE}                = $xpath->findvalue('date', $release)->value;
89    $info{ORIGINALDATE}        = $xpath->findvalue('release-group/first-release-date', $release)->value;
90
91    # select the proper medium (important for multidisc releases)
92    my ($medium) = $xpath->findnodes("medium-list/medium[disc-list/disc/\@id='$discid']", $release);
93
94    # disc position info
95    $info{DISCNUMBER} = $xpath->findvalue('position', $medium)->value;
96    $info{DISCTOTAL}  = $xpath->findvalue('../@count', $medium)->value;
97
98    #my $ua = LWP::UserAgent->new;
99    my $tracknum = 1;
100    for my $track_node ($xpath->findnodes('track-list/track', $medium)) {
101        my $prefix = sprintf('TRACK%02d', $tracknum);
102
103        $info{"$prefix.MUSICBRAINZ_TRACKID"} = $xpath->findvalue('@id', $track_node)->value;
104
105        my ($recording) = $xpath->findnodes('recording', $track_node);
106        $info{"$prefix.MUSICBRAINZ_RECORDINGID"} = $xpath->findvalue('@id', $recording)->value;
107        $info{"$prefix.TITLE"} = $xpath->findvalue('title', $recording)->value;
108        @info{"$prefix.ARTIST", "$prefix.ARTISTSORT"} = get_artist_credits($xpath, $recording);
109
110        $info{TRACKS}[$tracknum]{TITLE} = $info{"$prefix.TITLE"};
111        $info{TRACKS}[$tracknum]{ARTIST} = $info{"$prefix.ARTIST"};
112        $info{TRACKS}[$tracknum]{ARTISTSORT} = $info{"$prefix.ARTISTSORT"};
113
114        #my $uri = URI->new("http://musicbrainz.org/ws/2/recording/$recording_mbid");
115        #$uri->query_form(inc => 'artists');
116        #my $res = $ua->get($uri);
117        #die $res->decoded_content;
118
119        #TODO: get track relations (Covers, etc.)
120
121        $tracknum++;
122    }
123
124    return \%info;
125}
126
127sub get_artist_credits {
128    my ($xpath, $context_node) = @_;
129
130    # use the MusicBrainz join phrase to build up the multiple artist credits
131    my ($credit, $sort_credit) = ('', '');
132    for my $credit_node ($xpath->findnodes('artist-credit/name-credit', $context_node)) {
133        $credit      .= $xpath->findvalue('concat(artist/name, @joinphrase)', $credit_node)->value;
134        $sort_credit .= $xpath->findvalue('concat(artist/sort-name, @joinphrase)', $credit_node)->value;
135    }
136
137    return ($credit, $sort_credit);
138}
139
140# module return
1411;
Note: See TracBrowser for help on using the repository browser.