Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Copyright © 2018, Typhon Baal Hammon
- #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- #documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
- #rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
- #and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- #The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement.
- #In no event shall the authors or copyright holders X be liable for any claim, damages or other liability,
- #whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software
- #or the use or other dealings in the Software.
- #Except as contained in this notice, the name of the copyright holder shall not be used in advertising or otherwise
- #to promote the sale, use or other dealings in this Software without prior written authorization from the copyright holders.
- #A script that downloads and register lyrics in the appropriate folder
- #And displays them in the terminal
- #
- #This program requires wget, pcregrep and the html-xml-utils debian package (also available on Archlinux's AUR)
- #This program also requires a web browser and a text editor (Defaults to firefox and pluma)
- #Use :
- # $lyrics.bash artistname songname
- #This prints the lyrics to the song in the terminal ;
- #if they have to be fetched from a distant website, the program will ask for confirmation, then register them for future consultation
- #WARNING : NOTE THAT BOTH THE ARTIST NAME AND THE SONG NAME SHOULD BE WRITTEN IN CAMEL CASE.
- #This script assumes that they are in camel case at various points.
- # $lyrics.bash artistname
- #This prints out the content of the folder with the name artistname, in the lyrics folder.
- # $lyrics
- #This prints out the content of the lyrics folder itself
- #The convention followed by this script is that the folder in the variable $lyricsfolder contains folders named after artists
- #These folders in turn contain files named after the artist and the song which contain the lyrics
- #This program uses the following convention : lyrics to a specific song by a specific artist have the following path :
- # $lyricsfolder/$artistname/$artistname_---_$songname
- #
- #If no such file exists with this specific path, this program will then attempt to download them
- #and write them to a new file with this path, after asking for confirmation
- #If no folder called $artistname exists, the program will ask for confirmation in creating it and exit immediately if this is denied
- #Configuration variables
- #This variable is the path to the lyrics folder where every lyrics file will be, in the appropriate subfolder
- lyricsfolder="$HOME/textes/Paroles_de_chansons"
- #lyricsfolder="$HOME/lyrics"
- #This can either be a new folder or an existing one.
- #Any pre-existing subfolders and files not conforming to the naming convention will be ignored by the script
- #If all else fails, the script will ask you for confirmation to send you on a google search and then google the lyrics
- #and open a text editor so that you can copy them yourself.
- #Here you can configure which browser and text editor to use
- browser=firefox
- texteditor=pluma
- #global variables : HTTP header stuff and regexes that are used throughout the script
- userag="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"
- regex2='s=<[^>]+>==g'
- regex1='s=<br[^>]*>=\n=g'
- regex3='s=\r==g'
- mkdir -p "$lyricsfolder"
- artiste=$1
- titre=$2
- texte=""
- if [ -z "$artiste" ]
- then
- #echo "Erreur : nom de l'artiste et de la chanson requis"
- #exit 1
- ls "$lyricsfolder"
- exit 0
- fi
- repertoire="$lyricsfolder/$artiste"
- if [ ! -e $repertoire ]
- then
- artiste=${artiste#The}
- artiste=${artiste#Les}
- repertoire="$lyricsfolder/$artiste"
- fi
- if [ -e "$repertoire" ]
- then
- if [ -z "$titre" ]
- then
- echo $repertoire
- ls -B --color $repertoire
- fi
- else
- echo -e "$repertoire n'existe pas.\n Le créer ? (y/n)"
- read repcreer
- if [ "n" != "${repcreer}" ]
- then
- mkdir -v "$repertoire"
- else
- exit 0
- fi
- fi
- #firstletter=${artiste:0:1}
- if [ ! -z "$titre" ]
- then
- #repertoire="/$lyricsfolder/$artiste"
- paroles="$repertoire/${artiste}_---_$titre"
- if [ -e "$repertoire/${artiste#The}_---_$titre" ]
- then
- paroles="$repertoire/${artiste#The}_---_$titre"
- elif [ -e "$repertoire/${artiste#Les}_---_$titre" ]
- then
- paroles="$repertoire/${artiste#Les}_---_$titre"
- fi
- if [ ! -e "$paroles" ]
- then
- for website in {www.azlyrics.com,www.plyrics.com} #,www.darklyrics.com,www.urbanlyrics.com
- do
- if [ -z "$texte" ]
- then
- #echo "$website"
- url1="http://$website/lyrics/${artiste,,}/${titre,,}.html"
- artiste2=${artiste,,}
- artiste2=${artiste2#the}
- url2="http://$website/lyrics/${artiste2,,}/${titre,,}.html"
- #regex="(<div>[\r\n]+<\!--([^-]|-p)+-->)[\r\n]+(.*<br>\r\n)+.*"
- regex="(<!-- start of lyrics -->|<div>)(.*[\r\n]*)+?(</div>|<!-- end of lyrics -->)"
- #regex2='s=<(br|/|i|div|!--.*?--|\s)+>==g'
- if $(wget -qU "$userag" --spider "$url1")
- then
- texte=$(wget -U "$userag" -qO- $url1 | pcregrep -oMi "$regex" )
- elif $(wget -qU "$userag" --spider "$url2")
- then
- texte=$(wget -U "$userag" -qO- $url2 | pcregrep -oMi "$regex")
- fi
- fi
- done
- if [ -z "$texte" ]
- then
- #uppermatch='s/\(\S\)\([[:upper:]]\)/\1_\2/g'
- uppermatch='s/(?<=\S)(?=[[:upper:]])/_/g'
- artiste3=$(echo $artiste | perl -pe $uppermatch )
- titre3=$(echo $titre | perl -pe $uppermatch )
- echo "lyrics.wikia"
- url1="http://lyrics.wikia.com/wiki/$artiste3:$titre3"
- regexwikia="<div\W+class='lyricbox'>(.*)</div>"
- if $(wget -qU "$userag" --spider "$url1")
- then
- texte=$(wget -qU "$userag" -O- $url1 | pcregrep -oMi "$regexwikia")
- fi
- fi
- if [ -z "$texte" ]
- then
- uppermatch='s/(?<=\S)(?=[[:upper:]])/-/g'
- artiste4=$(echo $artiste | perl -pe $uppermatch )
- titre4=$(echo $titre | perl -pe $uppermatch )
- #echo "metrolyrics"
- url5="http://www.metrolyrics.com/${titre4,,}-lyrics-${artiste4,,}.html"
- artiste5=${artiste4#The}
- artiste5=${artiste4#Les}
- artiste5=${artiste4#Die}
- #echo $artiste4,$titre4
- url6="http://www.metrolyrics.com/${titre4,,}-lyrics-${artiste5,,}.html"
- if $( wget -qU "$userag" --spider "$url5" )
- then
- texte=$(wget -U "$userag" -O- "$url5" | pcregrep -oMi "<p class='verse'>(.|\r|\n)+?</p>" )
- elif $( wget -qU "$userag" --spider "$url5" )
- then
- texte=$(wget -U "$userag" -O- "$url6" | pcregrep -oMi "<p class='verse'>(.|\r|\n)+?</p>" )
- else
- for url7 in ""{,}
- do
- echo
- done
- fi
- fi
- if [ ! -z "$texte" ]
- then
- echo "$texte" | sed -re $regex1 | sed -re $regex3 | \
- sed -re $regex2 | sed -e "/^$/d" | hxunent | grep -Eiv "Visit .+ for these lyrics." > "$paroles"
- fi
- if [ ! -e "$paroles" ]
- then
- echo "Artist : \"$artiste\""
- echo "Song : \"$titre\""
- echo "Do you want to search for the lyrics with google ? (Y/n)"
- read ouinon
- if [ "n" != "${ouinon,,}" ]
- then
- $browser "http://www.google.fr/search?q=$artiste+$titre+lyrics"
- $texteditor $paroles
- echo "Lyrics saved !"
- fi
- fi
- if [ -e "$paroles" ]
- then
- #echo "$paroles"
- cat "$paroles"
- echo
- echo "Lyrics correct ? (Y/n)"
- read ouinon
- if [ "n" == "${ouinon,,}" ]
- then
- rm -v "$paroles"
- fi
- fi
- elif [ -e $paroles ]
- then
- #clear
- echo $paroles
- cat $paroles
- echo
- fi
- fi
- exit 0
Add Comment
Please, Sign In to add comment