Skip to content

Commit f234953

Browse files
committed
tumblr_backup: add option -l/--likes to backup likes
Fixes #67
1 parent 2a74bcb commit f234953

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

tumblr_backup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ feasible, download and install from the links above.
4848
-D, --dirs save each post in its own folder
4949
-q, --quiet suppress progress messages
5050
-i, --incremental incremental backup mode
51+
-l, --likes save a blog's likes, not its posts
5152
-j, --json save the original JSON source
5253
-k, --skip-images do not save images; link to Tumblr instead
5354
--save-video save video files

tumblr_backup.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def get_api_url(account):
173173
blog_name = account
174174
if '.' not in account:
175175
blog_name += '.tumblr.com'
176-
return 'https://api.tumblr.com/v2/blog/' + blog_name + '/posts'
176+
return 'https://api.tumblr.com/v2/blog/%s/%s' % (
177+
blog_name, 'likes' if options.likes else 'posts'
178+
)
177179

178180

179181
def set_period():
@@ -252,6 +254,7 @@ def save_style():
252254
img { max-width: 720px; }
253255
blockquote { margin-left: 0; border-left: 8px #999 solid; padding: 0 24px; }
254256
.archive h1, .subtitle, article { padding-bottom: 0.75em; border-bottom: 1px #ccc dotted; }
257+
article[class^="liked-"] { background-color: #f0f0f8; }
255258
.post a.llink { display: none; }
256259
header a, footer a { text-decoration: none; }
257260
footer, article footer a { font-size: small; color: #999; }
@@ -473,18 +476,21 @@ def backup(self, account):
473476

474477
# collect all the meta information
475478
resp = soup['response']
476-
blog = resp['blog']
477-
try:
478-
self.title = escape(blog['title'])
479-
except KeyError:
480-
self.title = account
481-
self.subtitle = blog['description']
479+
if options.likes:
480+
_get_content = lambda soup: soup['response']['liked_posts']
481+
blog = {}
482+
last_post = resp['liked_count']
483+
else:
484+
_get_content = lambda soup: soup['response']['posts']
485+
blog = resp['blog']
486+
last_post = blog['posts']
487+
self.title = escape(blog.get('title', account))
488+
self.subtitle = blog.get('description', '')
482489

483490
# use the meta information to create a HTML header
484491
TumblrPost.post_header = self.header(body_class='post')
485492

486493
# find the post number limit to back up
487-
last_post = blog['posts']
488494
if options.count:
489495
last_post = min(last_post, options.count + options.skip)
490496

@@ -534,7 +540,7 @@ def _backup(posts):
534540
self.errors = True
535541
continue
536542

537-
posts = soup['response']['posts']
543+
posts = _get_content(soup)
538544
# posts can be empty if we don't backup reblogged posts
539545
if not posts or not _backup(posts):
540546
break
@@ -828,7 +834,8 @@ def download_media(self, url, filename):
828834

829835
def get_post(self):
830836
"""returns this post in HTML"""
831-
post = self.post_header + u'<article class=%s id=p-%s>\n' % (self.typ, self.ident)
837+
typ = ('liked-' if options.likes else '') + self.typ
838+
post = self.post_header + u'<article class=%s id=p-%s>\n' % (typ, self.ident)
832839
post += u'<header>\n<p><time datetime=%s>%s</time>\n' % (self.isodate, strftime('%x %X', self.tm))
833840
post += u'<a class=llink href=%s%s/%s>¶</a>\n' % (save_dir, post_dir, self.llink)
834841
post += u'<a href=%s>●</a></header>\n' % self.shorturl
@@ -988,6 +995,9 @@ def request_callback(option, opt, value, parser):
988995
parser.add_option('-i', '--incremental', action='store_true',
989996
help="incremental backup mode"
990997
)
998+
parser.add_option('-l', '--likes', action='store_true',
999+
dest='likes', help="save a blog's likes, not its posts"
1000+
)
9911001
parser.add_option('-k', '--skip-images', action='store_false', default=True,
9921002
dest='save_images', help="do not save images; link to Tumblr instead"
9931003
)

0 commit comments

Comments
 (0)