Skip to content

Commit

Permalink
✨v0.2,不会接受字数大于5的展览查询,添加展览详细信息展示,返回详细错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Asankilp committed Aug 24, 2024
1 parent 80a9b4d commit d5eeea6
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ _✨ 从哔哩哔哩会员购获取展览简易信息 ✨_
| 配置项 | 必填 | 默认值 | 说明 |
| :---------------: | :--: | :----: | :----------------------------------------------------------: |
| ACGNSHOW_PAGESIZE || 8 | 单个图片的条目数,最大为 20,条目数过大可能导致 Bot 无法发送 |
| ACGNSHOW_BGIMAGE_PATH || 插件内置背景图 | 插件返回图片的背景图目录路径 |
5 changes: 5 additions & 0 deletions nonebot_plugin_acgnshow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
展览 <地区> [页码]
<地区>展览 [页码]
获取指定地区的展览列表
其中地区为省级行政区或地级行政区(不包含后缀)
(如北京,福建,平顶山,绍兴,香港...,或海外/全国)
展览详情 <ID>
其中ID为展览列表处返回的ID
示例:
展览 福建 2
福建展览 2
全国展览
海外展览
展览详情 86827
※数据来源于哔哩哔哩会员购,由于API返回结果与实际存在差异,数据可能不准确。"""
__author__ = "Asankilp"
Expand Down
87 changes: 67 additions & 20 deletions nonebot_plugin_acgnshow/acgnapis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

CITY_API_ROOT = "https://show.bilibili.com/api/ticket/city/list?channel=3"
SHOWS_API_ROOT = "https://show.bilibili.com/api/ticket/project/listV2"
SHOW_DETAILS_API_ROOT = "https://show.bilibili.com/api/ticket/project/getV2"
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"
}
Expand Down Expand Up @@ -57,28 +58,74 @@ async def get_shows_data(region_id: int, page=1, pagesize=20):
shows_data = await resp.json()
return shows_data

async def get_show_details(show_id: int):
param = {
"id": show_id,
"project_id": show_id,
"requestSource": "neul-next"
}
async with ClientSession() as session:
async with session.get(SHOW_DETAILS_API_ROOT, headers=HEADERS, params=param) as resp:
show_details_data = await resp.json()
return show_details_data

# def process_shows_data_to_text(shows_data: dict):
# showlist = []
# data = shows_data["data"]
# total_pages = data["numPages"]
# result = data["result"]
# for i in 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"]
# start_unix = i["start_unix"]
# start_time = convert_timestamp(start_unix)
# 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 process_show_details_data_to_template(show_details_data: dict):
data = show_details_data["data"]

banner_url = "https:"+data["banner"]
# banner_url = extract_banner_url(data["performance_image"])

# 提取事件基本信息
name = data["name"]
start_time = convert_timestamp(data["start_time"])
end_time = convert_timestamp(data["end_time"])

# 提取场馆信息
venue_name = data["venue_info"]["name"]
venue_detail = data["venue_info"]["address_detail"]

# 提取主办方信息
organizer = data["merchant"]["company"]

# 提取实名制,退票等信息
is_refund = data["is_refund"]
id_bind = data["id_bind"]
has_eticket = data["has_eticket"]

# 提取票务信息
ticket_info = []
for screen in data.get("screen_list", []):
for ticket in screen.get("ticket_list", []):
ticket_info.append({
"description": ticket.get("desc", ""),
"price": ticket.get("price", 0),
"sale_start": convert_timestamp(ticket.get("saleStart", 0)),
"sale_end": convert_timestamp(ticket.get("saleEnd", 0)),
"status": ticket.get("sale_flag", {}).get("display_name", "")
})
guests_list = data["guests"]
if guests_list != None:
guests = "、".join(n["name"] for n in guests_list)
else:
guests = ""

# 构建返回的字典
item_dict = {
"banner_url": banner_url,
"name": name,
"start_time": start_time,
"end_time": end_time,
"venue_name": venue_name,
"venue_detail": venue_detail,
"organizer": organizer,
"ticket_info": ticket_info,
"guests": guests,
"is_refund": is_refund,
"id_bind": id_bind,
"has_eticket": has_eticket
}

return item_dict

def process_shows_data_to_template(shows_data: dict):
showlist = []
Expand Down
43 changes: 39 additions & 4 deletions nonebot_plugin_acgnshow/acgnshower.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nonebot_plugin_alconna import on_alconna
from nonebot_plugin_alconna.uniseg import UniMessage
from arclet.alconna import Alconna, Args
from .config import RES_PATH, TEMPLATE_NAME, config
from .config import RES_PATH, LIST_TEMPLATE_NAME, DETAILS_TEMPLATE_NAME, config
from .util import *
from .__init__ import __plugin_meta__

Expand All @@ -25,17 +25,52 @@
"args": ["{region}", "{page}", "{date}"],
},
)
showcmd_details = on_alconna(
Alconna(
"展览详情",
Args["id?", int], # 这里定义了一个必需的 int 参数 "id"
)
)
showcmd_details.shortcut(
r"展览详情\s*(?P<id>\d+)", # 正则表达式匹配 "展览详情" 后跟一个整数 ID
{
"prefix": True,
"command": "展览详情",
"args": ["{id}"], # 将 ID 参数传递给命令
},
)

@showcmd_details.handle()
async def get_show_details_cmd(
id: Optional[int] = None
):
show_details = await get_show_details(id)
if show_details["errno"] != 0: await UniMessage("发生错误").send() ; return
try:
show_details_data = process_show_details_data_to_template(show_details)
print(show_details_data)
template = {
"show": show_details_data,
"bgimage": choose_random_bgimage(),
}
pic = await template_to_pic(str(RES_PATH), DETAILS_TEMPLATE_NAME, template)
except Exception as e:
await UniMessage(f"图片生成时产生错误:\n{str(e)}").send()
traceback.print_exc()
return
await UniMessage.image(raw=pic).send()

@showcmd.handle()
async def find_show(
async def find_shows_cmd(
region: Optional[str] = None,
page: Optional[int] = None,
date: Optional[str] = None,
):
if not region:
await UniMessage(__plugin_meta__.usage).send()
return
if len(region) > 5:
return
if not page:
page = 1
if not date:
Expand All @@ -55,9 +90,9 @@ async def find_show(
"bgimage": choose_random_bgimage(),
"global_data": shows_data[1],
}
pic = await template_to_pic(str(RES_PATH), TEMPLATE_NAME, template)
pic = await template_to_pic(str(RES_PATH), LIST_TEMPLATE_NAME, template)
except Exception as e:
await UniMessage("图片生成时产生未知错误").send()
await UniMessage(f"图片生成时产生错误:\n{str(e)}").send()
traceback.print_exc()
return

Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_acgnshow/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from nonebot import get_plugin_config

RES_PATH = Path(__file__).parent / "res"
TEMPLATE_NAME = "template.html"
LIST_TEMPLATE_NAME = "template.html"
DETAILS_TEMPLATE_NAME = "details.html"
BGIMAGE_PATH = RES_PATH / "bgimage"


Expand Down
Loading

0 comments on commit d5eeea6

Please sign in to comment.