Skip to content

Instantly share code, notes, and snippets.

@fzerorubigd
Created September 15, 2012 18:11
Show Gist options
  • Save fzerorubigd/3729118 to your computer and use it in GitHub Desktop.
Save fzerorubigd/3729118 to your computer and use it in GitHub Desktop.
Wget with zenity
DOWNLOAD() {
rand="$RANDOM `date`"
pipe="/tmp/pipe.`echo '$rand' | md5sum | tr -d ' -'`"
mkfifo $pipe
wget -c $1 2>&1 | while read data;do
if [ "`echo $data | grep '^Length:'`" ]; then
total_size=`echo $data | grep "^Length:" | sed 's/.*\((.*)\).*/\1/' | tr -d '()'`
fi
if [ "`echo $data | grep '[0-9]*%' `" ];then
percent=`echo $data | grep -o "[0-9]*%" | tr -d '%'`
current=`echo $data | grep "[0-9]*%" | sed 's/\([0-9BKMG]\+\).*/\1/' `
speed=`echo $data | grep "[0-9]*%" | sed 's/.*\(% [0-9BKMG.]\+\).*/\1/' | tr -d ' %'`
remain=`echo $data | grep -o "[0-9A-Za-z]*$" `
echo $percent
echo "#Downloading $1\n$current of $total_size ($percent%)\nSpeed : $speed/Sec\nEstimated time : $remain"
fi
done > $pipe &
wget_info=`ps ax |grep "wget.*$1" |awk '{print $1"|"$2}'`
wget_pid=`echo $wget_info|cut -d'|' -f1 `
zenity --progress --auto-close --text="Connecting to $1\n\n\n" --width="350" --title="Downloading"< $pipe
if [ "`ps -A |grep "$wget_pid"`" ];then
kill $wget_pid
fi
rm -f $pipe
}
if [ $1 ];then
DOWNLOAD "$1"
else
dllink=$(zenity --entry --text "Your download link :" --width="350" --entry-text "" --title="Download url")
if [ $dllink ];then
DOWNLOAD "$dllink"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment