Skip to content

Commit

Permalink
[kemonoparty] initial support for post revisions (#4498, #4597)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Oct 19, 2023
1 parent 174191c commit aaf5390
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
22 changes: 19 additions & 3 deletions gallery_dl/extractor/kemonoparty.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ def _discord_channels(self, server):
self.root, server)
return self.request(url).json()

@memcache(keyarg=1)
def _post_revisions(self, url):
return self.request(url + "/revisions").json()


def _validate(response):
return (response.headers["content-length"] != "9" or
Expand Down Expand Up @@ -248,19 +252,31 @@ def posts(self):
class KemonopartyPostExtractor(KemonopartyExtractor):
"""Extractor for a single kemono.party post"""
subcategory = "post"
pattern = USER_PATTERN + r"/post/([^/?#]+)"
pattern = USER_PATTERN + r"/post/([^/?#]+)(/revisions?(?:/(\d*))?)?"
example = "https://kemono.party/SERVICE/user/12345/post/12345"

def __init__(self, match):
_, _, service, user_id, post_id = match.groups()
_, _, service, user_id, post_id, self.revision, self.revision_id = \
match.groups()
self.subcategory = service
KemonopartyExtractor.__init__(self, match)
self.api_url = "{}/api/v1/{}/user/{}/post/{}".format(
self.root, service, user_id, post_id)
self.user_url = "{}/{}/user/{}".format(self.root, service, user_id)

def posts(self):
return (self.request(self.api_url).json(),)
if not self.revision:
return (self.request(self.api_url).json(),)

revs = self._post_revisions(self.api_url)
if not self.revision_id:
return revs

for rev in revs:
if str(rev["revision_id"]) == self.revision_id:
return (rev,)

raise exception.NotFoundError("revision")


class KemonopartyDiscordExtractor(KemonopartyExtractor):
Expand Down
32 changes: 32 additions & 0 deletions test/results/kemonoparty.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# published by the Free Software Foundation.

from gallery_dl.extractor import kemonoparty
from gallery_dl import exception


__tests__ = (
Expand Down Expand Up @@ -154,6 +155,37 @@
"#class" : kemonoparty.KemonopartyPostExtractor,
},

{
"#url" : "https://kemono.party/patreon/user/3161935/post/68231671/revision/134996",
"#comment" : "revisions (#4498)",
"#category": ("", "kemonoparty", "patreon"),
"#class" : kemonoparty.KemonopartyPostExtractor,
"#urls" : "https://kemono.party/data/88/52/88521f71822dfa2f42df3beba319ea4fceda2a2d6dc59da0276a75238f743f86.jpg",

"revision_id": 134996,
},

{
"#url" : "https://kemono.party/patreon/user/3161935/post/68231671/revisions",
"#comment" : "revisions (#4498)",
"#category": ("", "kemonoparty", "patreon"),
"#class" : kemonoparty.KemonopartyPostExtractor,
"#pattern" : r"https://kemono\.party/data/88/52/88521f71822dfa2f42df3beba319ea4fceda2a2d6dc59da0276a75238f743f86\.jpg",
"#count" : 9,
"#archive" : False,

"revision_id": range(134996, 3052965),
},


{
"#url" : "https://kemono.party/patreon/user/3161935/post/68231671/revision/12345",
"#comment" : "revisions (#4498)",
"#category": ("", "kemonoparty", "patreon"),
"#class" : kemonoparty.KemonopartyPostExtractor,
"#exception": exception.NotFoundError,
},

{
"#url" : "https://kemono.party/discord/server/488668827274444803#608504710906904576",
"#category": ("", "kemonoparty", "discord"),
Expand Down

0 comments on commit aaf5390

Please sign in to comment.