Changeset 12 in flacrip for trunk/lib


Ignore:
Timestamp:
05/15/14 20:37:05 (11 years ago)
Author:
peter
Message:
  • 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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/MusicBrainz.pm

    r11 r12  
    3232} 
    3333 
     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 
    3466sub get_musicbrainz_info { 
    3567    my ($discid) = @_; 
     
    4577    $xpath->set_xml($xml); 
    4678 
    47     # get the release; if there is more than one, take the first one 
    48     my $release_count = $xpath->findvalue('count(//release)'); 
    49     my @releases = $xpath->findnodes('//release'); 
    50     my $base = 'http://musicbrainz.org/release/'; 
    51  
    52     my $i = 1; 
    53     #TODO: use this as the basis for an interactive menu to pick the correct release ID 
    54     warn "$release_count release(s) found matching $discid\n"; 
    55     for my $release (@releases) { 
    56         warn sprintf "%2d) $base%s %s %s (%s)\n",  
    57             $i++, 
    58             $xpath->findvalue('@id', $release)->value, 
    59             $xpath->findvalue('.//label-info/label/name', $release)->value, 
    60             $xpath->findvalue('.//label-info/catalog-number', $release)->value, 
    61             $xpath->findvalue('barcode', $release)->value; 
    62     } 
    63  
    6479    # use the VorbisComment names from here http://musicbrainz.org/doc/MusicBrainz_Picard/Tags/Mapping 
    6580 
    66     # use the first release by default 
    67     # TODO: configurable release selection criteria 
    68     my $release = $releases[0]; 
     81    #my $release = $releases[0]; 
     82    my $release = select_release($xpath, $discid); 
     83    return unless $release; 
    6984 
    7085    $info{MUSICBRAINZ_ALBUMID} = $xpath->findvalue('@id', $release)->value; 
     
    125140# module return 
    1261411; 
    127  
    128 =begin MBZ API version 1 
    129  
    130 sub lookup_release { 
    131     my ($discid) = @_; 
    132     my $ua = LWP::UserAgent->new; 
    133  
    134     my $uri = URI->new('http://musicbrainz.org/ws/1/release/'); 
    135     $uri->query_form(type => 'xml', discid => $discid); 
    136  
    137     my $res = $ua->get($uri); 
    138     return $res->decoded_content; 
    139 } 
    140  
    141 sub get_musicbrainz_info { 
    142     my ($discid) = @_; 
    143     my %info; 
    144  
    145     $info{MBZ_DISCID} = $discid; 
    146  
    147     my $xpath = XML::XPath->new(); 
    148  
    149     $xpath->set_xml(lookup_release($discid)); 
    150  
    151     # TODO: check for more than 1 release? 
    152  
    153     $info{MB_RELEASE_ID} = $xpath->findvalue('//release/@id'); 
    154     $info{ALBUM}         = $xpath->findvalue('//release/title'); 
    155     $info{ARTIST}        = $xpath->findvalue('//release/artist/name'); 
    156     $info{TRACKS}        = []; 
    157  
    158     # TODO: get release date 
    159  
    160     my $tracknum = 1; 
    161     for my $track_node ($xpath->findnodes('//track-list/track')) { 
    162         $info{TRACKS}[$tracknum]{MB_TRACKID} = $xpath->findvalue('@id', $track_node); 
    163         $info{TRACKS}[$tracknum]{TITLE}      = $xpath->findvalue('title', $track_node); 
    164         $info{TRACKS}[$tracknum]{ARTIST}     = $xpath->findvalue('artist/name', $track_node) || $info{ARTIST}; 
    165         $tracknum++; 
    166     } 
    167  
    168     return %info; 
    169 } 
    170  
    171 =cut 
    172  
    173 =begin WebService::MusicBrainz code 
    174  
    175     my $ws_artists = WebService::MusicBrainz->new_artist; 
    176     my $ws_releases = WebService::MusicBrainz->new_release; 
    177     my $ws_tracks = WebService::MusicBrainz->new_track; 
    178  
    179     # search on the discid 
    180     my $response = $ws_releases->search({ DISCID => $discid }); 
    181  
    182     # save this object, since WS::MBZ deletes it when you fetch it 
    183     # TODO: bug report to WS::MBZ? 
    184     my $release = $response->release; 
    185  
    186     # return undef if there is no matching release for this DiscID 
    187     return unless defined $release; 
    188  
    189     # search again, using the MBID of the first release found 
    190     # TODO: deal with multiple releases found? 
    191     # include tracks and artist info 
    192     $response = $ws_releases->search({ 
    193         MBID => $release->id,  
    194         INC => 'discs tracks artist release-events counts', 
    195     }); 
    196  
    197     # get the fully filled out Release object (that represents the disc) 
    198     $release = $response->release; 
    199  
    200     if (defined $release->artist) { 
    201         $info{ARTIST} = $release->artist->name; 
    202     } 
    203     if (defined $release->title) { 
    204         $info{ALBUM} = $release->title; 
    205     } 
    206  
    207     # this is ID3v2:TDRL = Release Date 
    208     # (for now we just take the first date) 
    209     my $release_date = eval { @{ $release->release_event_list->events }[0]->date }; 
    210     $release_date = '' if $@; 
    211  
    212     $info{DATE} = $release_date; 
    213  
    214     # get full info on each of the tracks 
    215     my @tracks; 
    216     my $track_num = 1; 
    217     for my $track_id (map { $_->id } @{ $release->track_list->tracks }) { 
    218         my $response = $ws_tracks->search({  
    219             MBID => $track_id, 
    220             INC => 'artist track-rels', 
    221         }); 
    222         my $track = $response->track; 
    223         my $prefix = sprintf('TRACK%02d', $track_num); 
    224         $info{"$prefix.TITLE"} = $track->title; 
    225         #if (defined $track->artist && $track->artist->name ne $release->artist->name) { 
    226             $info{"$prefix.ARTIST"} = $track->artist->name; 
    227             $info{"$prefix.DATE"} = $release_date; 
    228         #} 
    229         push @tracks, $track; 
    230  
    231  
    232         if (defined $track->relation_list) { 
    233             for my $relation (@{ $track->relation_list->relations }) { 
    234                 #warn $relation->type, $relation->target; 
    235                 my $response = $ws_tracks->search({ 
    236                     MBID => $relation->target, 
    237                     INC => 'artist releases', 
    238                 }); 
    239                 my $track = $response->track; 
    240                 $info{"$prefix.ORIGINAL_ARTIST"} = $track->artist->name; 
    241                 $info{"$prefix.ORIGINAL_ALBUM"} =  
    242                     ( (@{ $track->release_list->releases })[0]->title ); 
    243             } 
    244         } 
    245  
    246         $track_num++; 
    247     } 
    248  
    249 =cut 
Note: See TracChangeset for help on using the changeset viewer.