Hello, here's a script that polls periodically what's playing on KOIT, FM 96.5 in San Francisco (you can listen via the net), and finds lyrics from another site. Not all lyrics can be found, but most seem to be ok.

update. added \Q\E around $title since it can contain something like (.

#!/usr/bin/perl -w # # polls what's current playing on radio KOIT in San Francisco, # get the lyrics from lyricsbox.com # # TODO: (1) make the title search a little fuzzier # (2) search more sites for lyrics # (3) nicer interface? color, scrolling? # use strict; use LWP::Simple; use URI::Escape; my $nowplay = "http://koit.com/nowplay_data.cfm"; my $lyricsbox = "http://www.lyricsbox.com"; my $lyrics_list = "$lyricsbox/cgi-exe/am.cgi?a=search&p=1&l=artist&s=" +; my $last_artist = ""; my $last_title = ""; my $sleep = 60; # poll once a minute while(1){ my $data = get($nowplay); my ($artist, $title) = $data =~ m|box=(.+) - (.+)&|; $title =~ s/\W+$//g; # remove junk from end $title =~ s/^\W+//g; # remove junk from beginning if ($artist ne $last_artist && $title ne $last_title){ print "-"x40,"\n"; print "\t$artist: $title\n\n"; $last_artist = $artist; $last_title = $title; # try to get lyrics my $list = get($lyrics_list.uri_escape($artist)); my ($url) = $list =~ m|<A href="([^"]+)">\Q$title\E.*?</A></t +d>|i; if($url){ my $song = get($lyricsbox.$url); my ($ly) = $song =~ m|<PRE.*?>(.+)</PRE>|si; print $ly; }else{ print "No lyrics found from $lyricsbox\n"; } print "\n\n"; } sleep $sleep; }
  • Comment on Lyrics for what's playing on radio KOIT (FM 96.5 in San Francisco)
  • Download Code

Replies are listed 'Best First'.
Re: Lyrics for what's playing on radio KOIT (FM 96.5 in San Francisco)
by fdillon (Novice) on Mar 31, 2005 at 14:43 UTC
    Johnny, I got it to work by just erasing the + as suggested by warnings. This is a neat piece of code. Questions: A.) I have not seen 'while(1)' used in any book. What does the 1 refer to? B.) I can't get KOIT to stream on my Real or my Windows Media player. Is it possible to get such data from other radio stations, or is the data KOIT-specific? Regards, fdillon

      while (1) { will loop as long as 1 is true, which should be a very very long time since 1 is always true. It is a way to create an infinite loop.


      ___________
      Eric Hodges
      I've seen a few radio stations that post their playlists. Some are realtime, some are not. KFOG is another San Francisco station that does post their playlist and I think it's fairly realtime or it was. However BBC6, which I listen to at work, does post their playlists, but some presenters do it about 5-10 minutes after the song plays, while others wait a day to post. I've even seen some Real Audio feeds that actually give you the title and artist.

      Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
      BTW, the "+" you see is added by perlmonks for formatting, you can turn the text wrapping on/off from your user settings page.
      Not sure why you're having problem streaming, I listen to KOIT via the net (Windows Media), all seems to be fine. Yes, the "waht's playing now" is for KOIT only, it's a couple of seconds late, but usually ok.
you should clean your lyrics first...
by cazz (Pilgrim) on Mar 31, 2005 at 21:46 UTC
    You should filter the lyrics for printable characters before blindly printing them to the screen. Many terminals have support for escape sequences that can execute code.

    By printing raw data from a third-party without cleaning it first, you open yourself up to being "0wnz0red" if you are using a vulnerable terminal.
Re: Lyrics for what's playing on radio KOIT (FM 96.5 in San Francisco)
by fdillon (Novice) on Apr 01, 2005 at 19:16 UTC
    Thanks Johnny, Eric, Dave, and cazz, Update: I got my RealPlayer to work; had to go to KOIT's website and register. Now both the script and the radio work! Making for an excellent office karaoke scenario here today. Something is missing... maybe several Anchor Steams, mimosas, etc. Eric: Hmm... infinite loop... Is that like an 8-track tape? L8, fdillon
Re: Lyrics for what's playing on radio KOIT (FM 96.5 in San Francisco)
by ady (Deacon) on Apr 17, 2005 at 18:39 UTC
    Great!
    But i get:
    Use of uninitialized value in print at lyrics.pl line 38 (print $ly;)

    (ie. $url: 404 Song not found)
    -- allan
      Something changed recently in KOIT or lyricbox.com that's breaking the code, need to fix the regex, will do it when I found time.
        Seems the problem is with the lyricsbox.com site (404 err)
        I tried switching to musicsonglyrics.com, and that loks better (tho' my hack is pure screen scraping...)
        Could be nice if you get some time to fix it!

        ____________________________________________________
        Update April 19: added yet another lyrics channel... -- allan
        ____________________________________________________
        Update April 26: updated code (incl TK GUI) in node 450914
        ____________________________________________________
        #!/usr/bin/perl -w # polls what's current playing on radio KOIT in San Francisco, # get the lyrics from lyricsbox.com # TODO: (1) make the title search a little fuzzier # (2) search more sites for lyrics # (3) nicer interface? color, scrolling? # ==================================================================== +========== use strict; use LWP::Simple; use URI::Escape; my $nowplay = "http://koit.com/nowplay_data.cfm"; my $lyricsbox = "http://www.lyricsbox.com"; my $lyrics_list = "$lyricsbox/cgi-exe/am.cgi?a=search&p=1&l=artist&s=" +; my $musicsong = "http://www.musicsonglyrics.com"; my $matrix = "http://www.ntl.matrix.com.br"; my ($last_artist,$last_title) = ("",""); my $sleep = 60; # poll once/min select((select(STDOUT), $| = 1)[0]); # autoflush STDOUT my $data; while(1){ while (!($data = get($nowplay))) { print "*"; sleep $sleep; next; +} my ($artist, $title) = $data =~ m|box=(.+) - (.+)&|; for ($title) { s/\W+$//g; s/^\W+//g; } # rm head&trail junk my ($a1,$t1,$a,$t,$r) = (substr($artist,0,1), substr($title,0,1)) +; if ($artist ne $last_artist && $title ne $last_title){ # NowPlaying on KOIT print "\n","-"x50,"\n"; print "[$artist]: [$title]\n\n"; ($last_artist, $last_title) = ($artist, $title); my ($list, $url, $song); my $ly = ""; # Lyrics from lyricsbox? $list = get($lyrics_list.uri_escape($artist)); ($url) = $list =~ m|<A href="([^"]+)">\Q$title\E.*?</A></td>| +i; if ($url) { $song = get($lyricsbox.$url); } if ($song) { ($ly) = $song =~ m|<PRE.*?>(.+)</PRE>|si; } if ($ly) { print "Lyrics: $lyricsbox\n\n $ly\n\n"; next; } else { print "No lyrics: $lyricsbox\n"; } # Lyrics from matrix? ($a, $t) = ($artist, $title); for ($t) { s/\s/_/g; s/\'//g; } foreach my $p ("html/lyrics/$t1", "oldies_list/top/lyrics") { $url = lc("pfilho/$p/${t}.txt"); $ly = get("$matrix/$url"); last if $ly; } if ($ly) { for ($ly) { s/^\s+//; s/\s+$//; } print "Lyrics: $matrix\n\n $ly\n\n"; next; } else { print "No lyrics: $matrix\n"; } # Lyrics from musicsong? ($a, $t) = ($artist, $title); for $r (\$a, \$t) { $$r =~ s/\s//g; $$r = lc($$r); } $url = "$a1/${a}lyrics/$a${t}lyrics.htm"; $song = get("$musicsong/$url"); if ($song) { ($ly) = $song =~ m|<span.*?>(.+)</span>|si; } if ($ly) { for ($ly) { s/^\s+//; s/\s+$//; s/<.*?>//gi; } print "Lyrics: $musicsong\n\n $ly\n\n"; next; } else { print "No lyrics: $musicsong\n"; } } else { print "."; } sleep $sleep; }

        -- Allan

        ===========================================================
        As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses

        -- Hakuin Ekaku Zenji