Unsplash 图片下载脚本

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# description: download pictures from https://unsplash.com/ with curl, jq and axel
# dependency: apt install jq; apt install axel
# author: guo
# date: 2020-05-30

baseDir=/mnt/e/unsplash_pic
paramsStr=""

# kill all axel download processes
# delete all temporary download file: picName.jpg.st, picName.jpg(unfinished)
function clean_func() {
ps -aux | grep 'axel -[n]' | awk '{ print $2 }' | xargs -n 1 kill -9
for name in $(find $baseDir -type f -name "*.jpg.st"); do
picName=${name%.st}
# echo "deleting $name and $picName"
rm $name
rm $picName
done
}

# function to append request query parameters
function add_query_param() {
if [ -z "$paramsStr" ]; then
paramsStr="$1"
else
paramsStr="$paramsStr&$1"
fi
return 0
}

# 2-SIGINT ctrl+c
# 9-SIGKILL forced termination
# 15-SIGTERM default termination behaviour
# 20-SIGTSTP ctrl+z
# trap signal outside, make all download stop and do clean suff
trap 'clean_func' 2 9 15 20

while getopts 'vrq:d:f:n:' OPT; do
case "$OPT" in
v)
showVerbose=1
;;
r)
isRandom=1
;;
f)
add_query_param "featured=$OPTARG"
;;
n)
add_query_param "count=$OPTARG"
;;
d)
add_query_param "orientation=$OPTARG"
;;
q)
add_query_param "query=$OPTARG"
;;
?)
echo "avaliable options: [-vr] [-f feature]" >&2 ## standard error
exit 1
;;
esac
done
shift "$(($OPTIND - 1))"

baseUrl=https://api.unsplash.com/photos
if (($isRandom == 1)); then
url=$baseUrl/random
else
url=$baseUrl
fi

if [ -n "$paramsStr" ]; then
url="$url?$paramsStr"
fi

scriptDir="$baseDir/script"
returnFile="$scriptDir/pic_json.json"
downloadUrlFile="$scriptDir/pic_download_url.txt"

# specify the clientId !!!
# specify the clientId !!!
# specify the clientId !!!
authHeader='Authorization: Client-ID ${clientId}'

echo "url: $url"
curl -H "$authHeader" $url >$returnFile
jq -M .[].links.download_location $returnFile >$downloadUrlFile

# greedy replace mode, replace all " character
# acquire unsplash pi id as a part of file name, format: unsplash_${id}.jpg
# use axel to download

# todo//: follow unsplash's development guide: Triggering a Download(https://help.unsplash.com/en/articles/2511258-guideline-triggering-a-download)
# curl "$baseUrl/:$picId/download" &
while read line; do
downUrl=${line//\"/}
actUrl=$(curl -H "$authHeader" $downUrl | jq .url | tr -d "\"")

picId=$(echo $downUrl | grep --colour=never -o -P [^/]*/download | tr / _ | sed -e 's/_download//')
echo "########## $picId start downloading ##########"
axel -n 10 -o "$baseDir/unsplash_${picId}.jpg" $actUrl &
done <$downloadUrlFile
wait
echo "################ all done ################"