lyrics fetcher (perl script)

If you want to see the lyrics for the song you're listening to, you might be interested in this script. It's a quick hack (I do only quick hacks :D), but it works at least for the most popular tracks.

It's a frontend for David Precious' Lyrics::Fetcher CPAN Module, that outputs the lyrics to a pager of your choice. To use it, you can map a function key to it, e.g. F1, select a track, hit it and hope that it's found:

ExecCommand1 = "/home/kb/bin/moc-lyrics --artist %r --title %t"

Here it is:

#!/usr/bin/perl 
#===============================================================================
#
#         FILE:  moc-lyrics.pl
#
#        USAGE:  ./moc-lyrics.pl 
#
#  DESCRIPTION:  Retrieves lyrics from some sites
#
#        NOTES:  Lyrics::Fetcher is free software by David Precious
#                (URL: http://search.cpan.org/~bigpresh/)
#       AUTHOR:  kb, <unixprog@gmail.com>
#      COMPANY:  "freelance" :-)
#      VERSION:  ---
#      CREATED:  12/02/2007 11:08:50 PM CET
#     REVISION:  $Id: moc-lyrics.pl 14 2008-04-04 17:46:38Z kb $
#===============================================================================

use strict;
use warnings;
use Lyrics::Fetcher;
use Encode;
use Getopt::Lazy
    'artist|a=s'  => 'Artist',
    'title|t=s'   => 'Song Title',
    -summary    => 'Looks up lyrics in LyricWiki',
    -usage      => '%c %o',
;
getopt();
exit usage unless ($artist && $title);

our $INDENT  = 4;      # Number of spaces for indentation (if any)
our $HEADING = 1;      # Print a Heading?
our $SOURCE  = 1;      # Show Source?
our $PAGER   = 'less'; # Pager to show lyrics in

our $lyrics;
our $source;
# Comment in/out what you want. order does matter.
# For more check out Lyrics::Fetcher's CPAN page
our @sources = (
    'LyricWiki',
    #'AZLyrics',
    #'AstraWeb',
    #'LyricsNet',
    #'LyricsTime',
);

sub get_lyrics {
    my $source = shift;
    return Lyrics::Fetcher->fetch( $artist, $title, $source );
}

# Catch lyrics for every source, until one yields a result
foreach my $site (@sources) {
    $lyrics = get_lyrics($source);
    if ($lyrics) {
        $source = $site;
        last;
    }
}

die "No lyrics found, sorry\n" unless $lyrics;
    
if ($HEADING) {
    my $header  = decode('utf8', "$artist - $title\n");
    $header .= "=" x length($header) . "\n";
    $lyrics = $header . $lyrics;
}

if ($SOURCE) {
    $lyrics .= "\n\n[ retrieved from $source ]\n";
}

if ($INDENT) {
    $lyrics = indent( $lyrics );
    sub indent {
        my $lyrics = shift;
        my $prepend = ' ' x $INDENT;
        my $temp;
        foreach( split( "\n", $lyrics ) ) {
            s/^/$prepend/;
            $temp .= $_ . "\n";
        }
        return $temp;
    }
}

open LESS, "|$PAGER";
print LESS $lyrics;
close LESS;

exit 1;

I forgot: If anyone has a

I forgot: If anyone has a better solution for a script, that gets lyrics for a song from the net and outputs it to STDOUT, I'd be happy to know. I would even be glad about links to lyrics sites, that are comprehensive and machine-accessible.

There are actually a lot of sites that offer lyrics, but most of them are crammed with ads and hard to parse with a script.

Thanks for the script! Could

Thanks for the script! Could you please send me it as an attachment by mail so I can put it in the contrib section to download? After copy & paste from the forum the formatting is bad.

--
Damian Pietras - MOC developer