Feature request: save fetched lyrics

Forums:

Hi,

when moc-lyrics.pl finds a lyrics file online, the file is displayed inside moc using less.

Could a feature be added to moc to save the lyrics file?

Thanks,
eigernorth

That's really question for author of the contributed script but I happen to have made modification to it on my own just to allow saving. It puts lyrics in a file with the same name as the track but without extension which can be accessed by pressing "L". It will not overwrite existing files (I hope).

Note that right now MOC has a bit annoying behaviour that it checks for the presence of the lyrics file at the beginning of playback. So, if you grabbed lyrics file while listening to the song, you have to re-play it for MOC to see it. I have a patch for this also, if you wish to try and compile your own version of MOC.

<br /> #!/usr/bin/perl -w<br /> #===============================================================================#{{{<br /> #<br /> # FILE: moc-lyrics_tg.pl<br /> #<br /> # USAGE: ./moc-lyrics_tg.pl<br /> # -a ARTIST<br /> # -t TITLE<br /> # -f FILENAME<br /> # In short: ExecCommand1 = "[path]/moc-lyrics_tg.pl -a %r -t %t -f %f"<br /> #<br /> # DESCRIPTION: Retrieves lyrics from some sites<br /> # To customize its behaviour have a look at the variables under<br /> # "Definitions"<br /> #<br /> # NOTES: Lyrics::Fetcher is free software by David Precious<br /> # (URL: http://search.cpan.org/~bigpresh/)<br /> # AUTHOR: kb, <unixprog@gmail.com>, modified by tomaszg@alpha.uwb.edu.pl<br /> #===============================================================================#}}}</p> <p>use Lyrics::Fetcher;<br /> use Encode;<br /> use Getopt::Std;<br /> use File::Basename;</p> <p>getopt('atf', \%opts);</p> <p>if (!($opts{'a'} &amp;& $opts{'t'} &amp;& $opts{'f'})){<br /> die "Usage: -a artist -t track -f filename\n";}</p> <p>$artist=$opts{'a'};<br /> $title=$opts{'t'};<br /> ($filename,$dir) = fileparse($opts{'f'}, qr/\.[^.]*/);<br /> #$filename=$opts{'f'};</p> <p>if (-e $dir.$filename) { die "File exists!\n";}</p> <p>##################################################<br /> # Definitions<br /> ###################################################{{{<br /> our $INDENT = 4; # Number of spaces for indentation (if any)<br /> our $HEADING = 1; # Print a heading?<br /> our $SOURCE = 1; # Show source of lyrics?<br /> our $PAGER = 'less'; # Pager to show lyrics (less,more,most,less.sh,info,cat etc.)<br /> our $TRYHARD = 1; # Try harder by changing case if nothing is found? (this means<br /> # a lot of bandwith usage, use with care on slow connections)<br /> our $DEBUG = 1; # Show debug warnings?</p> <p># Comment in/out what you want. order does matter.<br /> # For more check out Lyrics::Fetcher's CPAN page<br /> our @sources = (<br /> 'LyricWiki',<br /> 'AstraWeb',<br /> #'AZLyrics',<br /> #'LyricsNet',<br /> #'LyricsTime',<br /> #'LeosLyrics',<br /> #'LyrDB',<br /> #'Lyrics007',<br /> #'LyricsDownload',<br /> #'Google',<br /> );<br /> #}}}</p> <p>##################################################<br /> # Actual lyrics catching<br /> ###################################################{{{<br /> our $lyrics; # Lyrics as string (if any)<br /> our $source; # Source of the lyrics (if any)</p> <p>&amp;get_lyrics( $artist, $title );</p> <p># Catch lyrics for every source, until one yields a result<br /> sub get_lyrics {<br /> my ($artist, $title) = @_;<br /> foreach my $site (@sources) {<br /> warn "Trying $site" if $DEBUG;<br /> $lyrics = Lyrics::Fetcher->fetch( $artist, $title, $site );<br /> if ($lyrics) {<br /> $source = $site;<br /> last;<br /> }<br /> warn "Found nothing @ $site" if $DEBUG;<br /> }<br /> }<br /> #}}}</p> <p>##################################################<br /> # Heuristic stuff<br /> ###################################################{{{</p> <p># Try harder<br /> if ($TRYHARD) {<br /> # try it with every starting letter uppercase<br /> # (e.g. 'Law of Average' => 'Law Of Average')<br /> unless ($lyrics) { #title<br /> my $title_guess = &amp;upcase_first( $title );<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist, $title_guess );<br /> }<br /> unless ($lyrics) { #artist<br /> my $artist_guess = &amp;upcase_first( $artist );<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title );<br /> }<br /> unless ($lyrics) { #both<br /> my $artist_guess = &amp;upcase_first( $artist );<br /> my $title_guess = &amp;upcase_first( $title );<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title_guess );<br /> }</p> <p> # try it with every starting letter uppercase but making<br /> # all other letters lowercase<br /> # (e.g. 'LAW oF AVERAGE' => 'Law Of Average')<br /> unless ($lyrics) { #title<br /> my $title_guess = &amp;upcase_first( lc($title) );<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist, $title_guess );<br /> }<br /> unless ($lyrics) { #artist<br /> my $artist_guess = &amp;upcase_first( lc($artist) );<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title );<br /> }<br /> unless ($lyrics) { #both<br /> my $artist_guess = &amp;upcase_first( lc($artist) );<br /> my $title_guess = &amp;upcase_first( lc($title) );<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title_guess );<br /> }</p> <p> # try it with all-caps (i.e. 'ac/dc' => 'AC/DC')<br /> unless ($lyrics) { #title<br /> my $title_guess = uc($title);<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist, $title_guess );<br /> }<br /> unless ($lyrics) { #artist<br /> my $artist_guess = uc($artist);<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title );<br /> }<br /> unless ($lyrics) { #both<br /> my $artist_guess = uc($artist);<br /> my $title_guess = uc($title);<br /> warn "Rewritten: '$artist' => '$artist_guess'" if $DEBUG;<br /> warn "Rewritten: '$title' => '$title_guess'" if $DEBUG;<br /> &amp;get_lyrics( $artist_guess, $title_guess );<br /> }<br /> }<br /> die "No lyrics found, sorry\n" unless $lyrics;</p> <p># Make first letter of every word in string uppercase<br /> sub upcase_first {<br /> my $str = shift;<br /> my @tok_up = map(ucfirst, split(/ /, $str));<br /> my $str_up = join( ' ', @tok_up );<br /> return $str_up<br /> }<br /> #}}}</p> <p>##################################################<br /> # Formatting stuff<br /> ###################################################{{{<br /> if ($HEADING) {<br /> my $header = decode('utf8', "$artist - $title\n");<br /> $header .= "=" x length($header) . "\n";<br /> $lyrics = $header . $lyrics;<br /> }</p> <p>if ($SOURCE) {<br /> $lyrics .= "\n\n[ retrieved from $source ]\n";<br /> }</p> <p>if ($INDENT) {<br /> $lyrics = &amp;indent( $lyrics );<br /> sub indent {<br /> my $lyrics = shift;<br /> my $prepend = ' ' x $INDENT;<br /> my $temp;<br /> foreach( split( "\n", $lyrics ) ) {<br /> s/^/$prepend/;<br /> $temp .= $_ . "\n";<br /> }<br /> return $temp;<br /> }<br /> }<br /> #}}}</p> <p>open $file, ">", $dir.$filename;<br /> print $file $lyrics;<br /> close $file;</p> <p>exit 0;<br />