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