#!/bin/sh
export LC_ALL=C
# Use 2 global variables
SAV_TRACK_PATH=/tmp/savetrack.temp
SAV_TRACK=$(cat $SAV_TRACK_PATH 2> /dev/null)
RESULT=
TRACK_INFO=
export RESULT
# No luck with the following services:
# lyricstime: limits the # of requests for IP address
# metrolyrics, mp3lyrics.org: lyrics not visible (seems to use placeholders)
absolutelyrics()
{
set -- `echo $TRACK_INFO | sed 's/\ /_/g' | awk -F';' '{ print $1} {print $2 }'`
ARTIST=$1
TRACK=$(echo $2 | sed 's/(.*)//g' | sed 's/\[.*\]//g')
MYURL=$(echo http://www.absolutelyrics.com/lyrics/view/$ARTIST/$TRACK/ | sed 's/\?//g' | sed s/\'//g | tr '[:upper:]' '[:lower:]' | sed 's/\+//g' | sed 's/\,//g' | sed 's/\ /_/g')
curl -s $MYURL > /tmp/ablyrics_tmp.lyr
grep realText /tmp/ablyrics_tmp.lyr > /tmp/ablyrics_tmp2.lyr
while read RESULT
do
break
done < /tmp/ablyrics_tmp2.lyr
RESULT=$(echo $RESULT | sed 's/<br\ \/>/^/g' | sed 's/<\/p>/^^/g' | sed '/^$/!{s/<[^>]*>//g;}' | tr '\^' '\n')
return
}
azlyrics()
{
set -- `echo $TRACK_INFO | sed 's/\ //g' | awk -F';' '{ print $1} {print $2 }'`
ARTIST=$(echo $1 | sed 's/the//g' | sed 's/The//g')
TRACK=$(echo $2 | sed 's/(.*)//g' | sed 's/\[.*\]//g')
MYURL=$(echo http://www.azlyrics.com/lyrics/$ARTIST/$TRACK.html | sed 's/\?//g' | sed s/\'//g | tr '[:upper:]' '[:lower:]' | sed 's/\+//g' | sed 's/\,//g' | sed 's/\ //g')
RESULT=$(curl -s $MYURL | sed '1,/END OF RINGTONE/d' | sed '/\[ <a href/,9999d' | sed '/<br><br><br><br>/,9999d' | sed 's/<b>//g' | sed 's/<\/b>//g' | sed 's/<i>//g' | sed 's/<\/i>//g' | sed 's/<br>//g' | sed 's/<br \/>//g' | sed 's/<b>//g' | sed 's/<\/b>//g' | sed '3,4d')
return
}
lyrics007()
{
set -- `echo $TRACK_INFO| sed 's/\ /%20/g' | sed 's/\[.*\]//g' | awk -F';' '{ print $1} {print $2 }'`
MYURL=$(echo "http://www.lyrics007.com/$ARTIST%20Lyrics/$2%20Lyrics.html") # | sed 's/\?//g' | sed s/\'//g | sed 's/\+//g' | sed 's/\&//g')
RESULT=$(curl -s $MYURL)
if [[ "$RESULT" == *tf_artist* ]]; then # it's been found
true
else
RESULT=""
return
fi
RESULT=$(echo $RESULT | sed '1,/<br><br><br>/d' | sed '1,1d' | sed '/<br><br><script/,9999d' | sed 's/<br><br><br>//g' | sed 's/<\/script>//g' | sed 's/<BR>//g' | sed s/<br>/\^/g | tr '\^' '\n' | sed 's/<br>//g' | sed 's/<br \/>//g')
if [ "$RESULT" = "" ]; then
ARTIST=$(echo $1 | sed 's/the\%20//g' | sed 's/The\%20//g')
MYURL=$(echo "http://www.lyrics007.com/$ARTIST%20Lyrics/$2%20Lyrics.html" | sed 's/\?//g' | sed s/\'//g | sed 's/\+//g' | sed 's/\&//g')
RESULT=$(curl -s $MYURL | sed '1,/<br><br><br>/d' | sed '1,1d' | sed '/<br><br><script/,9999d' | sed 's/<br><br><br>//g' | sed 's/<\/script>//g' | sed 's/<BR>//g' | sed 's/<br>//g' | sed 's/<br \/>//g' | perl -i~ -pe 's/\x92/ /g')
fi
return
}
update_lyrics()
{
# Update lyrics on iTunes
osascript -e '
tell application "iTunes"
set lyrics of current track to system attribute "RESULT"
end tell'
}
# MAIN LOGIC
# Use an embedded AppleScript to get the iTunes information
if ps x | grep iTunes | grep -v grep | grep -v -q iTunesHelper; then
TRACK_INFO=$(osascript -e 'tell application "iTunes"
if player state is playing then
set trackname to name of current track
set artistname to artist of current track
set mylyrics to lyrics of current track
set output to artistname & ";" & trackname & "^" & mylyrics
end if
end tell' | iconv -f utf-8 -t ucs-2-internal)
# Don't fetch lyrics if already present in iTunes
LYRICS=$(echo $TRACK_INFO | awk -F"^" '{print $2}')
TRACK_INFO=$(echo $TRACK_INFO | awk -F"^" '{print $1}')
if [ "$LYRICS" != "" ]; then
echo $LYRICS
exit 0
fi
if [ "$TRACK_INFO" = "" ]; then # iTunes running but not playing
exit 0
fi
# 1. azlyrics.com
azlyrics
if [ "$RESULT" != "" ] && [ "$RESULT" != "0" ]; then
RESULT=$(echo "$RESULT")
update_lyrics
echo "$RESULT"
exit 0
fi
# 2. lyrics007.com
lyrics007
if [ "$RESULT" != "" ] && [ "$RESULT" != "0" ]; then
RESULT=$(echo "$RESULT")
update_lyrics
echo "$RESULT"
exit 0
fi
# 3. absolutelyrics.com
absolutelyrics
if [ "$RESULT" != "" ] && [ "$RESULT" != "0" ]; then
update_lyrics
echo "$RESULT"
exit 0
fi
# GROWL notification if lyrics are not found
if [ "$TRACK_INFO" != "$SAV_TRACK" ]; then
PARSED_INFO=$(echo $TRACK_INFO | awk -F";" '{print $1; print $2}')
/usr/local/bin/growlnotify --name "Geek Lyrics" -m "$PARSED_INFO" 'Lyrics not found' -I "/Users/thomas/Scripts/Add To iTunes.app"
echo $TRACK_INFO > $SAV_TRACK_PATH
fi
fi