Changeset 3 in flacrip for trunk/flactrack


Ignore:
Timestamp:
01/27/12 21:58:19 (12 years ago)
Author:
peter
Message:

moved the MusicBrainz info lookup code into a common MusicBrainz.pm module, to be used by both the mbz and flactrack scripts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/flactrack

    r1 r3  
    88 
    99use Getopt::Long; 
     10use MusicBrainz; 
    1011 
    1112GetOptions( 
     
    2122 
    2223# for getting track metadata from MusicBrainz 
    23 my %info; 
     24my $info; 
    2425if ($TYPE eq 'mp3') { 
    2526    require Audio::FLAC::Header; 
     
    3233    #TODO: calculate TOC and DISCID from cuesheet if there is no MBZ_DISCID tag present 
    3334 
    34     #TODO: use the functions in mbz instead of repeating them here) 
    35     %info = get_musicbrainz_info($discid); 
     35    $info = get_musicbrainz_info($discid); 
    3636} 
    3737 
     
    5454        $cmd .= qq{| lame -b 192}; 
    5555        # if there is track info, add it as ID3 tags 
    56         if (%info) { 
    57             my $track = $info{TRACKS}[$tracknum]; 
     56        if ($info) { 
     57            my $track = $info->{TRACKS}[$tracknum]; 
    5858            $cmd .= sprintf q{ --tt %s --ta %s --tl %s --tn %d}, 
    59                 quote($$track{TITLE}), 
    60                 quote($$track{ARTIST}), 
    61                 quote($info{ALBUM}), 
     59                quote($track->{TITLE}), 
     60                quote($track->{ARTIST}), 
     61                quote($info->{ALBUM}), 
    6262                $tracknum; 
    6363        } 
     
    7979    return qq{"$string"}; 
    8080} 
    81  
    82  
    83 # make the output terminal handle UTF-8 characters 
    84 #binmode STDOUT, ':utf8'; 
    85 #my $info = get_musicbrainz_info($discid); 
    86 #for my $key (sort keys %{ $info }) { 
    87 #    print "$key=$$info{$key}\n"; 
    88 #} 
    89  
    90 sub lookup_release { 
    91     my ($discid) = @_; 
    92     my $ua = LWP::UserAgent->new; 
    93  
    94     my $uri = URI->new('http://musicbrainz.org/ws/1/release/'); 
    95     $uri->query_form(type => 'xml', discid => $discid); 
    96  
    97     my $res = $ua->get($uri); 
    98     return $res->decoded_content; 
    99 } 
    100  
    101 sub get_musicbrainz_info { 
    102     my ($discid) = @_; 
    103     my %info; 
    104  
    105     $info{MBZ_DISCID} = $discid; 
    106  
    107     my $xpath = XML::XPath->new(); 
    108  
    109     $xpath->set_xml(lookup_release($discid)); 
    110  
    111     # TODO: check for more than 1 release? 
    112  
    113     $info{MB_RELEASE_ID} = $xpath->findvalue('//release/@id'); 
    114     $info{ALBUM}         = $xpath->findvalue('//release/title'); 
    115     $info{ARTIST}        = $xpath->findvalue('//release/artist/name'); 
    116     $info{TRACKS}        = []; 
    117  
    118     # TODO: get release date 
    119  
    120     my $tracknum = 1; 
    121     for my $track_node ($xpath->findnodes('//track-list/track')) { 
    122         $info{TRACKS}[$tracknum]{MB_TRACKID} = $xpath->findvalue('@id', $track_node); 
    123         $info{TRACKS}[$tracknum]{TITLE}      = $xpath->findvalue('title', $track_node); 
    124         $info{TRACKS}[$tracknum]{ARTIST}     = $xpath->findvalue('artist/name', $track_node) || $info{ARTIST}; 
    125         $tracknum++; 
    126     } 
    127  
    128     return %info; 
    129 } 
Note: See TracChangeset for help on using the changeset viewer.