TyphonBaalHammon

lyrics.bash

Sep 17th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.01 KB | None | 0 0
  1. #!/bin/bash
  2. #Copyright © 2018, Typhon Baal Hammon
  3. #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  4. #documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
  5. #rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  6. #and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. #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.
  9. #In no event shall the authors or copyright holders X be liable for any claim, damages or other liability,
  10. #whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software
  11. #or the use or other dealings in the Software.
  12. #Except as contained in this notice, the name of the copyright holder shall not be used in advertising or otherwise
  13. #to promote the sale, use or other dealings in this Software without prior written authorization from the copyright holders.
  14.  
  15. #A script that downloads and register lyrics in the appropriate folder
  16. #And displays them in the terminal
  17. #
  18. #This program requires wget, pcregrep and the html-xml-utils debian package (also available on Archlinux's AUR)
  19. #This program also requires a web browser and a text editor (Defaults to firefox and pluma)
  20.  
  21. #Use :
  22. # $lyrics.bash artistname songname
  23. #This prints the lyrics to the song in the terminal ;
  24. #if they have to be fetched from a distant website, the program will ask for confirmation, then register them for future consultation
  25. #WARNING : NOTE THAT BOTH THE ARTIST NAME AND THE SONG NAME SHOULD BE WRITTEN IN CAMEL CASE.
  26. #This script assumes that they are in camel case at various points.
  27.  
  28. # $lyrics.bash artistname
  29. #This prints out the content of the folder with the name artistname, in the lyrics folder.
  30.  
  31. # $lyrics
  32. #This prints out the content of the lyrics folder itself
  33.  
  34. #The convention followed by this script is that the folder in the variable $lyricsfolder contains folders named after artists
  35. #These folders in turn contain files named after the artist and the song which contain the lyrics
  36. #This program uses the following convention : lyrics to a specific song by a specific artist have the following path :
  37. # $lyricsfolder/$artistname/$artistname_---_$songname
  38. #
  39. #If no such file exists with this specific path, this program will then attempt to download them
  40. #and write them to a new file with this path, after asking for confirmation
  41.  
  42. #If no folder called $artistname exists, the program will ask for confirmation in creating it and exit immediately if this is denied
  43.  
  44. #Configuration variables
  45. #This variable is the path to the lyrics folder where every lyrics file will be, in the appropriate subfolder
  46. lyricsfolder="$HOME/textes/Paroles_de_chansons"
  47. #lyricsfolder="$HOME/lyrics"
  48.  
  49. #This can either be a new folder or an existing one.
  50. #Any pre-existing subfolders and files not conforming to the naming convention will be ignored by the script
  51.  
  52. #If all else fails, the script will ask you for confirmation to send you on a google search and then google the lyrics
  53. #and open a text editor so that you can copy them yourself.
  54.  
  55. #Here you can configure which browser and text editor to use
  56. browser=firefox
  57. texteditor=pluma
  58.  
  59.  
  60.  
  61. #global variables : HTTP header stuff and regexes that are used throughout the script
  62. userag="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"
  63. regex2='s=<[^>]+>==g'
  64. regex1='s=<br[^>]*>=\n=g'
  65. regex3='s=\r==g'
  66.  
  67.  
  68. mkdir -p "$lyricsfolder"
  69.  
  70. artiste=$1
  71. titre=$2
  72.  
  73. texte=""
  74.  
  75. if [ -z "$artiste" ]
  76. then
  77.     #echo "Erreur : nom de l'artiste et de la chanson requis"
  78.     #exit 1
  79.     ls "$lyricsfolder"
  80.     exit 0
  81. fi
  82.  
  83. repertoire="$lyricsfolder/$artiste"
  84. if [ ! -e $repertoire ]
  85. then
  86.     artiste=${artiste#The}
  87.     artiste=${artiste#Les}
  88.     repertoire="$lyricsfolder/$artiste"
  89. fi
  90.  
  91. if [ -e "$repertoire" ]
  92. then
  93.     if [ -z "$titre"  ]
  94.     then
  95.         echo $repertoire
  96.         ls -B --color $repertoire
  97.     fi
  98. else
  99.     echo -e "$repertoire n'existe pas.\n Le créer ? (y/n)"
  100.     read repcreer
  101.    
  102.     if [ "n" != "${repcreer}" ]
  103.     then
  104.         mkdir -v "$repertoire"
  105.     else
  106.         exit 0
  107.     fi
  108.  
  109. fi
  110.  
  111. #firstletter=${artiste:0:1}
  112.  
  113.  
  114. if [ ! -z "$titre" ]
  115. then
  116.     #repertoire="/$lyricsfolder/$artiste"
  117.     paroles="$repertoire/${artiste}_---_$titre"
  118.    
  119.     if [ -e "$repertoire/${artiste#The}_---_$titre" ]
  120.     then
  121.         paroles="$repertoire/${artiste#The}_---_$titre"
  122.        
  123.     elif [ -e "$repertoire/${artiste#Les}_---_$titre" ]
  124.     then
  125.         paroles="$repertoire/${artiste#Les}_---_$titre"
  126.     fi
  127.    
  128.    
  129.     if [ ! -e "$paroles" ]
  130.     then
  131.        
  132.         for website in {www.azlyrics.com,www.plyrics.com} #,www.darklyrics.com,www.urbanlyrics.com
  133.         do
  134.             if [ -z "$texte" ]
  135.                 then
  136.                    
  137.                     #echo "$website"
  138.                    
  139.                     url1="http://$website/lyrics/${artiste,,}/${titre,,}.html"
  140.        
  141.                     artiste2=${artiste,,}
  142.                     artiste2=${artiste2#the}
  143.                     url2="http://$website/lyrics/${artiste2,,}/${titre,,}.html"
  144.        
  145.                     #regex="(<div>[\r\n]+<\!--([^-]|-p)+-->)[\r\n]+(.*<br>\r\n)+.*"
  146.                     regex="(<!-- start of lyrics -->|<div>)(.*[\r\n]*)+?(</div>|<!-- end of lyrics -->)"
  147.                     #regex2='s=<(br|/|i|div|!--.*?--|\s)+>==g'
  148.                    
  149.        
  150.                     if $(wget -qU "$userag"  --spider "$url1")
  151.                     then
  152.                         texte=$(wget -U "$userag" -qO- $url1 | pcregrep -oMi "$regex" )
  153.                     elif $(wget -qU "$userag"  --spider "$url2")
  154.                     then
  155.                         texte=$(wget -U "$userag" -qO- $url2 | pcregrep -oMi "$regex")
  156.                     fi
  157.                 fi
  158.         done
  159.        
  160.         if [ -z "$texte" ]
  161.         then
  162.             #uppermatch='s/\(\S\)\([[:upper:]]\)/\1_\2/g'
  163.             uppermatch='s/(?<=\S)(?=[[:upper:]])/_/g'
  164.             artiste3=$(echo $artiste | perl -pe $uppermatch )
  165.             titre3=$(echo $titre |  perl -pe $uppermatch  )
  166.            
  167.             echo "lyrics.wikia"
  168.            
  169.             url1="http://lyrics.wikia.com/wiki/$artiste3:$titre3"
  170.            
  171.             regexwikia="<div\W+class='lyricbox'>(.*)</div>"
  172.            
  173.             if $(wget -qU "$userag"  --spider "$url1")
  174.             then
  175.                 texte=$(wget -qU "$userag" -O- $url1 | pcregrep -oMi "$regexwikia")
  176.             fi 
  177.         fi
  178.        
  179.         if [ -z "$texte" ]
  180.         then
  181.             uppermatch='s/(?<=\S)(?=[[:upper:]])/-/g'
  182.             artiste4=$(echo $artiste | perl -pe $uppermatch )
  183.             titre4=$(echo $titre | perl -pe $uppermatch  )
  184.            
  185.             #echo "metrolyrics"
  186.            
  187.            
  188.             url5="http://www.metrolyrics.com/${titre4,,}-lyrics-${artiste4,,}.html"
  189.             artiste5=${artiste4#The}
  190.             artiste5=${artiste4#Les}
  191.             artiste5=${artiste4#Die}
  192.            
  193.             #echo $artiste4,$titre4
  194.            
  195.             url6="http://www.metrolyrics.com/${titre4,,}-lyrics-${artiste5,,}.html"
  196.            
  197.             if $( wget -qU "$userag" --spider "$url5" )
  198.             then
  199.                 texte=$(wget -U "$userag" -O- "$url5" | pcregrep -oMi "<p class='verse'>(.|\r|\n)+?</p>" )
  200.             elif $( wget -qU "$userag" --spider "$url5" )
  201.             then
  202.                 texte=$(wget -U "$userag" -O- "$url6" | pcregrep -oMi "<p class='verse'>(.|\r|\n)+?</p>" )
  203.             else
  204.                 for url7 in ""{,}
  205.                 do
  206.                     echo
  207.                 done
  208.             fi
  209.            
  210.            
  211.         fi
  212.        
  213.         if [ ! -z "$texte" ]
  214.         then
  215.             echo "$texte" |  sed -re $regex1 | sed -re $regex3 | \
  216.             sed -re $regex2 | sed -e "/^$/d" | hxunent | grep -Eiv "Visit .+ for these lyrics." > "$paroles"
  217.                    
  218.         fi
  219.        
  220.         if [ ! -e "$paroles" ]
  221.         then
  222.             echo "Artist : \"$artiste\""
  223.             echo "Song : \"$titre\""
  224.             echo "Do you want to search for the lyrics with google ? (Y/n)"
  225.             read ouinon
  226.  
  227.             if [ "n" != "${ouinon,,}"  ]
  228.             then
  229.                
  230.                 $browser "http://www.google.fr/search?q=$artiste+$titre+lyrics"
  231.                
  232.                 $texteditor $paroles
  233.  
  234.                 echo "Lyrics saved !"
  235.                
  236.             fi
  237.         fi
  238.        
  239.    
  240.         if [ -e "$paroles" ]
  241.         then
  242.             #echo "$paroles"
  243.             cat "$paroles"
  244.             echo
  245.             echo "Lyrics correct ? (Y/n)"
  246.             read ouinon
  247.            
  248.             if [ "n" == "${ouinon,,}" ]
  249.             then
  250.                 rm -v "$paroles"
  251.             fi
  252.            
  253.         fi
  254.    
  255.     elif [ -e $paroles ]
  256.     then
  257.         #clear
  258.         echo $paroles
  259.         cat $paroles
  260.         echo
  261.     fi
  262. fi
  263.  
  264. exit 0
Add Comment
Please, Sign In to add comment