diff --git a/gallery_dl/extractor/patreon.py b/gallery_dl/extractor/patreon.py index 62d11f23e6..7b8c8625df 100644 --- a/gallery_dl/extractor/patreon.py +++ b/gallery_dl/extractor/patreon.py @@ -136,6 +136,8 @@ def _process(self, post, included): if attr.get("current_user_can_view", True): + self._process_post_comments(attr) + relationships = post["relationships"] attr["images"] = self._files(post, included, "images") attr["attachments"] = self._files(post, included, "attachments") @@ -156,6 +158,36 @@ def _process(self, post, included): return attr + def _process_post_comments(self, post): + headers = { + "Content-Type": "application/vnd.api+json", + } + + post["comments"] = [] + + comments_url = text.ensure_http_scheme( + PatreonExtractor._build_comments_url(post["id"])) + while comments_url: + comments = self.request(comments_url, headers=headers).json() + post["comments"].extend(comments["data"]) + comments_url = comments["links"].get("next") + + for comment in post['comments']: + self._process_comment_replies(comment) + + def _process_comment_replies(self, comment): + headers = { + "Content-Type": "application/vnd.api+json", + } + + replies_url = text.ensure_http_scheme( + PatreonExtractor._build_comment_replies_url(comment["id"])) + comments = self.request(replies_url, headers=headers).json() + comment["replies"] = comments["data"] + + for reply in comment['replies']: + self._process_comment_replies(reply) + @staticmethod def _transform(included): """Transform 'included' into an easier to handle format""" @@ -243,6 +275,20 @@ def _build_url(endpoint, query): "&json-api-version=1.0" ) + @staticmethod + def _build_comments_url(post_id): + return ( + "https://www.patreon.com/api/posts/" + str(post_id) + + "/comments" + ) + + @staticmethod + def _build_comment_replies_url(comment_id): + return ( + "https://www.patreon.com/api/comments/" + str(comment_id) + + "/replies" + ) + def _build_file_generators(self, filetypes): if filetypes is None: return (self._images, self._image_large,