Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed Jul 27, 2023
1 parent 8ef44d8 commit b54e212
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 11 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div align="center">

<a href="https://v2.nonebot.dev/store">
<img src="https://raw.githubusercontent.com/A-kirami/nonebot-plugin-template/resources/nbp_logo.png" width="180" height="180" alt="NoneBotPluginLogo">
<img src="https://raw.githubusercontent.com/NoneMeme/NoneMeme/main/static/favicon.png" width="180" height="180" alt="NoneBotPluginLogo">
</a>

<p>
Expand Down Expand Up @@ -107,12 +107,7 @@ plugins = [

## ⚙️ 配置

在 nonebot2 项目的`.env`文件中添加下表中的必填配置

| 配置项 | 必填 | 默认值 | 说明 |
| :------: | :--: | :----: | :------: |
| 配置项 1 ||| 配置说明 |
| 配置项 2 ||| 配置说明 |
[config.py](./nonebot_plugin_nonememe/config.py)

## 🎉 使用

Expand Down
7 changes: 5 additions & 2 deletions nonebot_plugin_nonememe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from nonebot import require
from nonebot.plugin import PluginMetadata

from . import __main__ as __main__
from .config import ConfigModel
require("nonebot_plugin_saa")

from . import __main__ as __main__ # noqa: E402
from .config import ConfigModel # noqa: E402

__version__ = "0.1.0"
__plugin_meta__ = PluginMetadata(
Expand Down
67 changes: 67 additions & 0 deletions nonebot_plugin_nonememe/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import random
from typing import List, NoReturn

from nonebot import on_command
from nonebot.adapters import Message
from nonebot.internal.adapter import Event
from nonebot.matcher import Matcher
from nonebot.params import CommandArg
from nonebot.typing import T_State
from nonebot_plugin_saa import Image, MessageFactory, Text

from .config import config
from .data_source import MemeItem, fetch_meme, meme_list, search_meme_items


async def finish_with_meme(meme_item: MemeItem) -> NoReturn:
image_bytes = await fetch_meme(meme_item.path)
await MessageFactory(
[
Text(f"# {meme_item.name}"),
Image(image_bytes),
],
).finish()


cmd_meme = on_command("nonememe", aliases={"nb草图", "nb梗图"})


@cmd_meme.handle()
async def _(matcher: Matcher, state: T_State, arg_msg: Message = CommandArg()):
arg = arg_msg.extract_plain_text().strip()
if not arg:
await finish_with_meme(random.choice(meme_list))

use_regex = arg.startswith("/") and arg.endswith("/")
if use_regex:
arg = arg[1:-1]

searched = search_meme_items(arg, use_regex=use_regex)
if not searched:
await matcher.finish("没有找到相关 NoneMeme")
if len(searched) == 1:
await finish_with_meme(searched[0])

over_length = len(searched) > config.nonememe_search_limit
if over_length:
searched = searched[: config.nonememe_search_limit]
state["items"] = searched

list_text = "\n".join(f"{i }. {item.name}" for i, item in enumerate(searched, 1))
over_len_tip = (
f"\nTip:搜索到的结果过多,仅显示前 {config.nonememe_search_limit} 条" if over_length else ""
)
await matcher.pause(f"找到多张图片,请发送序号选择:\n{list_text}{over_len_tip}")


@cmd_meme.handle()
async def _(matcher: Matcher, state: T_State, event: Event):
arg = event.get_plaintext().strip()
if arg in ("0", "q", "quit", "exit", "退出"):
await matcher.finish("已退出选择")

searched: List[MemeItem] = state["items"]
if not (arg.isdigit() and (0 <= (index := (int(arg) - 1)) < len(searched))):
await matcher.reject("请输入正确的结果序号,退出选择请发送 `0`")

await finish_with_meme(searched[index])
9 changes: 8 additions & 1 deletion nonebot_plugin_nonememe/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from typing import Optional

from nonebot import get_driver
from pydantic import BaseModel


class ConfigModel(BaseModel):
pass
nonememe_proxy: Optional[str] = None
nonememe_repo_prefix: str = (
"https://raw.githubusercontent.com/NoneMeme/NoneMeme/main"
)

nonememe_search_limit: int = 5


config: ConfigModel = ConfigModel.parse_obj(get_driver().config.dict())
71 changes: 71 additions & 0 deletions nonebot_plugin_nonememe/data_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import re
import urllib.parse
from dataclasses import dataclass
from pathlib import Path
from typing import List, cast

import json5
from httpx import AsyncClient
from nonebot import get_driver, logger

from .config import config


@dataclass
class MemeItem:
name: str
path: str


meme_list: List[MemeItem] = []


def search_meme_items(
keyword: str,
use_regex: bool = False, # noqa: FBT001
) -> List[MemeItem]:
if use_regex:
pattern = re.compile(keyword, re.I)
return [item for item in meme_list if pattern.search(item.name)]

kws = keyword.lower().split()
matches = [
(s, x)
for x in meme_list
if (s := len([kw for kw in kws if kw in x.name.lower()]))
]
return [x[1] for x in sorted(matches, key=lambda x: x[0], reverse=True)]


async def fetch_meme(path: str) -> bytes:
async with AsyncClient(proxies=config.nonememe_proxy) as cli:
resp = await cli.get(f"{config.nonememe_repo_prefix}/{path}")
return resp.content


def build_meme_item(meme_path: str) -> MemeItem:
name = Path(urllib.parse.unquote(meme_path)).stem
return MemeItem(name=name, path=meme_path)


async def fetch_meme_list() -> List[MemeItem]:
async with AsyncClient(proxies=config.nonememe_proxy) as cli:
resp = await cli.get(f"{config.nonememe_repo_prefix}/static/scripts/config.js")
text = resp.text
text = text[text.find("{") : text.rfind("}") + 1]

items: List[str] = cast(dict, json5.loads(text))["items"]
return [build_meme_item(item) for item in items]


async def init_meme_list():
meme_list.clear()
meme_list.extend(await fetch_meme_list())
logger.opt(colors=True).success(
f"Succeed to init meme list, Loaded <y>{len(meme_list)}</y> memes",
)
print(meme_list)


driver = get_driver()
driver.on_startup(init_meme_list)
11 changes: 10 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies = [
"pydantic>=1.10.4,<2",
"nonebot-plugin-send-anything-anywhere>=0.2.7",
"httpx>=0.24.1",
"json5>=0.9.14",
]
requires-python = ">=3.8,<4.0"
readme = "README.md"
Expand Down

0 comments on commit b54e212

Please sign in to comment.