Skip to content

Commit

Permalink
tumblr_backup: Use before parameter to implement --period
Browse files Browse the repository at this point in the history
  • Loading branch information
cebtenzzre committed Sep 24, 2020
1 parent 375690c commit 482d357
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions tumblr_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ def mktime(tml):
tmt = tuple(tml) # type: Any
return time.mktime(tmt)

options.p_start = mktime(tm)
options.p_start = int(mktime(tm))
tm[i] += 1
options.p_stop = mktime(tm)
options.p_stop = int(mktime(tm))


def initial_apiparse(base, prev_archive):
Expand All @@ -298,7 +298,7 @@ def initial_apiparse(base, prev_archive):
return prev_resps, apiparse(base, prev_resps, 1)


def apiparse(base, prev_resps, count, start=0):
def apiparse(base, prev_resps, count, start=0, before=None):
# type: (...) -> Optional[JSONDict]
if prev_resps is not None:
# Reconstruct the API response
Expand All @@ -318,6 +318,8 @@ def apiparse(base, prev_resps, count, start=0):
'blog': dict(posts[0]['blog'] if posts else {}, posts=len(prev_resps))}

params = {'api_key': API_KEY, 'limit': count, 'reblog_info': 'true'}
if before:
params['before'] = before
if start > 0:
params['offset'] = start
url = base + '?' + urlencode(params)
Expand Down Expand Up @@ -779,7 +781,8 @@ def _backup(posts, post_respfiles):
return False
if options.period:
if post.date >= options.p_stop:
continue
raise RuntimeError('Found post with date ({}) older than before param ({})'.format(
post.date, options.p_stop))
if post.date < options.p_start:
return False
if options.request:
Expand Down Expand Up @@ -810,13 +813,14 @@ def _backup(posts, post_respfiles):
# Get the JSON entries from the API, which we can only do for MAX_POSTS posts at once.
# Posts "arrive" in reverse chronological order. Post #0 is the most recent one.
i = options.skip
before = options.p_stop if options.period else None
while True:
# find the upper bound
log.status('Getting {}posts {} to {} (of {} expected)\r'.format(
'liked ' if options.likes else '', i, i + MAX_POSTS - 1, count_estimate,
))

resp = apiparse(base, prev_resps, MAX_POSTS, i)
resp = apiparse(base, prev_resps, MAX_POSTS, i, before)
if resp is None:
self.errors = True
break
Expand Down

0 comments on commit 482d357

Please sign in to comment.