-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr-downloader.sh
69 lines (54 loc) · 2.27 KB
/
tumblr-downloader.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
#!/usr/bin/bash
# Home: https://github.com/Pernat1y/tumblr-downloader
# Tumblr API reference: https://www.tumblr.com/docs/en/api/v2
tumblr_app_key=
tumblr_blog_url=`echo $1 | sed 's/https://g; s/http://g; s/\///g'`
tumblr_tags=$2
tumblr_post_offset=0
tumble_total_posts=
if [ -z $1 ]; then
echo "Usage: $0 blog.tumblr.com [tag]"
exit
fi
which curl jq >>/dev/null
if [ $? -ne "0" ]; then
echo "I need curl ( https://curl.haxx.se ) and jq ( https://github.com/stedolan/jq ) to work"
exit
fi
if [ -z $tumblr_app_key ]; then
echo "You need own API key. You can get one here:"
echo "https://www.tumblr.com/oauth/apps"
echo "then set tumblr_app_key variable at the top of this script"
exit
fi
tumble_total_posts=`curl --silent --referer "https://www.tumblr.com/dashboard" --user-agent "Mozilla/5.0" --retry 3 --retry-delay 3 \
"https://api.tumblr.com/v2/blog/$tumblr_blog_url/info?api_key=$tumblr_app_key" |\
jq '.response | .blog | .total_posts' 2>/dev/null`
if [ -z $tumble_total_posts ]; then
echo "Nothing found or too many requests."
exit
fi
echo "There are $tumble_total_posts posts. Getting list of URLs to download..."
sleep 10
mkdir -p "$tumblr_blog_url/$tumblr_tags" 2>/dev/null
cd "$tumblr_blog_url/$tumblr_tags"
if [ $? -ne "0" ]; then
echo "Unable to create/enter directory. Check free space and permissions on current directory."
exit
fi
while [ $tumblr_post_offset -lt $tumble_total_posts ]; do
echo "Downloading images. Page $tumblr_post_offset from $tumble_total_posts"
curl --silent --referer "https://www.tumblr.com/dashboard" --user-agent "Mozilla/5.0" --retry 3 --retry-delay 3 \
"https://api.tumblr.com/v2/blog/$tumblr_blog_url/posts/photo?api_key=$tumblr_app_key&tag=$tumblr_tags&offset=$tumblr_post_offset&limit=20" |\
jq '.response | .posts | .[] | .photos | .[] | .original_size | .url' 2>/dev/null > $tumblr_blog_url.list
if [ $? -eq "5" ]; then
echo "Error parsing output. Too many requests or API was changed."
exit
fi
for tumblr_download_url in `cat $tumblr_blog_url.list | sed 's/\"//g'`; do
curl --silent --referer "https://www.tumblr.com/dashboard" --user-agent "Mozilla/5.0" \
--continue-at - --remote-name --remote-name-all --retry 3 --retry-delay 3 $tumblr_download_url
done
tumblr_post_offset=`expr $tumblr_post_offset + 20`
done
echo "Done."