lyrics fetcher (perl script)

Forums:

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,
# 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 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 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

Hi kb,

it might have been obvious to you, but it wasn't to me:

can you please say in your description that the script only works when the mp3 files are tagged with Artist and track Name?

Banging my head against the wall here!

Many thanks,
eighernorth

Hi,

I installed this plugin but I get the following error when executing it:

Bareword "usage" not allowed while "strict subs" in use at /home/dakkar/bin/moc-lyrics.pl line 30.
Execution of /home/dakkar/bin/moc-lyrics.pl aborted due to compilation errors.

Any help will be appreciated.

Thanks
DalaiDakkar

If you're like I was and thought that Perl is something you do when you're knitting then you might have had difficulty getting this to work.

If you try,

sudo perl -MCPAN -e shell

and at the prompt,

reload index

followed by,

install Getopt::Lazy

install YAML

install Lyrics::Fetcher

install Lyrics::Fetcher::LyricWiki

install Lyrics::Fetcher::AZLyrics

install Lyrics::Fetcher::AstraWeb

and remove # from in front of the relevant sources in moc-lyrics.pl then you should have all the requirements. You can see what Lyric fetchers are installed by typing

perl -MLyrics::Fetcher -e 'print Lyrics::Fetcher->available_fetchers'

and you can test the script by typing

perl -MLyrics::Fetcher -e 'print Lyrics::Fetcher->fetch("Captain Beefheart and the Magic Band", "Ice Rose");'

or

perl /path/to/moc-lyrics.pl -a "Captain Beefheart And The Magic Band" -t "Ice Rose"

Happy Christmas

Hi kb,

it might have been obvious to you, but it wasn't to me:

can you please say in your description that the script only works when
the mp3 files are tagged with Artist and track Name?

Banging my head against the wall here!

Otherwise: nice script.

Many thanks,
eighernorth