How to write this small script


Forum: Programming and Scripting
Topic: How to write this small script
started by: piccolo

Posted by piccolo on June 19 2006,20:56
I listen music from a radio station on internet. The title from current song can be seen on radio's website. The page playlist is updated each time that another song begin.
I want write a program that read the song title from website and automatically open the respective text file saved on my computer that has the lyric.
Considering I'm not expert programming the question is what language you recommend to use for this application?

Posted by dare2dreamer on June 19 2006,22:49
http://tldp.org/LDP/abs/html/index.html

Bash shell scripting, to the rescue.

Assuming your lyric files and the website are in consistant formats, I bet you can simply wget the files you need and then have the script fire up a text editor with your lyrics file.

Posted by tedmoore99 on June 20 2006,00:25
I agree with Dare2Dreamer.  It looks like a shell script would do nicely.  You have helped me so much, maybe I can return the favor.  What do you need?
--Ted

Posted by piccolo on June 21 2006,10:53
Sometimes when I read some answers on this forum I think it is wonderful to find people so considerate. This is one of that times. Thanks sincerely.

I want to let you know that I'm learning english in self taught manner then I read the lyrics while listen the song to know so the english pronunciation. I think the melodic songs are ideal for this purpose because they have a specific timing between words more easy to listen and to remember while the music is pleasing to the listener.

The idea is, to extract only the title string from currently playing song on this "web playlist":
< http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm >

With that string, to open the file such as:
"string" = titleX = filename
From this set of lyric files:
title1.txt, title2.txt,...,titleN.txt

I'm considering to use JavaScript or Bash as Dare2Dreamer said.
Any idea about how to get the string title from BSI_WKTZFM_Page.htm file is of course welcomed.

Regards.

Posted by tedmoore99 on June 21 2006,13:53
Piccolo:
First of all, you seem to have an adequate enough understanding of English to offer help on the Forum.  I am proud of you.  Secondly, the radio station is not broadcasting right now, so I can't really look at it now.  

Get back to you later.
--Ted

Posted by clacker on June 21 2006,15:29
piccolo, I'd try using the wget command to get the contents of the web page and then use grep and possibly awk to trim it down into the parts you want.  I would use pipes to feed the output of one command into the input for another and string them together to do what you want.

wget can send a web page's source to the standard output if you use the -O - switch like so:

wget < http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm > -O -

Now you can look at the text and see what the lines you want have on them.  I noticed they contain a time, so we can use grep to print only lines that have a time on them by "piping" the output from the wget command into the grep command using a | symbol:

wget < http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm > -O -  | grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"

You can further narrow down the lines by using sed, a stream editor, to print only the parts you want (the \ at the end of the first two lines allows you to continue a line if the command gets too long):

wget < http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm > -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/'

This prints out two lines, and the first one is the one you want, so use head to print only the first line (using the -n 1 switch):

wget < http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm > -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1

Now you can see that the title of the song is after the comma, so only print out what is after the comma and that following space:

wget < http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm > -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1| \
    sed 's/.*, \(.*\)/\1/'

And there is the title of the current song.  A bash script could take the above code and set a variable to its value:

Code Sample
#!/bin/bash

SONGTITLE=`wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1| \
    sed 's/.*, \(.*\)/\1/'`

echo "The current song is $SONGTITLE"

Posted by tedmoore99 on June 22 2006,01:07
What an excellent job.  I did not even begin on it and here is the solution already.  Only one problem as I see it.  When I ran the script I did not get the current song but the one before, so I changed the "head -n 1" to "tail -n 1" and the current song printed out.  For Piccolo: the "tail" command works just like the "head" command except it counts from the end of the file.  So "tail -n 1" tells it to print the last line.
Enjoy
--Ted

Posted by tedmoore99 on June 22 2006,13:46
Piccolo:
Question for you:  Where do you get the files of song lyrics?
--Ted

Posted by piccolo on June 22 2006,18:41
WOW! I'm very excited. Thanks very much. You all have helped me, from Clacker who did translate that wrote Dare2Dreamer into Bash code to finally the small but opportune correction of Tedmoore99 whose words encourage me to follow improving.
However I must say that who have worked really hard am I. Because I thougth the script name: The script is called "clacker" in honour to "The GodBash". :D

Let know that due I live inside a community that not speaks english, to offer help on DSL forum is a remote method to learn english that I'm using. But I do not understand nothing from that I listen. Evidently to read, write, talk the mechanisms are different then to exercise my sense of hearing I need read the exact text from that I listen.
Heretofore I was doing this manually losing time and therefore reading the lyrics later, sometimes too late the song had begun.
This script is really very useful for my purpose!.

I below have posted the script that can be useful for others that want learn english like me.
The interested can contact me if need lyrics files or any other kind of help about the script.

The radio can be listened by download the play.pls file from:
www.live365.com/cgi-bin/play.pls?stationid=309492
Then play with command:
mplayer -playlist play.pls

I'm saving the lyrics files inside the same directory were is the script with exactly the "$ SONGTITLE" filename given by the script. Generally I found the lyrics in this website: < http://www.lyricsplayground.com >  

Code Sample
#!/bin/bash
# Real time lyrics viewer for Jones College Radio (http://wktz.jones.edu)

i=1
while [ i=1 ] # infinite loop

do
 SONGTITLE=`wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
 grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
 sed 's/.*>\(.*\)<br><\/font>/\1/' | \
 tail -n 1 | \
 sed 's/.*, \(.*\)/\1/'`

 echo "Currently playing: $SONGTITLE"
 
 if [ "$SONGTITLE1" != "$SONGTITLE" ]   # Check if the playlist was updated
   then                              #+ prevent open one lyrics file more than one time.  
     echo "New song playing"
     if [ -e "$SONGTITLE" ]                                      # Check if lyrics file exists.
       then
         # Write here command to open the $SONGTITLE file using text editor.
        kate "$SONGTITLE"                         # Kate text editor in this example
       else
      # write here command if want to generate automatically empty lyrics files
        echo "$file Lyrics file does not exist yet."
     fi
 fi

SONGTITLE1="$SONGTITLE"   # Actual song title stored for next iteration
sleep 10                              # Delay 10 seg before retrieve playlist again.

done

#Thanks Dare2Dreamer, Clacker and Tedmoore99 users from DSL Forum

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.