From c0e09c4dbffd5001cf79cb1be0559ef1c97a6aef Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sat, 3 Sep 2016 18:28:43 -0400 Subject: [PATCH] Lyrics for the bot. --- modules/sing/sing.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 modules/sing/sing.sh diff --git a/modules/sing/sing.sh b/modules/sing/sing.sh new file mode 100755 index 0000000..3a44139 --- /dev/null +++ b/modules/sing/sing.sh @@ -0,0 +1,42 @@ +[ -f functions.sh ] && source functions.sh + +userNick="$1" +shift +chan="$1" +shift +#get the lyric text into a variable +trackName="$@" +curl="$(command -v curl)" +trackName="$(echo "$trackName" | sed -e "s/ /_/g" -e 's/([[:print:]]*)//g' -e "s/['\/\.]//g" -e 's/&/and/g')" +artist="$(echo "${trackName,,}" | cut -d "-" -f 1 | sed -e 's/_$//' -e 's/^the_\(.*\)/\1_the/')" +song="$(echo "${trackName,,}" | cut -d "-" -f 2 | sed 's/^_//')" +lyricsUrl="http://www.lyricsmania.com/${song}_lyrics_${artist}.html" +lyricText="$($curl -s "$lyricsUrl" | grep -A 100 'Lyrics to ' | grep -B 100 ' ' | tail -n +2 | head -n -2 | sed -e 's/<[^>]*>//g' -e 's/
/\n/g' -e 's/<\/div>.*//')" +#get the number of lines in the text +lyricMaxLines=$(echo "$lyricText" | wc -l) +#keep track of 10 line chunks of text for your starting point. +x=10 +lyricStartLine=$x +while [[ $x -le $(($lyricMaxLines - 10)) ]] ; do +x=$((x + 10)) +lyricStartLine="$lyricStartLine $x" +done +#pass the starting options through shuf so we can get our starting line +lyricStartLine=$(shuf -n 1 -e $lyricStartLine) +#we only need to do the head, tail manipulation if there's more than one line. +if [ $lyricMaxLines -gt 10 ] ; then + lyricText="$(echo "$lyricText" | tail -n $lyricStartLine | head -n 10)" +fi +#Make sure all white space is not an enter or vertical space of any kind. +lyricText="$(echo "$lyricText" | tr "[:space:]" " " | tr -s "[:space:]")" +#sometimes lyrics are just one long line with everything. If this happens, cut it down so it's not too long for the 512 character channel limit. +if [ ${#lyricText} -gt 412 ] ; then + lyricText="${lyricText:0:409}... " + lyricText="$(echo "$lyricText" | rev | cut -d " " -f 1- | rev)" +fi +#Display the lyric text +if [ ${#lyricText} -gt 15 ] ; then +msg "$chan" "${lyricText}" +else +msg "$chan" "Sorry $userNick: I couldn't find any lyrics for $@" +fi