forked from simonjava/tinypng_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinypng.sh
executable file
·82 lines (79 loc) · 1.91 KB
/
tinypng.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
keys=(
"-QGvZ7H-4RFzjizRMsNf-rVVI_UTnmYK"
"Zkf1LHXMWyyGcpUhsgH8bjET9j5oSw7S"
"DC7Fxhf-iEw40XfDmAb3KrWRnEPfBEDN"
"qJ2q_PJPcwPNkh-67iyBFLjja08hTaF6"
"zZBeZRbbiuqn0k2H8Cl-3l66lPWCZ6VB"
"BoOoA1UulwN_4-O_MN4UyN3U9YAm1ZUC"
"bNQajkm4E5swM-T3eYUr9l_Hkpmg2SWM"
"92eMC5fvjMhqyn3rnFhvnV7HH4QyQAR4"
"T7V6Hoqj43O9mmot0wBB1Ic6A-XyTK0g"
"ofXiw_AinfTA3XvK6TQ8gemLp32UqeGf"
"HqeEITFeyKmkRME0tC3jaQBHmkSZuzUV"
"8GLzzhNJPzruTOVysu9yD39kh-J0QGlH"
)
key_index=0
echo $key
echo "" >> tinypng_record.txt
removeQuotation(){
val=$1
len=${#val}
echo ${val:1:len-2}
}
compress(){
echo "begin compress $img"
conent="curl --user api:${keys[key_index]} --data-binary @$1 https://api.tinify.com/shrink"
echo "exec:$conent"
response2=`$conent`
echo $response2
error=`echo $response2|jq ".error"`
if [[ x$error != x"null" ]]; then
echo "error msg:$error"
key_index=`expr $key_index + 1`
length=${#keys[@]}
if [[ $key_index -ge $length ]]; then
echo "no useful key,last file $img"
exit
else
compress $1
fi
else
url=`echo $response2|jq ".output.url"`
url=$(removeQuotation $url)
echo "begin download url:$url"
curl $url -o $img
echo "done $url"
echo "$1 " >> tinypng_record.txt
echo ""
fi
}
list_alldir(){
for file2 in `ls -a $1`
do
if [ x"$file2" != x"." -a x"$file2" != x".." ];then
if [ -d "$1/$file2" ];then
if [[ x$file2 = x"build" ]]; then
echo "skip build dir"
else
list_alldir "$1/$file2"
fi
else
img="$1/$file2"
result=`cat tinypng_record.txt | grep $img`
if [[ x$result != x"" ]]; then
echo "skip $img,have already compress"
continue;
fi
right=${img##*.}
if [[ x"$right" = x"png" ]]; then
compress $img
fi
if [[ x"$right" = x"jpg" ]]; then
compress $img
fi
fi
fi
done
}
list_alldir .