Changeset 43 in flacrip


Ignore:
Timestamp:
07/02/15 15:41:41 (9 years ago)
Author:
peter
Message:

fallback to an interactive selection of the release if there are multiple hits with the same barcode and disc ID

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/MusicBrainz.pm

    r40 r43  
    3232} 
    3333 
     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 
    3462sub select_release { 
    3563    my ($xpath, $params) = @_; 
     
    4169        # try to pick the release automatically by the barcode 
    4270        my @releases = $xpath->findnodes("//release[barcode='$barcode']"); 
    43         die "Found no releases with discid $discid and barcode $barcode\n" unless @releases; 
    44         die "Found more than one release with discid $discid and barcode $barcode\n" if @releases > 1; 
     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        } 
    4578        return $releases[0]; 
    4679    } else { 
    4780        my $release_count = $xpath->findvalue('count(//release)'); 
    4881        my @releases = $xpath->findnodes('//release'); 
    49         my $base = 'http://musicbrainz.org/release/'; 
    50  
    51         my $i = 1; 
    52         # present the user with an interactive menu to pick/confirm the correct release ID 
    53         warn "$release_count release(s) found matching $discid\n"; 
    54         for my $release (@releases) { 
    55             warn sprintf "%2d) $base%s %s %s (%s)\n",  
    56             $i++, 
    57             $xpath->findvalue('@id', $release)->value, 
    58             $xpath->findvalue('.//label-info/label/name', $release)->value, 
    59             $xpath->findvalue('.//label-info/catalog-number', $release)->value, 
    60             $xpath->findvalue('barcode', $release)->value; 
    61         } 
    62  
    63         my $selection = 0; 
    64  
    65         while ($selection < 1 || $selection > $release_count) { 
    66             print STDERR "Select a release (1-$release_count): "; 
    67             $selection = <STDIN>; 
    68             chomp $selection; 
    69             return if $selection =~ /^q/i; 
    70         } 
    71  
    72         return $releases[$selection - 1]; 
     82        return interactive_select_release($xpath, $release_count, $discid, @releases); 
    7383    } 
    7484} 
Note: See TracChangeset for help on using the changeset viewer.