From d8ba30b91b16cc110d3b6d1ff0366780750fee01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Koch-Kramer?= Date: Mon, 13 Jan 2020 19:56:50 +0100 Subject: [PATCH] Speed up accessing property `comments` of `Post` This has been asked for in #436. --- instaloader/structures.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/instaloader/structures.py b/instaloader/structures.py index 824cabf..f97aa5e 100644 --- a/instaloader/structures.py +++ b/instaloader/structures.py @@ -325,6 +325,11 @@ class Post: @property def comments(self) -> int: """Comment count including answers""" + # If the count is already present in `self._node`, do not use `self._field` which could trigger fetching the + # full metadata dict. + comments = self._node.get('edge_media_to_comment') + if comments and 'count' in comments: + return comments['count'] try: return self._field('edge_media_to_parent_comment', 'count') except KeyError: