Radio Playlist Work
So, I was going to try to get the VU Meter Tie project going again, only I decided to put the LEDs on the buttons of my shirt instead of in a tie. Unfortunately I destroyed four surface mount LEDs while trying to solder wires or leads to them. I’m either going to need slightly bigger LEDs or some tiny wire. Out of frustration I’ve decided to come work on the Radio Playlist project instead.
Where I last left off, I now have a script that can retrieve the Artist and Song title of a song played on the Edge at a specified time. I would run the script like this:
./script2.bash 6:28\ PM
Running this exact command right now returns the result:
FINGER ELEVEN
Paralyzer
So, all is well in the script so far. Now everything gets trickier. For one, the website doesn’t update the songs in real time. When a song is played on the radio, it won’t show up on the list for around 10-30 minutes. 30 minutes is the maximum delay that I have noticed so far. This means that when the script receives a notice to check for the song, it will have to wait about 30 minutes before actually checking. My first reaction is to schedule a cron job to call the script 30 minutes after it receives the message from me. This means that I will actually need two scripts. One to check for the e-mails and set the cron job, and another to actually look up the song and save it in a list.
The next trick lies in the fact that my computer time is not synchronized with the radio station’s time. Therefore, my computer will see that I want a song that was being played at say, 3:14. The radio station’s playlist might say it was played at 3:12 or 3:16. My script has to have some way of finding the right time, without going too far and getting the wrong song. I think if the script first looks up the exact time on my computer and finds nothing, then it can increment by one minute and check again. It will go up a maximum of two minutes. If it finds nothing, than it will start subtracting one minute from my computer’s time until it finds a song. The first song it encounters will be considered the correct song. This is the first thing I will work on today.
So I have added a bit to the script and I’m beginning to think a different programming language would have been a better choice. Perl maybe? I’m not sure. I’m just going to keep on moving with bash until I feel i have to change environments. Here is the newer, revised version of the script:
———————CODE————————–
#Next 3 lines are temp for testing the script
#They take the input values and store them in variables (h=hour, m=minute, a=am/pm)
h=$1
m=$2
a=$3
echo “time: $h:$m”
#—–Get the page and save it to a file
curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=075069068074045070077 > savedPage.html
#—–Get the Artist——-
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
if [[ $artist = “” ]] #–If we didn’t find an artist
then #–Then increment the minute
m=`expr $m + 1`
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
fi
while [[ $artist = “” ]] #–If we didn’t find an artist
do
m=`expr $m - 1`
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
done
#—–Get the Song———
song=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/D><td nowrap><span class=blackMain11px>/\n/’ | tail -n1 | sed ’s/<\/td>/\n/’ | head -n1`
#——Print the artist and song——
echo $artist
echo $song
——————–END CODE———————-
The first change I did was to take the input time and save the values into variables. I then use those variables to search for the time within the html code instead of the raw input values. This allows me to change the values easily if I need too.
The next addition is the “if” condition. This says that if the $artist variable is empty (we didn’t find anything for that time) then add one minute to the time and check THAT time.
The next change is the “while” loop. This loop checks to see if the $artist variable is blank again. If it is still blank, then it decrements the time by one minute and checks THAT time. It will continue to decrement and check the time until it finds a song. This works, but I think it MIGHT end up working better if I check one minute up, then one minute down, then two minutes up, then two minutes down, etc. This would allow me to find the closest song to the current time.
The last change I made was to have it save the artist and the song title into two variables. This was to make it easier to check them. At the end of the loop I just echo the variable values.
Now that this part of the script is working, it’s time to write a new script to check for an e-mail message. I’m thinking I’ll probably use pine, since it seems to be a popular console-based e-mail client.
So after trying to get pine installed for a while I had no luck. I kept needed dependencies and not knowing where to get them or how to install them. I ended up finding this little guide on installing alpine by using yum and rpmforge. I followed it and it seems to have worked. Now I just need to figure out how to set it up to check my GMail account.
I found this tutorial on how to set up alpine to read messages from Gmail. Not very difficult at all. Now that I can view my e-mail with alpine, I still have no idea how to use this in a script. I’m beginning to think that I can’t. It looks like there must be a user issuing commands to Alpine in such a way that doesn’t allow for scripting.
I found this perl script for checking an email account for unwanted messages and then deleting them. The script checks the subject line. If all it contains is the word “Document” then it attempts to delete the message. This is similar to what I want to do, only instead of just deleting the message, I want to trigger an event and then delete it.
OK it’s late and I suppose I’m done for the night. I have tomorrow off, except for the last final exam I’ll ever have to take… ever. So hopefully I can get this working tomorrow.