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

Last change on this file since 46 was 45, checked in by peter, 9 years ago

if the barcode element is present but empty, return '[none]' as the value of the BARCODE tag

File size: 5.8 KB
RevLine 
[3]1package MusicBrainz;
2
3use strict;
4use warnings;
5
6our @ISA    = qw{Exporter};
[8]7our @EXPORT = qw{get_musicbrainz_info lookup_release};
[3]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
[43]34sub interactive_select_release {
35    my ($xpath, $release_count, $discid, @releases) = @_;
36    my $base = 'http://musicbrainz.org/release/';
37
38    my $i = 1;
39    # present the user with an interactive menu to pick/confirm the correct release ID
40    warn "$release_count release(s) found matching $discid\n";
41    for my $release (@releases) {
42        warn sprintf "%2d) $base%s %s %s (%s)\n", 
43        $i++,
44        $xpath->findvalue('@id', $release)->value,
45        $xpath->findvalue('.//label-info/label/name', $release)->value,
46        $xpath->findvalue('.//label-info/catalog-number', $release)->value,
47        $xpath->findvalue('barcode', $release)->value;
48    }
49
50    my $selection = 0;
51
52    while ($selection < 1 || $selection > $release_count) {
53        print STDERR "Select a release (1-$release_count): ";
54        $selection = <STDIN>;
55        chomp $selection;
56        return if $selection =~ /^q/i;
57    }
58
59    return $releases[$selection - 1];
60}
61
[12]62sub select_release {
[40]63    my ($xpath, $params) = @_;
[3]64
[40]65    my $discid = $params->{discid};
66    my $barcode = $params->{barcode};
[3]67
[40]68    if ($barcode) {
69        # try to pick the release automatically by the barcode
70        my @releases = $xpath->findnodes("//release[barcode='$barcode']");
[43]71        if (@releases > 1) {
72            warn "Found more than one release with discid $discid and barcode $barcode\n";
73            return interactive_select_release($xpath, scalar @releases, $discid, @releases);
74        } elsif (!@releases) {
75            warn "Found no releases with discid $discid and barcode $barcode\n";
76            return;
77        }
[40]78        return $releases[0];
79    } else {
80        my $release_count = $xpath->findvalue('count(//release)');
81        my @releases = $xpath->findnodes('//release');
[43]82        return interactive_select_release($xpath, $release_count, $discid, @releases);
[12]83    }
84}
85
86sub get_musicbrainz_info {
[40]87    my $params = shift;
88
89    my $discid = $params->{discid};
[12]90    my %info;
91
92    $info{MUSICBRAINZ_DISCID} = $discid;
93
94    my $xpath = XML::XPath->new();
95    my $xml = lookup_release($discid) || return;
96   
97    $xpath->set_xml($xml);
98
[8]99    # use the VorbisComment names from here http://musicbrainz.org/doc/MusicBrainz_Picard/Tags/Mapping
100
[12]101    #my $release = $releases[0];
[40]102    my $release = select_release($xpath, $params);
[12]103    return unless $release;
[8]104
105    $info{MUSICBRAINZ_ALBUMID} = $xpath->findvalue('@id', $release)->value;
106    $info{ALBUM}               = $xpath->findvalue('title', $release)->value;
[9]107    @info{qw{ALBUMARTIST ALBUMARTISTSORT}} = get_artist_credits($xpath, $release);
[8]108    $info{DATE}                = $xpath->findvalue('date', $release)->value;
109    $info{ORIGINALDATE}        = $xpath->findvalue('release-group/first-release-date', $release)->value;
[16]110    $info{LANGUAGE}            = $xpath->findvalue('text-representation/language', $release)->value;
111    $info{SCRIPT}              = $xpath->findvalue('text-representation/script', $release)->value;
[8]112
[45]113    # add the barcode tag if the element exists
114    # if the barcode tag is empty, it is a release that explicitly has no barcode
115    # use the dummy value "[none]" for those cases
116    if ($xpath->exists('barcode', $release)) {
117        $info{BARCODE} = $xpath->findvalue('barcode', $release)->value || '[none]';
118    }
119
[5]120    # select the proper medium (important for multidisc releases)
121    my ($medium) = $xpath->findnodes("medium-list/medium[disc-list/disc/\@id='$discid']", $release);
122
[10]123    # disc position info
124    $info{DISCNUMBER} = $xpath->findvalue('position', $medium)->value;
125    $info{DISCTOTAL}  = $xpath->findvalue('../@count', $medium)->value;
126
[9]127    #my $ua = LWP::UserAgent->new;
[3]128    my $tracknum = 1;
[5]129    for my $track_node ($xpath->findnodes('track-list/track', $medium)) {
[3]130        my $prefix = sprintf('TRACK%02d', $tracknum);
[8]131
[9]132        $info{"$prefix.MUSICBRAINZ_TRACKID"} = $xpath->findvalue('@id', $track_node)->value;
[8]133
[9]134        my ($recording) = $xpath->findnodes('recording', $track_node);
135        $info{"$prefix.MUSICBRAINZ_RECORDINGID"} = $xpath->findvalue('@id', $recording)->value;
136        $info{"$prefix.TITLE"} = $xpath->findvalue('title', $recording)->value;
137        @info{"$prefix.ARTIST", "$prefix.ARTISTSORT"} = get_artist_credits($xpath, $recording);
[8]138
[3]139        #my $uri = URI->new("http://musicbrainz.org/ws/2/recording/$recording_mbid");
140        #$uri->query_form(inc => 'artists');
141        #my $res = $ua->get($uri);
142        #die $res->decoded_content;
143
144        #TODO: get track relations (Covers, etc.)
145
146        $tracknum++;
147    }
148
149    return \%info;
150}
151
[9]152sub get_artist_credits {
153    my ($xpath, $context_node) = @_;
154
155    # use the MusicBrainz join phrase to build up the multiple artist credits
156    my ($credit, $sort_credit) = ('', '');
157    for my $credit_node ($xpath->findnodes('artist-credit/name-credit', $context_node)) {
158        $credit      .= $xpath->findvalue('concat(artist/name, @joinphrase)', $credit_node)->value;
159        $sort_credit .= $xpath->findvalue('concat(artist/sort-name, @joinphrase)', $credit_node)->value;
160    }
161
162    return ($credit, $sort_credit);
163}
164
[3]165# module return
1661;
Note: See TracBrowser for help on using the repository browser.