Skip to content

Commit

Permalink
fixup! tumblr_backup: Use before parameter to implement --period
Browse files Browse the repository at this point in the history
Use this parameter when reading responses from --prev-archives
  • Loading branch information
cebtenzzre committed Oct 21, 2020
1 parent 593a2c1 commit 0157714
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions tumblr_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

if TYPE_CHECKING:
from queue import Queue
from typing import Any, Callable, DefaultDict, Dict, List, Optional, Set, Text, Tuple, Type
from typing import Any, Callable, DefaultDict, Dict, Iterable, List, Optional, Set, Text, Tuple, Type

JSONDict = Dict[str, Any]

Expand Down Expand Up @@ -91,10 +91,10 @@
pyjq = None

try:
from os import scandir # type: ignore[attr-defined]
from os import DirEntry, scandir # type: ignore[attr-defined]
except ImportError:
try:
from scandir import scandir # type: ignore[no-redef]
from scandir import DirEntry, scandir # type: ignore[no-redef]
except ImportError:
scandir = None # type: ignore[assignment,no-redef]

Expand Down Expand Up @@ -317,20 +317,25 @@ def apiparse(base, prev_resps, count, start=0, before=None):
# type: (...) -> Optional[JSONDict]
if prev_resps is not None:
# Reconstruct the API response
posts = []
post_respfiles = prev_resps[start:]
for prf in post_respfiles[:count]:
def read_post(prf):
with io.open(prf, encoding=FILE_ENCODING) as f:
try:
post = json.load(f)
except ValueError as e:
f.seek(0)
log('{}: {}\n{!r}\n'.format(e.__class__.__name__, e, f.read()))
return None
posts.append(post)
return {'posts': posts,
'post_respfiles': post_respfiles,
'blog': dict(posts[0]['blog'] if posts else {}, posts=len(prev_resps))}
return prf, post
posts = map(read_post, prev_resps) # type: Iterable[Tuple[DirEntry[str], JSONDict]]
if before is not None:
posts = itertools.dropwhile(
lambda pp: pp[1]['liked_timestamp' if options.likes else 'timestamp'] >= before,
posts,
)
posts = list(itertools.islice(posts, start, start + count))
return {'posts': [post for prf, post in posts],
'post_respfiles': [prf for prf, post in posts],
'blog': dict(posts[0][1]['blog'] if posts else {}, posts=len(prev_resps))}

params = {'api_key': API_KEY, 'limit': count, 'reblog_info': 'true'}
if before:
Expand Down

0 comments on commit 0157714

Please sign in to comment.