Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This post is a follow-up to thisthis.

This post is a follow-up to this.

This post is a follow-up to this.

Notice removed Reward existing answer by rolfl
Bounty Ended with janos's answer chosen by rolfl
Notice added Reward existing answer by rolfl
Bounty Started worth 50 reputation by rolfl
This really does not need to be bold
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm hoping I could get reviews for the new code.

I'm hoping I could get reviews for the new code.

I'm hoping I could get reviews for the new code.

I'm hoping I could get reviews for the new code.

added 577 characters in body
Source Link
Amr Ayman
  • 853
  • 1
  • 8
  • 24

I'm hoping I could get reviews for the new code.

I'm hoping I could get reviews for the new code.

#!/bin/bash # # Need to sort music, try tinkering with sort -u at the end of the load_sites function .. # Need to organize error codes .. # mkdir -p "$HOME/Music" ;   cd "$HOME/Music" ;   clear # So that any download will be in the music directory ######################################### Initial Values and Declarations ###################################### ##### needed files & folders declare -r settings_file="$HOME/.config/PlayMusic/PlayMusic.settings" ; declare -r log_file="$HOME/Desktop/PlayMusic_log.log" ; declare dropbox=false############################# ##### needed global variables declare SITES=()-r ;settings_file="$HOME/.config/PlayMusic/PlayMusic.settings" log_file="$HOME/Desktop/PlayMusic_log.log" # readonly variables declare -A Tracks ;# declarenames FILEare ;keys, sites are values declare FILE times_to_play=1 SITES=() ##### options & features declare no_choice=true ;chosen=false declaredropbox=false just_download=false ; declare double_check=false declare all=false ; declare player='mplayer' ############################################## Functions ################################################################################# function Play { # Function for playing music according to user preferences if [[ ${#1} -lt 23 ]] &&]];then notify-send "Now Playing: \"$1\" .." "$track" # Notifing the user, only if the track's name isn't too long fi for (( i=0 ; i<times_to_playi ;< times_to_play ; i++ ));do  $player "$1";done"$1" done }   function Find { # function to look for a previously downloaded file on disk local IFS=$'\n' ;   local REPLY ; unset FILE echo "Looking for '$1' on disk .. " local files=$(find ~ -name "$1*.mp3" 2>/dev/null) ; clear # The main file ( the found file, or null if not found ) .. if [[ $(echo "$files" | wc -l) -gt 1 ]];then # what to do if more than one file was found .. local file if ! $all;then # if the user is likely to be sitting on his computer and not sleeping .. echo 'File was found at various places .. Which one to use ?' select file in $files ; do"Cancel";do [[ -n $file"$file" ]] || continue   [[ "$file" == 'Cancel' ]] && exit 0  FILE="$file"   ;  break done readif IsYes -p 'Do you want to Delete the other ones ? '  if IsYes $REPLY;then';then for file in $files;do [[ "$file" == "$FILE" ]] || rm "$file" # removing the file if not the selected file done fi else file="${files[0]}" # getting the nearest file mv -f "$file" "$HOME/Music" # moving it to the music directory FILE="$HOME/Music/$(basename $"${files[0]}")" # getting that file for file in $files;do  [[ "$file" == "$FILE" ]] || rm "$file";done"$file"  # removing other files done fi else FILE="$files" ; fi ; clear } function IsYes {  d=$(echo $1 | tr [[:upper:]] [[:lower:]]) if ([[ "${d:0:1}" == 'y' ]] && [[ "${#d}" -le 4 ]]) || [[ $d == 'ok' ]];then return 0 ; else return 1fi ; ficlear } function IsNum { return $([[ "$1" =~ ^[0-9]+$ ]] && returnecho 0 || returnecho 1) } function create_settings_file { mkdir -p "$(dirname $settings_file"$settings_file")" cat > "$settings_file" <<- EOF # the default music settings file mplayer$player # the first line is always the media player false$dropbox # the second line is always for integration with dropbox # other than that is only sites .. http://ccmixter.org/view/media/samples/mixed http://ccmixter.org/view/media/remix http://mp3.com/top-downloads/genre/jazz/ http://mp3.com/top-downloads/ http://mp3.com/top-downloads/genre/rock/ http://mp3.com/top-downloads/genre/hip hop/ # empty or fully-commented lines are ignored .. http://mp3.com/top-downloads/genre/emo/ http://mp3.com/top-downloads/genre/pop/ # PS: '#' is marking a comment , and is ignored by the settings parser .. EOF if $2;then echo "# The settings file was recreated because the old one was useless or corrupt .." >> "$settings_file" nano "$settings_file" notify-send -t 450 'Here it is !' fi } function DOWNLOAD { local name="$track.mp3" Find "$track" # Looking for the specified track on disk if [[ -n $FILE ]] ;then # if the track was found on disk if [[ $(dirname "$FILE") ==!= "$HOME/Music" ]] || {]];then # if the found file is not in the music directory notify-send "$track was found at ($(dirname $FILE"$FILE")) and was moved to ($HOME/Music)" # inform the user of the changes mv -f "$FILE" "$HOME/Music" &>/dev/null ; FILE="$HOME/Music/$name" # move the file to the music home directory } FILE="$HOME/Music/$name" # updating info fi name="$FILE" fi notify-send "Downloading ($track) .." "in [ $PWD ] .." wget -cO "$name" "${Tracks[$track]}" $dropbox && cp "$name" "$HOME/Dropbox/Music" $1 && Play "$name" } function UNINSTALL { readIsYes -p 'Do you want to keep settings ? ' IsYes $REPLY || rm "$HOME/.config/PlayMusic/PlayMusic.settings" sudo rm '/usr/local/bin/PlayMusic' && sudo rm '/usr/local/man/man1/PlayMusic.1.gz' && echo 'Uninstallation Success !' &&; exit 0 } function load_sites { local counter=0 name site for SITE in "${SITES[@]}";do echo -e "\tSite"\t\033[30;3mSite #$\033[37m#\033[1m$((++counter))\033[0m\033[30;3m: '$SITE'"'\033[1;33;3m$SITE\033[1;30;3m'\033[0m" for site in $(lynx -source "$SITE" | egrep -o 'http://.*\.mp3');do # Grabbing all music links ( newline delimeted ) local name="$(echo $site | de_encode "$(basename "$site")" | sed -Ee 's/_\.mp3$/ /g'' -e 's@.*'s/@@g' -e '[email protected]@@g' -e 's@.*(%2D|-) @@g' -e 's@%2B@ @g' | sed -Ee "s@%2527@'@g" -e 's@%2528@(@g' -e\+|_/ 's@%2529@)@g'/g' -e 's@.* - @@' | sed -Ee "s@(\w+) (s$|s )@\1'\2@")" # Filtering names out of sites .. echo "$name" | grep -q '%' && continue # continue if name couldn't be correctly formatted  if $double_check;then # double checking local dupp=false ; local Ty for Ty in "${!Tracks[@]}";do if (echo "$Ty" | grep -Eq "'s/^.*+ - $name"//') || (echo "$name" | grep -Eq ".* - $Ty");then dupp=true ; break fi done ; $dupp && continue fi Tracks["$name"]="$site" # Adding the name as the key, and the site as the value done done   ;  clear if [[ ${#Tracks[@]} == 0 ]];then echo 'No Music was Found ! ' echo 'Please either check your internet connection or recreate the settings file using "( '"$(basename $0"$0") -s )"'"' >&2 exit 1 fi } function parse_settings { # function to parse settings file local IFS=$'\n' ; local line_number=0 ; local REPLYline if [[ -f "$settings_file" ]];then forif line[[ in! $(cat-r "$HOME/.config/PlayMusic/PlayMusic.settings"| "$settings_file" ]];then  sed -Eeecho 's/^"The +//g'Settings -efile 's/(.*$settings_file) *#.*/\1/g'exists, -ebut 's/(doesn't *)$//g'have -eread 's/permission *#granted .*//g')." >&2 do exit 1   fi  while read -r line;do [[ -n $line"$line" ]] || continue case $((++line_number)) in 1 ) player="$line" ;; 2 ) if [[echo "$line" =~| httpsegrep -q 'https?:.+ ]];then+';then # if a site .. SITES[${#SITES[@]}]="$line" # add it   else # if not .. [[ "$line" == 'true' ]] && dropbox=true || dropbox=false # if anything else, ignored ..   fi   ;  $dropbox && mkdir -p "$HOME/Dropbox/Music" ;; * ) SITES[${#SITES[@]}]="$line" ;; esac done < <(sed -Ee 's/^ +//g' -e 's/(.*) *#.*/\1/g' -e 's/( *)$//g' -e 's/ *#.*//g' -e 's/ /%20/g' "$settings_file") else notify-send "Settings File not Found .. !" readif IsYes -p "You haven't created your settings file .. Do you want to create it ? ";  if IsYes "$REPLY";then";then create_settings_file false else echo 'Then, The Default settings are going to be used this time ..' fi fi if [[ ${#SITES[@]} == 0 ]];then create_settings_file true fi $all && echo -e "$(date)\n###############" >> "$log_file" } function EVERYTHING { $just_download && notify-send 'Downloading Everything .. !' || notify-send 'Playing Everything .. !' for track in "${!Tracks[@]}";do $just_download && DOWNLOAD false || Play "${Tracks[$track]}" done   ;  exit 0 } function usage { cat <<- EOF PlayMusic [OPTION] -s ReCreate the Settings file .. -r [number] Set the number of times a track should be played .. -v View the Settings file .. -u Uninstall the Program .. -d Force download .. -p Force play .. -a -d / -p applies for everything in the list, so everything will be downloaded/played automatically .. -D Turn on double check mode .. --version Show Program Info .. EOF } ############################################### Parsing Arguments ######################################################### [[ "$1" == '--version' ]] && { echo 'PlayMusic v1.3, Licence:GPL3' ; exit 0 ; } # giving the version while getopts r:svdupaD opt;do # Getting options case $opt in r ) if IsNum "$OPTARG" && [[ "$OPTARG" -gt 1 ]];then declare times_to_play="$OPTARG" else echo 'Invalid Number of Times ..' >&2 fi ;; ;;s ) create_settings_file true s ) create_settings_file true ; notify-send "Settings File Recreated Successfully !"   ;  exit 0 ;; v ) read -p 'Editor ? ' ;editor  REPLY="${REPLY=nano}" ; $REPLY  if [[ -z "$editor" ]] || ! which "$editor" &>/dev/null;then editor='nano' fi $editor "$HOME/.config/PlayMusic/PlayMusic.settings"   ;  exit 0 ;; u ) readIsYes -p 'Are you sure you want to UnInstall ? ' ; IsYes $REPLY && UNINSTALL ;; d ) just_download=truedeclare ;just_download=true no_choice=falsechosen=true ;; # download without asking p ) just_download=falsedeclare ;just_download=false no_choice=falsechosen=true ;; # play online without asking a ) declare all=true ;; D ) declare double_check=true ;; # turn on double check mode ( it is an experimantal feature for now .. )  \? ) usage >&2; exit 2;; esac done if $all && $no_choice;then! $chosen;then echo "All what ? use -d or -p option to specify what to do with each of them .." >&2 ; exit 2 ; fi ############################################### Starting Work ############################################################### parse_settings ;   clear ; echo printf "Loading"\033[0;37mLoading ($\033[1m${#SITES[@]}\033[0;37m) Sites .." ; \n" load_sites ;   $all && EVERYTHING select track in "${!Tracks[@]}";do # interacting with the USER .. [[ -n $track ]] || continue # Continues the loop if choice is empty ( meaning that user's choice wasn't appropriate ) ..  printf '\033[0m'  clear if ! $no_choice;then$chosen;then # if the user already specified something to do as an option ( -d / -p ) $just_download && DOWNLOAD true || Play "${Tracks[$track]}"  exit 0  else select opt in "Download then Play" "Just Download" "Play Online";doOnline" "Play then ask to download";do [[ -n "$opt" ]] || continue case "$opt" in "Play Online") Play "${Tracks[$track]}" ;; "Download then Play") DOWNLOAD true ;; "Just Download") DOWNLOAD false ;;  "Play then ask to download") Play "${Tracks[$track]}" IsYes -p 'Do you want to download it ? ' && DOWNLOAD false ;;  esac exit 0break done fi done ; exit 0 

I'm hoping I could get reviews for the new code.

#!/bin/bash mkdir -p "$HOME/Music" ; cd "$HOME/Music" ; clear # So that any download will be in the music directory ######################################### Initial Values and Declarations ###################################### ##### needed files & folders declare -r settings_file="$HOME/.config/PlayMusic/PlayMusic.settings" ; declare -r log_file="$HOME/Desktop/PlayMusic_log.log" ; declare dropbox=false ##### needed global variables declare SITES=() ; declare -A Tracks ; declare FILE ; declare times_to_play=1 ##### options & features declare no_choice=true ; declare just_download=false ; declare double_check=false declare all=false ; declare player='mplayer' ############################################## Functions ############################################## function Play { # Function for playing music according to user preferences [[ ${#1} -lt 23 ]] && notify-send "Now Playing: \"$1\" .." "$track" # Notifing the user, only if the track's name isn't too long for ((i=0 ; i<times_to_play ; i++));do $player "$1";done } function Find { # function to look for a previously downloaded file on disk local IFS=$'\n' ; local REPLY ; unset FILE echo "Looking for '$1' on disk .. " local files=$(find ~ -name "$1*.mp3" 2>/dev/null) ; clear # The main file ( the found file, or null if not found ) .. if [[ $(echo "$files" | wc -l) -gt 1 ]];then # what to do if more than one file was found .. local file if ! $all;then # if the user is likely to be sitting on his computer and not sleeping .. echo 'File was found at various places .. Which one to use ?' select file in $files ; do [[ -n $file ]] || continue FILE="$file" ; break done read -p 'Do you want to Delete the other ones ? '  if IsYes $REPLY;then for file in $files;do [[ "$file" == "$FILE" ]] || rm "$file" # removing the file if not the selected file done fi else file="${files[0]}" # getting the nearest file mv -f "$file" "$HOME/Music" # moving it to the music directory FILE="$HOME/Music/$(basename ${files[0]})" # getting that file for file in $files;do [[ "$file" == "$FILE" ]] || rm "$file";done # removing other files fi else FILE="$files" ; fi ; clear } function IsYes {  d=$(echo $1 | tr [[:upper:]] [[:lower:]]) if ([[ "${d:0:1}" == 'y' ]] && [[ "${#d}" -le 4 ]]) || [[ $d == 'ok' ]];then return 0 ; else return 1 ; fi } function IsNum { [[ "$1" =~ ^[0-9]+$ ]] && return 0 || return 1 } function create_settings_file { mkdir -p "$(dirname $settings_file)" cat > "$settings_file" <<- EOF # the default music settings file mplayer # the first line is always the media player false # the second line is always for integration with dropbox # other than that is only sites .. http://ccmixter.org/view/media/samples/mixed http://ccmixter.org/view/media/remix http://mp3.com/top-downloads/genre/jazz/ http://mp3.com/top-downloads/ http://mp3.com/top-downloads/genre/rock/ http://mp3.com/top-downloads/genre/hip hop/ # empty or fully-commented lines are ignored .. http://mp3.com/top-downloads/genre/emo/ http://mp3.com/top-downloads/genre/pop/ # PS: '#' is marking a comment , and is ignored by the settings parser .. EOF if $2;then echo "# The settings file was recreated because the old one was useless or corrupt .." >> "$settings_file" nano "$settings_file" notify-send -t 450 'Here it is !' fi } function DOWNLOAD { local name="$track.mp3" Find "$track" # Looking for the specified track on disk if [[ -n $FILE ]] ;then # if the track was found on disk [[ $(dirname "$FILE") == "$HOME/Music" ]] || { # if the found file is not in the music directory notify-send "$track was found at ($(dirname $FILE)) and was moved to ($HOME/Music)" # inform the user of the changes mv -f "$FILE" "$HOME/Music" &>/dev/null ; FILE="$HOME/Music/$name" # move the file to the music home directory } name="$FILE" fi notify-send "Downloading ($track) .." "in [ $PWD ] .." wget -cO "$name" "${Tracks[$track]}" $dropbox && cp "$name" "$HOME/Dropbox/Music" $1 && Play "$name" } function UNINSTALL { read -p 'Do you want to keep settings ? ' IsYes $REPLY || rm "$HOME/.config/PlayMusic/PlayMusic.settings" sudo rm '/usr/local/bin/PlayMusic' && sudo rm '/usr/local/man/man1/PlayMusic.1.gz' && echo 'Uninstallation Success !' && exit 0 } function load_sites { local counter=0 for SITE in "${SITES[@]}";do echo -e "\tSite #$((++counter)): '$SITE'" for site in $(lynx -source "$SITE" | egrep -o 'http://.*\.mp3');do # Grabbing all music links ( newline delimeted ) local name="$(echo $site |  sed -Ee 's/_/ /g' -e 's@.*/@@g' -e '[email protected]@@g' -e 's@.*(%2D|-) @@g' -e 's@%2B@ @g' | sed -Ee "s@%2527@'@g" -e 's@%2528@(@g' -e 's@%2529@)@g' -e 's@.* - @@' | sed -Ee "s@(\w+) (s$|s )@\1'\2@")" # Filtering names out of sites .. echo "$name" | grep -q '%' && continue # continue if name couldn't be correctly formatted  if $double_check;then # double checking local dupp=false ; local Ty for Ty in "${!Tracks[@]}";do if (echo "$Ty" | grep -Eq ".* - $name") || (echo "$name" | grep -Eq ".* - $Ty");then dupp=true ; break fi done ; $dupp && continue fi Tracks["$name"]="$site" # Adding the name as the key, and the site as the value done done ; clear if [[ ${#Tracks[@]} == 0 ]];then echo 'No Music was Found ! ' echo 'Please either check your internet connection or recreate the settings file using "( '"$(basename $0) -s )"'"' >&2 exit 1 fi } function parse_settings { # function to parse settings file local IFS=$'\n' ; local line_number=0 ; local REPLY if [[ -f "$settings_file" ]];then for line in $(cat "$HOME/.config/PlayMusic/PlayMusic.settings"|  sed -Ee 's/^ +//g' -e 's/(.*) *#.*/\1/g' -e 's/( *)$//g' -e 's/ *#.*//g') do [[ -n $line ]] || continue case $((++line_number)) in 1 ) player="$line" ;; 2 ) if [[ "$line" =~ https?:.+ ]];then # if a site .. SITES[${#SITES[@]}]="$line" # add it else # if not .. [[ "$line" == 'true' ]] && dropbox=true || dropbox=false # if anything else, ignored .. fi ; $dropbox && mkdir -p "$HOME/Dropbox/Music" ;; * ) SITES[${#SITES[@]}]="$line" ;; esac done else notify-send "Settings File not Found .. !" read -p "You haven't created your settings file .. Do you want to create it ? ";  if IsYes "$REPLY";then create_settings_file false else echo 'Then, The Default settings are going to be used this time ..' fi fi if [[ ${#SITES[@]} == 0 ]];then create_settings_file true fi $all && echo -e "$(date)\n###############" >> "$log_file" } function EVERYTHING { $just_download && notify-send 'Downloading Everything .. !' || notify-send 'Playing Everything .. !' for track in "${!Tracks[@]}";do $just_download && DOWNLOAD false || Play "${Tracks[$track]}" done ; exit 0 } ############################################### Parsing Arguments ################################# [[ "$1" == '--version' ]] && { echo 'PlayMusic v1.3, Licence:GPL3' ; exit 0 ; } # giving the version while getopts r:svdupaD opt;do # Getting options case $opt in r ) if IsNum "$OPTARG" && [[ "$OPTARG" -gt 1 ]];then times_to_play="$OPTARG" else echo 'Invalid Number of Times ..' >&2 fi ;; s ) create_settings_file true ; notify-send "Settings File Recreated Successfully !" ; exit 0 ;; v ) read -p 'Editor ? ' ; REPLY="${REPLY=nano}" ; $REPLY "$HOME/.config/PlayMusic/PlayMusic.settings" ; exit 0 ;; u ) read -p 'Are you sure you want to UnInstall ? ' ; IsYes $REPLY && UNINSTALL ;; d ) just_download=true ; no_choice=false ;; # download without asking p ) just_download=false ; no_choice=false ;; # play online without asking a ) all=true ;; D ) double_check=true ;; # turn on double check mode ( it is an experimantal feature for now .. ) esac done if $all && $no_choice;then echo "All what ? use -d or -p option to specify what to do with each of them .." >&2 ; exit 2 ; fi ############################################### Starting Work ##################################### parse_settings ; clear ; echo "Loading (${#SITES[@]}) Sites .." ; load_sites ; $all && EVERYTHING select track in "${!Tracks[@]}";do # interacting with the USER .. [[ -n $track ]] || continue # Continues the loop if choice is empty ( meaning that user's choice wasn't appropriate ) .. clear if ! $no_choice;then # if the user already specified something to do as an option ( -d / -p ) $just_download && DOWNLOAD true || Play "${Tracks[$track]}"  exit 0  else select opt in "Download then Play" "Just Download" "Play Online";do [[ -n "$opt" ]] || continue case "$opt" in "Play Online") Play "${Tracks[$track]}" ;; "Download then Play") DOWNLOAD true ;; "Just Download") DOWNLOAD false ;; esac exit 0 done fi done ; exit 0 

I'm hoping I could get reviews for the new code.

#!/bin/bash # # Need to sort music, try tinkering with sort -u at the end of the load_sites function .. # Need to organize error codes .. # mkdir -p "$HOME/Music"  cd "$HOME/Music"  clear # So that any download will be in the music directory ######################################### Initial Values and Declarations ############################# ##### needed global variables declare -r settings_file="$HOME/.config/PlayMusic/PlayMusic.settings" log_file="$HOME/Desktop/PlayMusic_log.log" # readonly variables declare -A Tracks # names are keys, sites are values declare FILE times_to_play=1 SITES=() ##### options & features declare chosen=false dropbox=false just_download=false double_check=false all=false player='mplayer' ############################################## Functions ################################### function Play { # Function for playing music according to user preferences if [[ ${#1} -lt 23 ]];then notify-send "Now Playing: \"$1\" .." "$track" # Notifing the user, only if the track's name isn't too long fi for (( i=0 ; i < times_to_play ; i++ ));do  $player "$1" done }   function Find { # function to look for a previously downloaded file on disk local IFS=$'\n'   unset FILE echo "Looking for '$1' on disk .. " local files=$(find ~ -name "$1*.mp3" 2>/dev/null) ; clear # The main file ( the found file, or null if not found ) .. if [[ $(echo "$files" | wc -l) -gt 1 ]];then # what to do if more than one file was found .. local file if ! $all;then # if the user is likely to be sitting on his computer and not sleeping .. echo 'File was found at various places .. Which one to use ?' select file in $files "Cancel";do [[ -n "$file" ]] || continue   [[ "$file" == 'Cancel' ]] && exit 0  FILE="$file"     break done if IsYes -p 'Do you want to Delete the other ones ? ';then for file in $files;do [[ "$file" == "$FILE" ]] || rm "$file" # removing the file if not the selected file done fi else file="${files[0]}" # getting the nearest file mv -f "$file" "$HOME/Music" # moving it to the music directory FILE="$HOME/Music/$(basename "${files[0]}")" # getting that file for file in $files;do  [[ "$file" == "$FILE" ]] || rm "$file"   done fi else FILE="$files" fi ; clear } function IsNum { return $([[ "$1" =~ ^[0-9]+$ ]] && echo 0 || echo 1) } function create_settings_file { mkdir -p "$(dirname "$settings_file")" cat > "$settings_file" <<- EOF # the default music settings file $player # the first line is always the media player $dropbox # the second line is always for integration with dropbox # other than that is only sites .. http://ccmixter.org/view/media/samples/mixed http://ccmixter.org/view/media/remix http://mp3.com/top-downloads/genre/jazz/ http://mp3.com/top-downloads/ http://mp3.com/top-downloads/genre/rock/ http://mp3.com/top-downloads/genre/hip hop/ # empty or fully-commented lines are ignored .. http://mp3.com/top-downloads/genre/emo/ http://mp3.com/top-downloads/genre/pop/ # PS: '#' is marking a comment , and is ignored by the settings parser .. EOF if $2;then echo "# The settings file was recreated because the old one was useless or corrupt .." >> "$settings_file" nano "$settings_file" notify-send -t 450 'Here it is !' fi } function DOWNLOAD { local name="$track.mp3" Find "$track" # Looking for the specified track on disk if [[ -n $FILE ]] ;then # if the track was found on disk if [[ $(dirname "$FILE") != "$HOME/Music" ]];then # if the found file is not in the music directory notify-send "$track was found at ($(dirname "$FILE")) and was moved to ($HOME/Music)" # inform the user of the changes mv -f "$FILE" "$HOME/Music" &>/dev/null # move the file to the music home directory  FILE="$HOME/Music/$name" # updating info fi name="$FILE" fi notify-send "Downloading ($track) .." "in [ $PWD ] .." wget -cO "$name" "${Tracks[$track]}" $dropbox && cp "$name" "$HOME/Dropbox/Music" $1 && Play "$name" } function UNINSTALL { IsYes -p 'Do you want to keep settings ? ' || rm "$HOME/.config/PlayMusic/PlayMusic.settings" sudo rm '/usr/local/bin/PlayMusic' && sudo rm '/usr/local/man/man1/PlayMusic.1.gz' && echo 'Uninstallation Success !' ; exit 0 } function load_sites { local counter=0 name site for SITE in "${SITES[@]}";do echo -e "\t\033[30;3mSite \033[37m#\033[1m$((++counter))\033[0m\033[30;3m: '\033[1;33;3m$SITE\033[1;30;3m'\033[0m" for site in $(lynx -source "$SITE" | egrep -o 'http://.*\.mp3');do # Grabbing all music links ( newline delimeted ) name="$(de_encode "$(basename "$site")" | sed -Ee 's/\.mp3$//' -e 's/\+|_/ /g' -e 's/^.+ - //')" Tracks["$name"]="$site" # Adding the name as the key, and the site as the value done done     clear if [[ ${#Tracks[@]} == 0 ]];then echo 'No Music was Found ! ' echo 'Please either check your internet connection or recreate the settings file using "( '"$(basename "$0") -s )"'"' >&2 exit 1 fi } function parse_settings { # function to parse settings file local line_number=0 line if [[ -f "$settings_file" ]];then if [[ ! -r "$settings_file" ]];then  echo "The Settings file ($settings_file) exists, but doesn't have read permission granted .." >&2  exit 1   fi  while read -r line;do [[ -n "$line" ]] || continue case $((++line_number)) in 1 ) player="$line" ;; 2 ) if echo "$line" | egrep -q 'https?:.+';then # if a site .. SITES[${#SITES[@]}]="$line" # add it   else # if not .. [[ "$line" == 'true' ]] && dropbox=true || dropbox=false # if anything else, ignored ..   fi     $dropbox && mkdir -p "$HOME/Dropbox/Music" ;; * ) SITES[${#SITES[@]}]="$line" ;; esac done < <(sed -Ee 's/^ +//g' -e 's/(.*) *#.*/\1/g' -e 's/( *)$//g' -e 's/ *#.*//g' -e 's/ /%20/g' "$settings_file") else notify-send "Settings File not Found .. !" if IsYes -p "You haven't created your settings file .. Do you want to create it ? ";then create_settings_file false else echo 'Then, The Default settings are going to be used this time ..' fi fi if [[ ${#SITES[@]} == 0 ]];then create_settings_file true fi $all && echo -e "$(date)\n###############" >> "$log_file" } function EVERYTHING { $just_download && notify-send 'Downloading Everything .. !' || notify-send 'Playing Everything .. !' for track in "${!Tracks[@]}";do $just_download && DOWNLOAD false || Play "${Tracks[$track]}" done     exit 0 } function usage { cat <<- EOF PlayMusic [OPTION] -s ReCreate the Settings file .. -r [number] Set the number of times a track should be played .. -v View the Settings file .. -u Uninstall the Program .. -d Force download .. -p Force play .. -a -d / -p applies for everything in the list, so everything will be downloaded/played automatically .. -D Turn on double check mode .. --version Show Program Info .. EOF } ############################################### Parsing Arguments ######################## [[ "$1" == '--version' ]] && { echo 'PlayMusic v1.3, Licence:GPL3' ; exit 0 ; } # giving the version while getopts r:svdupaD opt;do # Getting options case $opt in r ) if IsNum "$OPTARG" && [[ "$OPTARG" -gt 1 ]];then declare times_to_play="$OPTARG" else echo 'Invalid Number of Times ..' >&2 fi ;; s ) create_settings_file true notify-send "Settings File Recreated Successfully !"     exit 0 ;; v ) read -p 'Editor ? ' editor    if [[ -z "$editor" ]] || ! which "$editor" &>/dev/null;then editor='nano' fi $editor "$HOME/.config/PlayMusic/PlayMusic.settings"     exit 0 ;; u ) IsYes -p 'Are you sure you want to UnInstall ? ' && UNINSTALL ;; d ) declare just_download=true chosen=true ;; # download without asking p ) declare just_download=false chosen=true ;; # play online without asking a ) declare all=true ;; D ) declare double_check=true ;; # turn on double check mode ( it is an experimantal feature for now .. )  \? ) usage >&2; exit 2;; esac done if $all && ! $chosen;then echo "All what ? use -d or -p option to specify what to do with each of them .." >&2 ; exit 2 ; fi ############################################### Starting Work ########################## parse_settings  clear printf "\033[0;37mLoading (\033[1m${#SITES[@]}\033[0;37m) Sites ..\n" load_sites  $all && EVERYTHING select track in "${!Tracks[@]}";do # interacting with the USER .. [[ -n $track ]] || continue # Continues the loop if choice is empty ( meaning that user's choice wasn't appropriate ) ..  printf '\033[0m'  clear if $chosen;then # if the user already specified something to do as an option ( -d / -p ) $just_download && DOWNLOAD true || Play "${Tracks[$track]}" else select opt in "Download then Play" "Just Download" "Play Online" "Play then ask to download";do [[ -n "$opt" ]] || continue case "$opt" in "Play Online") Play "${Tracks[$track]}" ;; "Download then Play") DOWNLOAD true ;; "Just Download") DOWNLOAD false ;;  "Play then ask to download") Play "${Tracks[$track]}" IsYes -p 'Do you want to download it ? ' && DOWNLOAD false ;;  esac break done fi done 
Tweeted twitter.com/#!/StackCodeReview/status/467332921494237185
improved formatting, and fixed the code when I noticed it wasn't working as it should be ..
Source Link
Amr Ayman
  • 853
  • 1
  • 8
  • 24
Loading
added 157 characters in body
Source Link
Amr Ayman
  • 853
  • 1
  • 8
  • 24
Loading
deleted 42 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Amr Ayman
  • 853
  • 1
  • 8
  • 24
Loading