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

Last change on this file since 16 was 16, checked in by peter, 10 years ago

Include language and script in the metadata fetched by mbz

File size: 5.0 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    $info{LANGUAGE}            = $xpath->findvalue('text-representation/language', $release)->value;
91    $info{SCRIPT}              = $xpath->findvalue('text-representation/script', $release)->value;
92
93    # select the proper medium (important for multidisc releases)
94    my ($medium) = $xpath->findnodes("medium-list/medium[disc-list/disc/\@id='$discid']", $release);
95
96    # disc position info
97    $info{DISCNUMBER} = $xpath->findvalue('position', $medium)->value;
98    $info{DISCTOTAL}  = $xpath->findvalue('../@count', $medium)->value;
99
100    #my $ua = LWP::UserAgent->new;
101    my $tracknum = 1;
102    for my $track_node ($xpath->findnodes('track-list/track', $medium)) {
103        my $prefix = sprintf('TRACK%02d', $tracknum);
104
105        $info{"$prefix.MUSICBRAINZ_TRACKID"} = $xpath->findvalue('@id', $track_node)->value;
106
107        my ($recording) = $xpath->findnodes('recording', $track_node);
108        $info{"$prefix.MUSICBRAINZ_RECORDINGID"} = $xpath->findvalue('@id', $recording)->value;
109        $info{"$prefix.TITLE"} = $xpath->findvalue('title', $recording)->value;
110        @info{"$prefix.ARTIST", "$prefix.ARTISTSORT"} = get_artist_credits($xpath, $recording);
111
112        $info{TRACKS}[$tracknum]{TITLE} = $info{"$prefix.TITLE"};
113        $info{TRACKS}[$tracknum]{ARTIST} = $info{"$prefix.ARTIST"};
114        $info{TRACKS}[$tracknum]{ARTISTSORT} = $info{"$prefix.ARTISTSORT"};
115
116        #my $uri = URI->new("http://musicbrainz.org/ws/2/recording/$recording_mbid");
117        #$uri->query_form(inc => 'artists');
118        #my $res = $ua->get($uri);
119        #die $res->decoded_content;
120
121        #TODO: get track relations (Covers, etc.)
122
123        $tracknum++;
124    }
125
126    return \%info;
127}
128
129sub get_artist_credits {
130    my ($xpath, $context_node) = @_;
131
132    # use the MusicBrainz join phrase to build up the multiple artist credits
133    my ($credit, $sort_credit) = ('', '');
134    for my $credit_node ($xpath->findnodes('artist-credit/name-credit', $context_node)) {
135        $credit      .= $xpath->findvalue('concat(artist/name, @joinphrase)', $credit_node)->value;
136        $sort_credit .= $xpath->findvalue('concat(artist/sort-name, @joinphrase)', $credit_node)->value;
137    }
138
139    return ($credit, $sort_credit);
140}
141
142# module return
1431;
Note: See TracBrowser for help on using the repository browser.