From b3df5a5f07aec81cbe5d69e9e30ccd0c57819f92 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Tue, 8 May 2018 17:39:00 +0200 Subject: [PATCH] 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. --- instaloader/instaloader.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/instaloader/instaloader.py b/instaloader/instaloader.py index 0e14bf0..3909bfa 100644 --- a/instaloader/instaloader.py +++ b/instaloader/instaloader.py @@ -178,27 +178,28 @@ class Instaloader: def update_comments(self, filename: str, post: Post) -> None: def _postcomment_asdict(comment): 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, 'owner': comment.owner._asdict()} filename += '_comments.json' try: - comments = json.load(open(filename)) - except FileNotFoundError: + with open(filename) as fp: + comments = json.load(fp) + except (FileNotFoundError, json.decoder.JSONDecodeError): comments = list() comments.extend(_postcomment_asdict(comment) for comment in post.get_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: - 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)) self.context.log('comments', end=' ', flush=True)