diff --git a/nonebot_plugin_acgnshow/__init__.py b/nonebot_plugin_acgnshow/__init__.py new file mode 100644 index 0000000..de87ee0 --- /dev/null +++ b/nonebot_plugin_acgnshow/__init__.py @@ -0,0 +1,22 @@ +from nonebot.plugin import PluginMetadata, inherit_supported_adapters, require +require("nonebot_plugin_htmlrender") +require("nonebot_plugin_alconna") +from .acgnshower import * +from nonebot import get_driver +from .config import ConfigModel +__author__ = "Asankilp" +__plugin_meta__ = PluginMetadata( + name="漫展/展览查询", + description="从哔哩哔哩会员购获取简易展览数据", + usage="application", + config=ConfigModel, + homepage="https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow", + supported_adapters=inherit_supported_adapters("nonebot_plugin_alconna") +) +driver = get_driver() + + +@driver.on_startup +async def _(): + pass + diff --git a/nonebot_plugin_acgnshow/acgnapis.py b/nonebot_plugin_acgnshow/acgnapis.py new file mode 100644 index 0000000..0b4dd7e --- /dev/null +++ b/nonebot_plugin_acgnshow/acgnapis.py @@ -0,0 +1,99 @@ +import json +import requests +from nonebot_plugin_htmlrender import template_to_pic +from jinja2 import Template +CITY_API_ROOT="https://show.bilibili.com/api/ticket/city/list?channel=3" +SHOWS_API_ROOT="https://show.bilibili.com/api/ticket/project/listV2" +HEADERS = { + "user-agent": "Mozilla/5.0 (Linux; Android 14; 114514YAJU Build/UKQ1.114514.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/125.0.6422.165 Mobile Safari/537.36 BiliApp/7810200 mobi_app/android isNotchWindow/0 NotchHeight=34 mallVersion/7810200 mVersion/242 disable_rcmd/0 7.81.0 os/android model/114514YAJU mobi_app/android build/7810200 channel/bilih5 innerVer/7810210 osVer/14 network/2" + } +def get_regions_data(): + ''' + 返回支持的地区数据 + ''' + regions_data = json.loads(requests.get(CITY_API_ROOT, headers=HEADERS).text) + return regions_data +def get_regions_dict(): + ''' + 返回支持的地区,键名为地区名,键值为地区id + ''' + dicts = {} + cityjson = get_regions_data() + for i in cityjson["data"]["list"]: + for j in i["city_list"]: + name = j["name"] + id = j["id"] + dicts.update({name: id}) + dicts.update({"全国": -1,"海外": 900000}) #添加api中未返回的结果 + #print(dicts) + return dicts +def get_shows_data(region_id: int, page=1, pagesize=20): + ''' + 返回举办中/即将举办/取消举办的展览数据 + Args: + region_id: 地区id + page: 页码 + pagesize: 一页最大条目数,最大20 + ''' + param = { + "version": 133, + "area": region_id, + "page": page, + "pagesize": pagesize, + "platform": "web", + "p_type": "展览", + "style": 1 + } + shows_data = json.loads(requests.get(SHOWS_API_ROOT, headers=HEADERS,params=param).text) + return shows_data +def process_shows_data_to_text(shows_data: dict): + showlist = [] + for i in shows_data["data"]["result"]: + name = i["project_name"] + venue_name = i["venue_name"] + project_id = i["project_id"] + sale_flag = i["sale_flag"] + start_time = i["start_time"] + end_time = i["end_time"] + price_low = i["price_low"] / 100 + price_high = i["price_high"] / 100 + district_name = i["district_name"] + text = f"名称:{name}\n举办地:{venue_name}\nid:{project_id}\nflag:{sale_flag}\n开始时间:{start_time}\n结束时间:{end_time}\n最低票价:{price_low}\n最高票价:{price_high}\n区名:{district_name}\n\n" + showlist.append(text) + return showlist +def read_template_file(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + return file.read() + +def process_shows_data_to_template(shows_data: dict): + showlist = [] + # show_template = read_template_file('/home/asankilp/LiteyukiBot/src/plugins/acgnshow/res/template.html') + for i in shows_data["data"]["result"]: + name = i["project_name"] + venue_name = i["venue_name"] + project_id = i["project_id"] + sale_flag = i["sale_flag"] + start_time = i["start_time"] + end_time = i["end_time"] + price_low = i["price_low"] / 100 + price_high = i["price_high"] / 100 + district_name = i["district_name"] + wish = i["wish"] + cover = "https:" + i["cover"] + if district_name == None : district_name = "" + dicts = {} + dicts.update({ + "name": name, + "location": district_name + venue_name, + "sale_flag": sale_flag, + "price": price_low, + "start_time": start_time, + "end_time": end_time, + "wish": wish, + "image_url": cover + }) + showlist.append(dicts) + return showlist + template = Template(show_template) + rendered = template.render(exhibitions=showlist) + return rendered \ No newline at end of file diff --git a/nonebot_plugin_acgnshow/acgnshower.py b/nonebot_plugin_acgnshow/acgnshower.py new file mode 100644 index 0000000..a5a261b --- /dev/null +++ b/nonebot_plugin_acgnshow/acgnshower.py @@ -0,0 +1,49 @@ +from nonebot import require, on_endswith +from nonebot.adapters import satori +from nonebot.adapters.onebot.v11 import MessageSegment +from nonebot.typing import T_State +from typing import Optional +from .acgnapis import * +from nonebot_plugin_htmlrender import text_to_pic, template_to_pic +from nonebot_plugin_alconna import on_alconna +from arclet.alconna import Alconna, Args +from .config import RES_PATH, TEMPLATE_NAME, config +from .util import * + +showcmd = on_alconna( + Alconna( + "展览", + Args["region?", str]["page?", int]["date?", str], + ) +) +showcmd.shortcut( + r"(?P.+?)展览\s*(?P\d+)?\s*(?P.+)?", + { + "prefix": True, + "command": "展览", + "args": ["{region}", "{page}", "{date}"], + } +) + +@showcmd.handle() +async def find_show( + state: T_State, region: Optional[str] = None, page: Optional[int] = None, date: Optional[str] = None, +): + if not region: await showcmd.finish("未指定地区") + if not page: page = 1 + if not date: date = "" + regions_dict = get_regions_dict() + regionid = regions_dict.get(region,None) + if regionid == None: await showcmd.finish("未找到此地区") ; return + #await showcmd.send("日期:"+ date) + shows = get_shows_data(regionid,page=page,pagesize=config.pagesize) + # print(shows) + template = { + "shows":process_shows_data_to_template(shows), + "bgimage": choose_random_bgimage() + } + pic = await template_to_pic(RES_PATH,TEMPLATE_NAME,template) + # print(pic) + # a = Image.open(io.BytesIO(pic)) + # a.save("template2pic.png", format="PNG") + await showcmd.finish(MessageSegment.image(pic)) \ No newline at end of file diff --git a/nonebot_plugin_acgnshow/config.py b/nonebot_plugin_acgnshow/config.py new file mode 100644 index 0000000..d6777dd --- /dev/null +++ b/nonebot_plugin_acgnshow/config.py @@ -0,0 +1,11 @@ +from pathlib import Path +from pydantic import BaseModel +from nonebot import get_plugin_config +RES_PATH = Path(__file__).parent / "res" +TEMPLATE_NAME = "template.html" +BGIMAGE_PATH = RES_PATH / "bgimage" + +class ConfigModel(BaseModel): + pagesize: int = 8 + +config: ConfigModel = get_plugin_config(ConfigModel) \ No newline at end of file diff --git a/nonebot_plugin_acgnshow/res/SourceHanSans.otf b/nonebot_plugin_acgnshow/res/SourceHanSans.otf new file mode 100644 index 0000000..c13789b Binary files /dev/null and b/nonebot_plugin_acgnshow/res/SourceHanSans.otf differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg1.webp b/nonebot_plugin_acgnshow/res/bgimage/bg1.webp new file mode 100644 index 0000000..6c3d925 Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg1.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg2.webp b/nonebot_plugin_acgnshow/res/bgimage/bg2.webp new file mode 100644 index 0000000..8edbe4b Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg2.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg3.webp b/nonebot_plugin_acgnshow/res/bgimage/bg3.webp new file mode 100644 index 0000000..c3330c9 Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg3.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg4.webp b/nonebot_plugin_acgnshow/res/bgimage/bg4.webp new file mode 100644 index 0000000..a3b098a Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg4.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg5.webp b/nonebot_plugin_acgnshow/res/bgimage/bg5.webp new file mode 100644 index 0000000..ab254e4 Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg5.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg6.webp b/nonebot_plugin_acgnshow/res/bgimage/bg6.webp new file mode 100644 index 0000000..035d5bf Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg6.webp differ diff --git a/nonebot_plugin_acgnshow/res/bgimage/bg7.webp b/nonebot_plugin_acgnshow/res/bgimage/bg7.webp new file mode 100644 index 0000000..a1754bb Binary files /dev/null and b/nonebot_plugin_acgnshow/res/bgimage/bg7.webp differ diff --git a/nonebot_plugin_acgnshow/res/template.html b/nonebot_plugin_acgnshow/res/template.html new file mode 100644 index 0000000..2ae955f --- /dev/null +++ b/nonebot_plugin_acgnshow/res/template.html @@ -0,0 +1,141 @@ + + + + + Show Information + + + +
+
展览信息
+ {% for show in shows %} +
+
+ Image +
+
+
+
{{ show.name }}
+
+
举办地点:{{ show.location }}
+
+
+
{{ show.sale_flag }}
+
¥{{ show.price }}起
+
{{ show.wish }}人想去
+
开始时间:{{ show.start_time }}
+
结束时间:{{ show.end_time }}
+
+
+ {% endfor %} +
+ Designed by Asankilp? +
+
+ + diff --git a/nonebot_plugin_acgnshow/typing.py b/nonebot_plugin_acgnshow/typing.py new file mode 100644 index 0000000..29da851 --- /dev/null +++ b/nonebot_plugin_acgnshow/typing.py @@ -0,0 +1,3 @@ +from nonebot.adapters.onebot import v11, v12 +from nonebot.adapters import satori +T_MessageEvent = v11.MessageEvent | v12.MessageEvent | satori.MessageEvent diff --git a/nonebot_plugin_acgnshow/util.py b/nonebot_plugin_acgnshow/util.py new file mode 100644 index 0000000..33a78ac --- /dev/null +++ b/nonebot_plugin_acgnshow/util.py @@ -0,0 +1,6 @@ +from .config import BGIMAGE_PATH +import random +from pathlib import Path +def choose_random_bgimage(): + randomfile = random.choice(list(BGIMAGE_PATH.iterdir())) + return str(randomfile) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d99d494 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,43 @@ +[project] +name = "nonebot-plugin-acgnshow" +version = "0.1.0" +description = "Nonebot2插件,从哔哩哔哩会员购获取简易展览数据" +readme = "README.md" +requires-python = "<4.0,>=3.8" +authors = [ + {name = "Asankilp", email = "asankilp@outlook.com"}, +] +dependencies = [ + "nonebot2>=2.2.0", + "nonebot-plugin-alconna>=0.48.0", + "nonebot-plugin-htmlrender>=0.3.2", + "jinja2>=3.1.4", + + +] +license = {text = "MIT"} + +[tool.nonebot] +plugins = ["nonebot_plugin_acgnshow"] +adapters = [ + { name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" } +] + +[tool.poetry.dependencies] +python = "^3.8" +nonebot2 = { version = "^2.2.0", extras = ["fastapi"] } +nonebot-plugin-alconna = ">=0.38.0,<1.0.0" + + +[tool.pdm] +distribution = true + +[tool.pdm.version] +source = "file" +path = "nonebot_plugin_acgnshow/__init__.py" + +[tool.pdm.build] +includes = [] +[build-system] +requires = ["pdm-backend"] +build-backend = "pdm.backend" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29