Created
September 21, 2016 13:38
-
-
Save dvdvck/9d9458d0df411ec3adf15d3e32287ce2 to your computer and use it in GitHub Desktop.
Consume la api de tinify.com, servicio otorgado por tinyjpg.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#Itera los directorios donde están montados las imagenes que se quieren | |
#optimizar a traves del api de tinyjpg.com. | |
# | |
#La estructura de directorios es la siguiente: | |
# /mnt/the-void/[unidad_medica]/Digital/Anexo C/[dept]/[filename].jpg | |
#Para su ejecucion se coloca CWD desde el directorio de la unidad medica | |
#que se va a operar | |
#Se realiza la peticion y la respuesta se almacena en | |
#~/t6e/issemym/img-proc/[unidad_medica]/[dept]/meta/[filename].txt | |
# | |
#En otro proceso se usa el path de [filename].txt para descargar la imagen | |
#en ~/t6e/issemym/img-proc/[unidad_medica]/[dept]/img/[filename].jpg | |
# | |
unit=${1?Falta unidad medica} | |
dest=$HOME/t6e/issemym/img-proc | |
if [ -z ${API:+x} ]; then echo "falta API"; exit 1; fi | |
counter=${2-0} | |
echo "Inicando el contador en $counter" | |
# setup log file | |
#logfile=$dest/slurp$(date +%F_%T).log | |
logfile=$dest/slurp.log | |
exec &> >(tee -a $logfile) | |
#exec 2> >(tee -ia $logfile >&2) | |
while IFS= read -r -d '' i; do | |
if [ "$counter" -ge 500 ]; then | |
echo "Se alcanzaron $counter peticiones" | |
exit 3 | |
fi | |
filename=${i##*/} | |
name=${filename%.*} | |
path=${i%/*} | |
#echo "$path" | |
#echo "$name" | |
#si no existe lo crea | |
#echo "$dest/$path" | |
[ -d "$dest/$path" ] || mkdir -p "$dest/$path" | |
#envia la peticion | |
source="@$PWD/$i" | |
output="$dest/$path/$name.txt" | |
echo "$filename" | |
#echo "$output" | |
if [ ! -e "$output" ]; then | |
#cp "$output.meme" "$output" | |
#last=0 | |
curl -Ss https://api.tinify.com/shrink -u api:$API --data-binary "$source" -o "$output" | |
last=$? | |
grep -q error "$output" | |
if [ "$last" -ne 0 ] || [ $? -eq 0 ]; then | |
echo "curl error $last on file $filename" | |
cat "$output" | |
unlink "$output" | |
exit 2 | |
fi | |
((counter++)) | |
echo "Procesado $counter" | |
fi | |
done < <(find "$unit/Digital/Anexo C/" -name *.jpg -print0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment