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
baseDir=/mnt/e/unsplash_pic paramsStr=""
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} rm $name rm $picName done }
function add_query_param() { if [ -z "$paramsStr" ]; then paramsStr="$1" else paramsStr="$paramsStr&$1" fi return 0 }
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 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"
authHeader='Authorization: Client-ID ${clientId}'
echo "url: $url" curl -H "$authHeader" $url >$returnFile jq -M .[].links.download_location $returnFile >$downloadUrlFile
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 ################"
|