Fix bugs in Instaloader.update_comments()
- Cast id and created_at from JSON to int to be compatible with comments JSON files from Instaloader 3.3, - Do not fail if comment JSON files are empty, - Close file descriptors of comment JSON files when reading, - Do no create empty comment JSON files if generating content fails. Closes #114.
This commit is contained in:
parent
5204990db9
commit
b3df5a5f07
@ -178,27 +178,28 @@ class Instaloader:
|
|||||||
def update_comments(self, filename: str, post: Post) -> None:
|
def update_comments(self, filename: str, post: Post) -> None:
|
||||||
def _postcomment_asdict(comment):
|
def _postcomment_asdict(comment):
|
||||||
return {'id': comment.id,
|
return {'id': comment.id,
|
||||||
'created_at': comment.created_at_utc.replace(tzinfo=timezone.utc).timestamp(),
|
'created_at': int(comment.created_at_utc.replace(tzinfo=timezone.utc).timestamp()),
|
||||||
'text': comment.text,
|
'text': comment.text,
|
||||||
'owner': comment.owner._asdict()}
|
'owner': comment.owner._asdict()}
|
||||||
filename += '_comments.json'
|
filename += '_comments.json'
|
||||||
try:
|
try:
|
||||||
comments = json.load(open(filename))
|
with open(filename) as fp:
|
||||||
except FileNotFoundError:
|
comments = json.load(fp)
|
||||||
|
except (FileNotFoundError, json.decoder.JSONDecodeError):
|
||||||
comments = list()
|
comments = list()
|
||||||
comments.extend(_postcomment_asdict(comment) for comment in post.get_comments())
|
comments.extend(_postcomment_asdict(comment) for comment in post.get_comments())
|
||||||
if comments:
|
if comments:
|
||||||
|
comments_list = sorted(sorted(list(comments), key=lambda t: int(t['id'])),
|
||||||
|
key=lambda t: int(t['created_at']), reverse=True)
|
||||||
|
unique_comments_list = [comments_list[0]]
|
||||||
|
#for comment in comments_list:
|
||||||
|
# if unique_comments_list[-1]['id'] != comment['id']:
|
||||||
|
# unique_comments_list.append(comment)
|
||||||
|
#file.write(json.dumps(unique_comments_list, indent=4))
|
||||||
|
for x, y in zip(comments_list[:-1], comments_list[1:]):
|
||||||
|
if x['id'] != y['id']:
|
||||||
|
unique_comments_list.append(y)
|
||||||
with open(filename, 'w') as file:
|
with open(filename, 'w') as file:
|
||||||
comments_list = sorted(sorted(list(comments), key=lambda t: t['id']),
|
|
||||||
key=lambda t: t['created_at'], reverse=True)
|
|
||||||
unique_comments_list = [comments_list[0]]
|
|
||||||
#for comment in comments_list:
|
|
||||||
# if unique_comments_list[-1]['id'] != comment['id']:
|
|
||||||
# unique_comments_list.append(comment)
|
|
||||||
#file.write(json.dumps(unique_comments_list, indent=4))
|
|
||||||
for x, y in zip(comments_list[:-1], comments_list[1:]):
|
|
||||||
if x['id'] != y['id']:
|
|
||||||
unique_comments_list.append(y)
|
|
||||||
file.write(json.dumps(unique_comments_list, indent=4))
|
file.write(json.dumps(unique_comments_list, indent=4))
|
||||||
self.context.log('comments', end=' ', flush=True)
|
self.context.log('comments', end=' ', flush=True)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user