[kemonoparty:discord] update server & channel metadata (#7569)

to match the Discord Extractors' names

- rename
    server       -> server_id
    channel      -> channel_id
    channel_name -> channel
- add
    server
    channel_nsfw
    channel_type
    channel_topic
    parent_id
This commit is contained in:
Mike Fährmann
2025-05-24 17:57:12 +02:00
parent ed9c960bb9
commit 1f869c2786
2 changed files with 77 additions and 49 deletions

View File

@@ -10,7 +10,7 @@
from .common import Extractor, Message
from .. import text, util, exception
from ..cache import cache
from ..cache import cache, memcache
import itertools
import json
import re
@@ -381,34 +381,32 @@ class KemonopartyPostExtractor(KemonopartyExtractor):
class KemonopartyDiscordExtractor(KemonopartyExtractor):
"""Extractor for kemono.su discord servers"""
subcategory = "discord"
directory_fmt = ("{category}", "discord", "{server}",
"{channel_name|channel}")
directory_fmt = ("{category}", "discord",
"{server_id} {server}", "{channel_id} {channel}")
filename_fmt = "{id}_{num:>02}_{filename}.{extension}"
archive_fmt = "discord_{server}_{id}_{num}"
pattern = (BASE_PATTERN + r"/discord/server/(\d+)"
r"(?:/(?:channel/)?(\d+)(?:#(.+))?|#(.+))")
archive_fmt = "discord_{server_id}_{id}_{num}"
pattern = BASE_PATTERN + r"/discord/server/(\d+)[/#](?:channel/)?(\d+)"
example = "https://kemono.su/discord/server/12345/12345"
def items(self):
_, _, server_id, channel_id, channel_name, channel = self.groups
_, _, server_id, channel_id = self.groups
if channel_id is None:
if channel.isdecimal() and len(channel) >= 16:
key = "id"
else:
key = "name"
else:
key = "id"
channel = channel_id
try:
server, channels = discord_server_info(self, server_id)
channel = channels[channel_id]
except Exception:
raise exception.NotFoundError("channel")
if not channel_name or not channel_id:
for ch in self.api.discord_server(server_id):
if ch[key] == channel:
break
else:
raise exception.NotFoundError("channel")
channel_id = ch["id"]
channel_name = ch["name"]
data = {
"server" : server["name"],
"server_id" : server["id"],
"channel" : channel["name"],
"channel_id" : channel["id"],
"channel_nsfw" : channel["is_nsfw"],
"channel_type" : channel["type"],
"channel_topic": channel["topic"],
"parent_id" : channel["parent_channel_id"],
}
find_inline = re.compile(
r"https?://(?:cdn\.discordapp.com|media\.discordapp\.net)"
@@ -432,7 +430,7 @@ class KemonopartyDiscordExtractor(KemonopartyExtractor):
append({"path": "https://cdn.discordapp.com" + path,
"name": path, "type": "inline", "hash": ""})
post["channel_name"] = channel_name
post.update(data)
post["date"] = self._parse_datetime(post["published"])
post["count"] = len(files)
yield Message.Directory, post
@@ -460,11 +458,24 @@ class KemonopartyDiscordServerExtractor(KemonopartyExtractor):
def items(self):
server_id = self.groups[2]
for channel in self.api.discord_server(server_id):
server, channels = discord_server_info(self, server_id)
for channel in channels.values():
url = "{}/discord/server/{}/{}#{}".format(
self.root, server_id, channel["id"], channel["name"])
channel["_extractor"] = KemonopartyDiscordExtractor
yield Message.Queue, url, channel
yield Message.Queue, url, {
"server" : server,
"channel" : channel,
"_extractor": KemonopartyDiscordExtractor,
}
@memcache(keyarg=1)
def discord_server_info(extr, server_id):
server = extr.api.discord_server(server_id)
return server, {
channel["id"]: channel
for channel in server.pop("channels")
}
class KemonopartyFavoriteExtractor(KemonopartyExtractor):
@@ -590,10 +601,14 @@ class KemonoAPI():
endpoint = "/discord/channel/{}".format(channel_id)
return self._pagination(endpoint, {}, 150)
def discord_server(self, server_id):
def discord_channel_lookup(self, server_id):
endpoint = "/discord/channel/lookup/{}".format(server_id)
return self._call(endpoint)
def discord_server(self, server_id):
endpoint = "/discord/server/{}".format(server_id)
return self._call(endpoint)
def account_favorites(self, type):
endpoint = "/account/favorites"
params = {"type": type}