-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.sh
81 lines (65 loc) · 2.04 KB
/
script.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
ALBUM_ID=$1
get_album_info() {
USER_AGENT='Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0'
ACCEPT='application/json, text/javascript, */*'
ACCEPT_LANGUAGE='en-US,en;q=0.5'
CONTENT_TYPE='application/x-www-form-urlencoded; charset=UTF-8'
X_REQUESTED_WITH='XMLHttpRequest'
ORIGIN='https://ibb.co'
dataRawACTION="get-album-contents&albumid=${ALBUM_ID}"
curl 'https://ibb.co/json' --silent -X POST -H "User-Agent: ${USER_AGENT}" -H "Accept: ${ACCEPT}" -H "Accept-Language: ${ACCEPT_LANGUAGE}" --compressed -H "Content-Type: ${CONTENT_TYPE}" -H "X-Requested-With: ${X_REQUESTED_WITH}" -H "Origin: ${ORIGIN}" --data-raw "action=${dataRawACTION}"
}
download_album() {
echo "Getting album info"
jsonResponse=$(get_album_info)
numberOfAlbumItems=$(echo "$jsonResponse" | jq -r ".album.image_count")
echo "Album contains '${numberOfAlbumItems}' items."
for (( i=0;i<${numberOfAlbumItems}; i++)); do
current_url=$(echo "$jsonResponse" | jq -r ".contents[${i}].url")
filename=$(basename ${current_url})
if ! [ -f ${filename} ]; then
echo "Saving '${filename}'"
curl ${current_url} --silent -o ${filename}
else
echo "'${filename}' exists. Skipping"
fi
done
echo "Ok. Downloaded album '${ALBUM_ID}'"
}
main() {
USAGE="\n\tbash script.sh <album_id>\n"
if [[ -z ${ALBUM_ID} ]]; then
echo "Album ID can't be empty."
echo -e ${USAGE}
exit 1
elif ! command -v curl &> /dev/null; then
echo 'I require "curl". Please install'
exit 1
elif ! command -v jq &> /dev/null; then
echo 'I require "jq". Please install'
exit 1
else
ANSWER=true
directory_name="ibb.co-${ALBUM_ID}"
if [[ -d ${directory_name} ]]; then
echo "directory ${directory_name} exists."
while true; do
read -p "run script anyway? [Y/n]: " yn
case $yn in
[Yy]) break ;;
[Nn]) ANSWER=false; break ;;
"") break ;;
esac
done
else
mkdir ${directory_name}
fi
if $ANSWER; then
(cd ${directory_name}; download_album)
else
echo "not running"
fi
exit 0
fi
}
main