· 7 years ago · Feb 24, 2018, 08:50 PM
1import pytumblr, json, urllib, datetime, os
2
3client = pytumblr.TumblrRestClient(
4 '<consumer_key>',
5 '<consumer_secret>',
6 '<oauth_token>',
7 '<oauth_secret>',
8)
9
10limit = 50
11tags = ["tag1", "tag2"]
12profile_url = 'username.tumblr.com'
13
14for tag in tags:
15 print("Current tag: %s" % tag)
16 offset = 0
17 while True:
18 print("Current offset: %d" % offset)
19 current = offset
20 response = client.posts(profile_url, offset=offset, limit=limit, tag=tag, type="photo")
21 for post in response["posts"]:
22 date = datetime.datetime.strptime(post["date"], "%Y-%m-%d %H:%M:%S GMT")
23 photo_num = 1
24 for photo in post["photos"]:
25 out_name = "output/%s-%s-%s_%s_%s.jpg" % (date.year, date.month, date.day, post["id"], photo_num)
26 if not os.path.isfile(out_name):
27 print("[%s][%d] Downloading... Output: %s" % (tag, current, out_name))
28 urllib.request.urlretrieve(photo["original_size"]["url"], out_name)
29 else: print("[%s][%d] file exists." % (tag, current))
30 photo_num += 1
31 current += 1
32
33 if len(response["posts"]) < 20:
34 break
35 offset += limit