1 | #!/bin/bash |
---|
2 | export TEXTDOMAIN="zero-center" |
---|
3 | |
---|
4 | MSG_DONWLOADED=$(gettext "is downloaded in your directory") |
---|
5 | MSG_STARTING=$(gettext "Starting to download your file") |
---|
6 | MSG_CANCELLED=$(gettext "Your dowloading has been cancelled about your file") |
---|
7 | MSG_URL_NOT_EXISTS=$(gettext "Your URL don't exists, imposible to download the file") |
---|
8 | MSG_BUTTON_OK=$(gettext "OK") |
---|
9 | MSG_BUTTON_CANCEL=$(gettext "Cancel") |
---|
10 | MSG_DONWLOADING=$(gettext "Downloading") |
---|
11 | |
---|
12 | |
---|
13 | ZEN_BUTTON_OK="--ok-label=$MSG_BUTTON_OK" |
---|
14 | ZEN_BUTTON_CANCEL_OK="--cancel-label=$MSG_BUTTON_CANCEL $ZEN_BUTTON_OK" |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | #Esta funcion intentara bajar lo que sea sin saber si existe o no en la red |
---|
21 | #Arg1: detecta si es "-u" o no. |
---|
22 | #Arg2: URL del fichero a descargar |
---|
23 | #Arg3: Donde se descargara. |
---|
24 | function untester { |
---|
25 | if [ "$1" == "-u" ]; then |
---|
26 | #OBTENGO EL NOMBRE DEL FICHERO A DESCARGAR |
---|
27 | if [ "$4" == "" ]; then |
---|
28 | NAME_FILE=$(echo $2 | rev | cut -d "/" -f1 | rev) |
---|
29 | else |
---|
30 | NAME_FILE="$4" |
---|
31 | fi |
---|
32 | mainwget "$2" "$3" "$NAME_FILE" |
---|
33 | exit 0 |
---|
34 | fi |
---|
35 | |
---|
36 | } |
---|
37 | |
---|
38 | function check_parameters { |
---|
39 | #Compruebo que el directorio existe |
---|
40 | [ -d $2 ] || mkdir -p $2 |
---|
41 | #Compruebo que la direccion existe |
---|
42 | |
---|
43 | if wget --spider -o /tmp/axel.log "$1"; then |
---|
44 | #OBTENGO EL NOMBRE DEL FICHERO A DESCARGAR |
---|
45 | NAME_FILE=$(echo $1 | rev | cut -d "/" -f1 | rev) |
---|
46 | #Si el fichero ya estaba descargado no puedo descargarlo de nuevo, antes he de borrarlo |
---|
47 | if [ -f "$2"/"$NAME_FILE" ] ; then |
---|
48 | echo "$2/$NAME_FILE exists, you have to delete it before if you want to download it again" |
---|
49 | zenity --error --text "$2/$NAME_FILE exists, you have to delete it before if you want to download it again" $ZEN_BUTTON_OK |
---|
50 | |
---|
51 | exit 1 |
---|
52 | else |
---|
53 | #Todo es correcto no existe el fichero y si existe la URL puedo descargarlo |
---|
54 | echo "" |
---|
55 | echo "URL exists, downloading......." |
---|
56 | fi |
---|
57 | |
---|
58 | else |
---|
59 | echo "" |
---|
60 | echo "URL does not exist: $1" |
---|
61 | echo "Please check it." |
---|
62 | echo "" |
---|
63 | zenity --error --text "$MSG_URL_NOT_EXISTS" $ZEN_BUTTON_OK |
---|
64 | exit 1 |
---|
65 | fi |
---|
66 | |
---|
67 | if [ -f "/tmp/axel.log" ]; then |
---|
68 | rm /tmp/axel.log -rf |
---|
69 | fi |
---|
70 | } |
---|
71 | |
---|
72 | function helper { |
---|
73 | if [ $1 = "--help" ] || [ $1 = "-h" ]; then |
---|
74 | echo |
---|
75 | echo "----ZERO-DOWNLOADED-HELPER----" |
---|
76 | echo |
---|
77 | echo "zero-lliurex-axel <option> <arg1> <arg2> <arg3>" |
---|
78 | echo |
---|
79 | echo "-u option: you can untested if URL exsist, downloading forced" |
---|
80 | echo |
---|
81 | echo "arg1: Is the URL from you download the file" |
---|
82 | echo "arg2: Is the directory to save the file." |
---|
83 | echo "arg3: It is an optional argument, you can define the name for the untested downloads" |
---|
84 | echo |
---|
85 | echo "If arg2 is blank you can restore the download when you want, and the directory to save it, it will be the actually." |
---|
86 | echo "--------------------------------------------" |
---|
87 | echo |
---|
88 | echo "example: zero-lliurex-axel http://sourceforge.net/projects/arasuite/files/arasuite_unix_2.1.sh /tmp/borrame" |
---|
89 | echo |
---|
90 | exit 1 |
---|
91 | fi |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | function main { |
---|
96 | |
---|
97 | #Creamos un fichero que nos servirá de ayuda para escribir y leer el tanto por cien del fichero descargado |
---|
98 | touch /tmp/axel.log |
---|
99 | #En segundo plano realizaremos la descaraga del fichero almacenando su salida en nuestro log, eliminando ést al termianr la descarga |
---|
100 | (axel -o "$2" "$1" > /tmp/axel.log; if [ -f "/tmp/axel.log" ]; then rm /tmp/axel.log; fi) & |
---|
101 | |
---|
102 | #Mientras que el fichero de ayuda existe es que estamos descargando |
---|
103 | #Cuando y ano exista saldremos del while |
---|
104 | rc=0 |
---|
105 | while [ -f "/tmp/axel.log" ]; do |
---|
106 | sleep .3 |
---|
107 | #En la variable PERCENT almacenamos el tanto por cien que llevamos de descarga |
---|
108 | #accediento a la ultima linea escrita en el fichero de ayuda y seleccionando solo el dato que necesitamos. |
---|
109 | PERCENT=$( tail -1 /tmp/axel.log 2>/dev/null | grep % | sed 's/[^0-9.]*\([0-9.]*\).*/\1/' ) |
---|
110 | #Hacemos echo de los datos que necesita el zenity |
---|
111 | echo "#MSG_DONWLOADING $3 in $2 ...................$PERCENT%" |
---|
112 | echo "$PERCENT" |
---|
113 | #por ultimo entubamos la salida al zenity progress para que nos muestre el resultado en pantalla |
---|
114 | done | zenity --progress percentage=0 --auto-close --width 550 --title="Zero-Downloader-Helper" $ZEN_BUTTON_CANCEL_OK > /dev/null 2>&1 || rc=1 |
---|
115 | |
---|
116 | #Si se presiono cancel en la descarga matamos los procesos para finiquitarla |
---|
117 | |
---|
118 | if [ ${rc} -eq 1 ] ; then |
---|
119 | |
---|
120 | #Borramos el axel.log |
---|
121 | if [ -f "/tmp/axel.log" ]; then |
---|
122 | rm /tmp/axel.log -rf |
---|
123 | fi |
---|
124 | #CREAMOS UNA LISTA CON LOS PID NECESARIOS Y LOS FINALIZAMOS |
---|
125 | LISTA=( $( ps -aux | grep axel | grep $1 | grep -v zero | awk '{print $2}') ) |
---|
126 | #echo "The package list is....." |
---|
127 | #Comento el echo que muestra la lista |
---|
128 | #echo ${LISTA[@]} |
---|
129 | for i in "${LISTA[@]}"; do |
---|
130 | #echo "You cancel zero-lliurex-axel process with PID $i" |
---|
131 | kill $i > /dev/null 2>&1 |
---|
132 | done |
---|
133 | rm $2/$NAME_FILE* |
---|
134 | notify-send -t 5000 -u normal -i /usr/share/icons/lliurex-neu/scalable/apps/zero-center.svg "Zero-Downloader-Helper" "$MSG_CANCELLED $NAME_FILE" |
---|
135 | echo "Your downloading file $1 has been cancelled" |
---|
136 | echo "" |
---|
137 | exit 1 |
---|
138 | fi |
---|
139 | notify-send -t 5000 -u normal -i /usr/share/icons/lliurex-neu/scalable/apps/zero-center.svg "Zero-Downloader-Helper" "$NAME_FILE $MSG_DONWLOADED $2" |
---|
140 | echo "File $1 is downloaded in $2" |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | function mainwget { |
---|
146 | #Arg1: La URL del fichero a descargar |
---|
147 | #Arg2: PATH de descarga del fichero |
---|
148 | #Arg3: Nombre del fichero una vez descargado. |
---|
149 | |
---|
150 | #Me aseguro que el nombre del fichero a descargar no es muy largo |
---|
151 | NAME=$(echo "$3" | cut -c1-35) |
---|
152 | |
---|
153 | #variable que nos indica que se cancela o no la descarga del proceso |
---|
154 | rc=0 |
---|
155 | |
---|
156 | #intruccion de descarga del fichero: |
---|
157 | # wget outputs progress messages that look like this: |
---|
158 | # 0K .......... .......... .......... .......... .......... 0% 823K 40s |
---|
159 | # This function replaces each such line with the pair of lines |
---|
160 | # 0% |
---|
161 | # # Downloading... 823K (40s) |
---|
162 | # It uses minimal buffering, so each line is output immediately |
---|
163 | # and the user can watch progress as it happens. |
---|
164 | # Old instruction: |
---|
165 | #( wget -O "$2"/"$3" "$1" 2>&1 | grep --line-buffered -o "[0-9]*%" | sed -u -e 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Downloaded \1\ from "'$NAME'" at \2\/s, ETA \3/' | zenity --progress percentage=0 --auto-close --auto-kill --width 600 --title="Zero-Downloader-Helper" > /dev/null 2>&1 )|| rc=1 |
---|
166 | ( wget -O "$2"/"$3" "$1" 2>&1 | sed -u 's/.*\ \([0-9]\+\)%\ \+\(.*\)$/\1\n'#$MSG_DONWLOADING' \1\% from "'$NAME'" at \2/' | zenity --progress percentage=0 --auto-close --auto-kill --width 600 --title="Zero-Downloader-Helper" --text="$MSG_STARTING '$NAME'........." > /dev/null 2>&1 )|| rc=1 |
---|
167 | |
---|
168 | #Esta parte cancela el proceso del wget si se cancela la descarga en el zenity. |
---|
169 | |
---|
170 | if [ ${rc} -eq 1 ] ; then |
---|
171 | |
---|
172 | #CREAMOS UNA LISTA CON LOS PID NECESARIOS Y LOS FINALIZAMOS |
---|
173 | LISTA=( $( ps -aux | grep wget | grep $1 | awk '{print $2}') ) |
---|
174 | #echo "The package list is....." |
---|
175 | #Comento el echo que muestra la lista |
---|
176 | #echo ${LISTA[@]} |
---|
177 | for i in "${LISTA[@]}"; do |
---|
178 | #echo "You cancel zero-lliurex-axel process with PID $i" |
---|
179 | kill $i > /dev/null 2>&1 |
---|
180 | |
---|
181 | done |
---|
182 | rm "$2"/"$3"* |
---|
183 | notify-send -t 5000 -u normal -i /usr/share/icons/lliurex-neu/scalable/apps/zero-center.svg "Zero-Downloader-Helper" "$MSG_CANCELLED $1" |
---|
184 | echo "Your downloading file $1 has been cancelled" |
---|
185 | echo "" |
---|
186 | exit 1 |
---|
187 | fi |
---|
188 | notify-send -t 5000 -u normal -i /usr/share/icons/lliurex-neu/scalable/apps/zero-center.svg "Zero-Downloader-Helper" "$1 $MSG_DONWLOADED $2" |
---|
189 | echo "File $3 is downloaded in $2" |
---|
190 | } |
---|
191 | |
---|
192 | helper "$1" |
---|
193 | untester "$1" "$2" "$3" "$4" |
---|
194 | check_parameters "$1" "$2" |
---|
195 | main "$1" "$2" "$NAME_FILE" |
---|
196 | |
---|
197 | exit 0 |
---|
198 | |
---|