Skip to content

Commit

Permalink
Updating to meet standards
Browse files Browse the repository at this point in the history
Fixes
1. Reset formatting for unnecessary line changes
2. Removed Type Hinting
3.Replaced f-string with "".format

Updates
Renamed function creator_posts_tags -> creator_tagged_posts for clarity of what it does (get posts tags vs get tagged posts)
  • Loading branch information
BishopRed90 committed Jan 15, 2025
1 parent 1b54629 commit 293b594
Showing 1 changed file with 38 additions and 42 deletions.
80 changes: 38 additions & 42 deletions gallery_dl/extractor/kemonoparty.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ def _init(self):

self._prepare_ddosguard_cookies()
self._find_inline = re.compile(
r'src="(?:https?://(?:kemono|coomer)\.(?:su|party))?('
r'/inline/[^"]+'
r'|/[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{64}\.[^"]+)').findall
r'src="(?:https?://(?:kemono|coomer)\.(?:su|party))?(/inline/[^"]+'
r'|/[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{64}\.[^"]+)').findall
self._json_dumps = json.JSONEncoder(
ensure_ascii=False, check_circular=False,
sort_keys=True, separators=(",", ":")).encode
ensure_ascii=False, check_circular=False,
sort_keys=True, separators=(",", ":")).encode

def items(self):
_creator_info: dict = {}
creator_info = {}

find_hash = re.compile(HASH_PATTERN).match
generators = self._build_file_generators(self.config("files"))
Expand All @@ -76,33 +75,32 @@ def items(self):

for post in posts:
headers["Referer"] = "{}/{}/user/{}/post/{}".format(
self.root, post["service"], post["user"], post["id"])
self.root, post["service"], post["user"], post["id"])
post["_http_headers"] = headers
post["date"] = self._parse_datetime(
post.get("published") or post.get("added") or "")
post.get("published") or post.get("added") or "")
service = post["service"]
creator_id = post["user"]

if self.config("metadata"):
if _creator_info.get(f"{service}_{creator_id}") is None:
_creator = self.api.creator_profile(service, creator_id)
_creator_info[f"{service}_{creator_id}"] = _creator
creator_key = "{}_{}".format(service, creator_id)
if creator_info.get(creator_key) is None:
creator = self.api.creator_profile(service, creator_id)
creator_info[creator_key] = creator

post["user_profile"] = _creator_info[f"{service}_{creator_id}"]
post["username"] = _creator_info[f"{service}_{creator_id}"]['name']
else:
post["user_profile"] = post["username"] = post["user"]
post["user_profile"] = creator_info[creator_key]
post["username"] = creator_info[creator_key]['name']

if comments:
try:
post["comments"] = self.api.creator_post_comments(
service, creator_id, post["id"])
service, creator_id, post["id"])
except exception.HttpError:
post["comments"] = ()
if dms is not None:
if dms is True:
dms = self.api.creator_dms(
post["service"], post["user"])
post["service"], post["user"])
try:
dms = dms["props"]["dms"]
except Exception:
Expand All @@ -111,7 +109,7 @@ def items(self):
if announcements is not None:
if announcements is True:
announcements = self.api.creator_announcements(
post["service"], post["user"])
post["service"], post["user"])
post["announcements"] = announcements

files = []
Expand Down Expand Up @@ -160,9 +158,9 @@ def login(self):
username, password = self._get_auth_info()
if username:
self.cookies_update(self._login_impl(
(username, self.cookies_domain), password))
(username, self.cookies_domain), password))

@cache(maxage=3650 * 86400, keyarg=1)
@cache(maxage=3650*86400, keyarg=1)
def _login_impl(self, username, password):
username = username[0]
self.log.info("Logging in as %s", username)
Expand Down Expand Up @@ -215,14 +213,14 @@ def _parse_datetime(self, date_string):

def _revisions(self, posts):
return itertools.chain.from_iterable(
self._revisions_post(post) for post in posts)
self._revisions_post(post) for post in posts)

def _revisions_post(self, post):
post["revision_id"] = 0

try:
revs = self.api.creator_post_revisions(
post["service"], post["user"], post["id"])
post["service"], post["user"], post["id"])
except exception.HttpError:
post["revision_hash"] = self._revision_hash(post)
post["revision_index"] = 1
Expand Down Expand Up @@ -301,11 +299,11 @@ def posts(self):
_, _, service, creator_id, query = self.groups
params = text.parse_query(query)
if params.get("tag"):
return self.api.creator_posts_tags(
service, creator_id, params.get("tag"), params.get("o"))
return self.api.creator_tagged_posts(
service, creator_id, params.get("tag"), params.get("o"))
else:
return self.api.creator_posts(
service, creator_id, params.get("o"), params.get("q"))
service, creator_id, params.get("o"), params.get("q"))


class KemonopartyPostsExtractor(KemonopartyExtractor):
Expand All @@ -317,7 +315,7 @@ class KemonopartyPostsExtractor(KemonopartyExtractor):
def posts(self):
params = text.parse_query(self.groups[4])
return self.api.posts(
params.get("o"), params.get("q"), params.get("tag"))
params.get("o"), params.get("q"), params.get("tag"))


class KemonopartyPostExtractor(KemonopartyExtractor):
Expand Down Expand Up @@ -357,7 +355,7 @@ class KemonopartyDiscordExtractor(KemonopartyExtractor):
filename_fmt = "{id}_{num:>02}_{filename}.{extension}"
archive_fmt = "discord_{server}_{id}_{num}"
pattern = (BASE_PATTERN + r"/discord/server/(\d+)"
r"(?:/(?:channel/)?(\d+)(?:#(.+))?|#(.+))")
r"(?:/(?:channel/)?(\d+)(?:#(.+))?|#(.+))")
example = "https://kemono.su/discord/server/12345/12345"

def items(self):
Expand All @@ -382,8 +380,8 @@ def items(self):
channel_name = ""

find_inline = re.compile(
r"https?://(?:cdn\.discordapp.com|media\.discordapp\.net)"
r"(/[A-Za-z0-9-._~:/?#\[\]@!$&'()*+,;%=]+)").findall
r"https?://(?:cdn\.discordapp.com|media\.discordapp\.net)"
r"(/[A-Za-z0-9-._~:/?#\[\]@!$&'()*+,;%=]+)").findall
find_hash = re.compile(HASH_PATTERN).match

posts = self.api.discord_channel(channel_id)
Expand All @@ -401,8 +399,7 @@ def items(self):
append(attachment)
for path in find_inline(post["content"] or ""):
append({"path": "https://cdn.discordapp.com" + path,
"name": path, "type": "inline", "hash": ""
})
"name": path, "type": "inline", "hash": ""})

post["channel_name"] = channel_name
post["date"] = self._parse_datetime(post["published"])
Expand Down Expand Up @@ -434,7 +431,7 @@ def items(self):
server_id = self.groups[2]
for channel in self.api.discord_server(server_id):
url = "{}/discord/server/{}/{}#{}".format(
self.root, server_id, channel["id"], channel["name"])
self.root, server_id, channel["id"], channel["name"])
channel["_extractor"] = KemonopartyDiscordExtractor
yield Message.Queue, url, channel

Expand All @@ -461,18 +458,18 @@ def items(self):
if not sort:
sort = "updated"
users.sort(key=lambda x: x[sort] or util.NONE,
reverse=(order == "desc"))
reverse=(order == "desc"))

for user in users:
service = user["service"]
if service == "discord":
user["_extractor"] = KemonopartyDiscordServerExtractor
url = "{}/discord/server/{}".format(
self.root, user["id"])
self.root, user["id"])
else:
user["_extractor"] = KemonopartyUserExtractor
url = "{}/{}/user/{}".format(
self.root, service, user["id"])
self.root, service, user["id"])
yield Message.Queue, url, user

elif type == "post":
Expand All @@ -481,12 +478,12 @@ def items(self):
if not sort:
sort = "faved_seq"
posts.sort(key=lambda x: x[sort] or util.NONE,
reverse=(order == "desc"))
reverse=(order == "desc"))

for post in posts:
post["_extractor"] = KemonopartyPostExtractor
url = "{}/{}/user/{}/post/{}".format(
self.root, post["service"], post["user"], post["id"])
self.root, post["service"], post["user"], post["id"])
yield Message.Queue, url, post


Expand All @@ -510,7 +507,7 @@ def creator_posts(self, service, creator_id, offset=0, query=None):
params = {"q": query, "o": offset}
return self._pagination(endpoint, params, 50)

def creator_posts_tags(self, service, creator_id, tags, offset=0):
def creator_tagged_posts(self, service, creator_id, tags, offset=0):
endpoint = "/{}/user/{}/posts-legacy".format(service, creator_id)
params = {"o": offset, "tag": tags}
return self._pagination(endpoint, params, 50, "results")
Expand All @@ -533,12 +530,12 @@ def creator_post(self, service, creator_id, post_id):

def creator_post_comments(self, service, creator_id, post_id):
endpoint = "/{}/user/{}/post/{}/comments".format(
service, creator_id, post_id)
service, creator_id, post_id)
return self._call(endpoint)

def creator_post_revisions(self, service, creator_id, post_id):
endpoint = "/{}/user/{}/post/{}/revisions".format(
service, creator_id, post_id)
service, creator_id, post_id)
return self._call(endpoint)

def creator_profile(self, service, creator_id):
Expand Down Expand Up @@ -571,8 +568,7 @@ def _call(self, endpoint, params=None):
response = self.extractor.request(url, params=params)
return response.json()

def _pagination(self, endpoint, params: dict, batch: int = 50, key: str =
None):
def _pagination(self, endpoint, params, batch=50, key=None):
offset = text.parse_int(params.get("o"))
params["o"] = offset - offset % batch

Expand Down

0 comments on commit 293b594

Please sign in to comment.