Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patreon] Post comments support #5280

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions gallery_dl/extractor/patreon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"""
Expand Down Expand Up @@ -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,
Expand Down