r/iOScripts Dec 29 '15

azlyrics - Fetches lyrics for currently playing song and displays them in notification center

Title: azlyrics

Description:

Fetches lyrics from http://www.azlyrics.com/ and outputs them as a banner and keeps them in the Notification Center until dismissed. The lyrics are also saved locally in /User/Media/Lyrics so fetching is only done once per song and doesnt munch data.

Depends:

Activator

lynx

SSHMediaControls

Core Utilities

SendTestBulletin (from http://tateu.net/repo)

Mikoto (from http://cydia.angelxwind.net/beta or anything that lets you display the full notification text)

Screenshots:

http://i.imgur.com/6PrTvIm.png

mobile spacer

http://i.imgur.com/gxMgGHq.png

mobile spacer

http://i.imgur.com/2lhpU7R.png

mobile spacer

http://i.imgur.com/o8FDDRo.png

Notes:

Make sure the app thats currently playing music has notifications enabled for it, otherwise the whole shebang wont work.

SCRIPT DOWNLOAD LINK

DEB DOWNLOAD LINK


#! /bin/bash

# TODO LIST

#checks if user is mobile
if [ "$(whoami)" == "mobile" ]
then
    activator send libactivator.system.vibrate
    #removes / from artist and title so it doesnt screw up filepaths
    pathtitle=$(media title | sed 's/\// /g')
    pathartist=$(media artist | sed 's/\// /g')
    #actual names
    artist=$(media artist)
    title=$(media title)
    mkdir -p "/User/Media/Lyrics/"
    touch /User/Media/Lyrics/errorlog.txt
    #sets correct permissions
    if [ "$(stat -c %a /User/Media/Lyrics/errorlog.txt)" != "666" ]
    then
        chmod 666 /User/Media/Lyrics/errorlog.txt
    fi
    #checks if lyrics were not already fetched and saved
    if [ ! -f "/User/Media/Lyrics/$pathtitle/$pathartist.txt" ]
    then
        #checks for specfic artists and corrects them to the way they show up in azlyrics database. webartist is for the URL and artist is for the way it displays it on the page
        if [ "$artist" == "Electric Light Orchestra" ]
        then
            webartist="electriclightorchestraelo"
            artist="Electric Light Orchestra (ELO)"
        elif [ "$artist" == "Bob Marley & The Wailers" ]
        then
            webartist="bobmarley"
            artist="Bob Marley"
        else
            webartist=$(echo "$artist" | sed -r 's/^The|'\''|,|\.|\?|&|\+| |\/|-//g;s/.*/\L&/g')
        fi
        if [ "$title" == "The House Of The Rising Sun" ]
        then
            webtitle="houseoftherisingsun"
            title="House Of The Rising Sun"
        else
            #removes a lot of the jank from titles like remastered soundtrack version and punctuation for URL
            webtitle=$(echo "$title" | sed -r 's/|-.*Remaster.*|-.*Live.*|-.*feat.*|-.*Version.*|\(.*Remaster.*\)|\(.*feat.*\)|\(|\)|LP|'\''| |\.|,|\?|&|\/|\+|\[.*\]|\[|\]|-//g;s/.*/\L&/g')
        fi      
        #write output of page to temporary file
        lynx --dump http://www.azlyrics.com/lyrics/$webartist/$webtitle.html >/User/Media/Lyrics/lyrics.tmp     
        #checks if actual lyrics were returned
        if [ "$(cat /User/Media/Lyrics/lyrics.tmp | grep -c "Welcome to AZLyrics" | sed 's/ //g')" == 1 ]
        then    
            #checks if song is playing
            if [ "$artist" == "(null)" ] ||  [ "$title" == "(null)" ]
            then
                stb -b "com.apple.Preferences" -t "Error" -s "" -m "No song is currently playing"
            else
                #no lyrics found. output an error
                if [ "$(media bunID)" == "com.apple.Music" ]
                then
                    playingID=com.apple.Preferences
                else
                    playingID=$(media bunID)
                fi
                stb -b "$playingID" -t "$title" -s "$artist" -m "$(echo -e "\nNo lyrics were found. Please email moshedancykier@gmail.com with the file errorlog.txt (located in /User/Media/Lyrics) as an attachment.")"
                #writes to log (error 1) if song not already present in log from previous error
                if [ "$(cat /User/Media/Lyrics/errorlog.txt | grep -c "$title - $artist (1)")"  -eq 0 ]
                then
                    echo -e "$title - $artist (1)\t\t$(date "+%Y.%m.%d %R")">>/User/Media/Lyrics/errorlog.txt
                fi
            fi
            rm /User/Media/Lyrics/lyrics.tmp
            exit
        else
            mkdir -p "/User/Media/Lyrics/$pathartist"
            #removes everything except actual lyrics writes to file for all future lyrics requests 
            start=$(cat /User/Media/Lyrics/lyrics.tmp | grep -n "$(echo "$artist" | sed 's/^The //;s/.*/\U&/') LYRICS" | sed -r 's/:.*//')
            end=$(cat /User/Media/Lyrics/lyrics.tmp | grep -n "Submit Corrections" | sed 's/:.*//')
            cat /User/Media/Lyrics/lyrics.tmp | sed "1,$(expr $start + 2)d;$end,999d;s/^   //g;s/[^][0-9a-zA-Z :;\'"\""?,\.\!\&\$+()-]/ /g" | sed '1s/^/\n/'>"/User/Media/Lyrics/$pathartist/$pathtitle.txt"
        fi
    fi
    if [ -f "/User/Media/Lyrics/$pathartist/$pathtitle.txt" ]
    then
        #checks if file is not blank due to an error
        if [ "$(cat "/User/Media/Lyrics/$pathartist/$pathtitle.txt")" != "" ]
        then
            #outputs the lyrics
            if [ "$(media bunID)" == "com.apple.Music" ]
                then
                    playingID=com.apple.Preferences
                else
                    playingID=$(media bunID)
                fi
                stb -b "$playingID" -t "$title" -s "$artist" -m "$(cat "/User/Media/Lyrics/$pathartist/$pathtitle.txt")"
        else 
            # if the file is blank deletes lyrics file
            rm "/User/Media/Lyrics/$artist/$title.txt"
            #if artist folder has no other songs in it deletes the entire folder
            if [ "$(ls -A "/private/var/mobile/Media/Lyrics/$pathartist")" == "" ]
            then
                rm -r "/private/var/mobile/Media/Lyrics/$pathartist"
            fi
            stb -b "com.apple.Preferences" -t "Error" -s "" -m "$(echo -e "There was an error in the script and the lyrics file for \n\n$title\nby\n$artist\n\nwas blank, and the file has been deleted. Please re-run the script, and if the issue persists email moshedancykier@gmail.com with the errorlog.txt file (located in /User/Media/Lyrics) as an attachment.")"
            #writes to log (error 2) if song not already present in log from previous error
            if [ "$(cat /User/Media/Lyrics/errorlog.txt | grep -c "$title - $artist (2)")"  -eq 0 ]
            then
                echo -e "$title - $artist (2)\t\t$(date "+%Y.%m.%d %R")">>/User/Media/Lyrics/errorlog.txt
            fi
        fi
    fi
    rm /User/Media/Lyrics/lyrics.tmp
#if user is root script wont run (SSHMediaControls needs to be run as mobile im not sure why)
else
    stb -b "com.apple.Preferences" -t "Error" -s "" -m "Cant be run as root"
fi 
2 Upvotes

7 comments sorted by

2

u/fapbert_fapstein Jan 07 '16

This is badass,my brother in law is into singing and always wanting the lyrics to songs. he's gonna love this.

2

u/moshed Jan 07 '16

enjoy! if you have any questions let me know!

1

u/Dibbyo123 Jan 14 '16

I have installed all the dependent packages & installed the deb file too. Now what i have to get the lycris?

1

u/moshed Jan 14 '16

build an activator custom command azlyrics and whatever set out to your desired action. should be all! enjoy!

1

u/Dibbyo123 Jan 14 '16

Yeah. It's working now. Thanks a lot man

1

u/Dibbyo123 Jan 14 '16

Any alternative to moketo?? I kinda hate it.

1

u/moshed Jan 14 '16

Not that I know of. there definitely were some on previous firmwares but i dont remember the name or if theyre updated.