mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow.git
synced 2024-11-10 17:17:40 +08:00
⚡ 在请求中使用异步代替同步防止进程阻塞,优化部分代码样式
This commit is contained in:
parent
b5525ce662
commit
fc5931bf33
3
.gitignore
vendored
3
.gitignore
vendored
@ -122,13 +122,14 @@ celerybeat.pid
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.env.prod
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
.idea
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
|
@ -25,7 +25,7 @@ __plugin_meta__ = PluginMetadata(
|
||||
config=ConfigModel,
|
||||
homepage="https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow",
|
||||
supported_adapters=inherit_supported_adapters("nonebot_plugin_alconna"),
|
||||
extra={"License":"MIT","Author":"Asankilp"}
|
||||
extra={"License":"MIT","Author":"Swankily"}
|
||||
)
|
||||
driver = get_driver()
|
||||
|
||||
|
@ -1,39 +1,54 @@
|
||||
import json
|
||||
import requests
|
||||
from typing import Dict
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from .models import *
|
||||
from .util import *
|
||||
|
||||
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():
|
||||
'''
|
||||
|
||||
|
||||
async def get_cities_data() -> CityResp:
|
||||
"""
|
||||
返回支持的城市数据
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
async with session.get(CITY_API_ROOT, headers=HEADERS) as resp:
|
||||
regions_data = await resp.json()
|
||||
return CityResp.model_validate(regions_data)
|
||||
|
||||
|
||||
async def get_regions_dict() -> Dict[str, int]:
|
||||
"""
|
||||
返回支持的地区,键名为地区名,键值为地区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):
|
||||
'''
|
||||
"""
|
||||
data = {}
|
||||
city_data = await get_cities_data()
|
||||
for i in city_data.data.list:
|
||||
for j in i.city_list:
|
||||
data.update({
|
||||
j.name: j.id
|
||||
})
|
||||
data.update({
|
||||
"全国": -1,
|
||||
"海外": 900000
|
||||
})
|
||||
return data
|
||||
|
||||
|
||||
async def get_shows_data(region_id: int, page=1, pagesize=20):
|
||||
"""
|
||||
返回举办中/即将举办/取消举办的展览数据
|
||||
Args:
|
||||
region_id: 地区id
|
||||
page: 页码
|
||||
pagesize: 一页最大条目数,最大20
|
||||
'''
|
||||
"""
|
||||
param = {
|
||||
"version" : 133,
|
||||
"area" : region_id,
|
||||
@ -43,8 +58,12 @@ def get_shows_data(region_id: int, page=1, pagesize=20):
|
||||
"p_type" : "展览",
|
||||
"style" : 1
|
||||
}
|
||||
shows_data = json.loads(requests.get(SHOWS_API_ROOT, headers=HEADERS,params=param).text)
|
||||
async with ClientSession() as session:
|
||||
async with session.get(SHOWS_API_ROOT, headers=HEADERS, params=param) as resp:
|
||||
shows_data = await resp.json()
|
||||
return shows_data
|
||||
|
||||
|
||||
def process_shows_data_to_text(shows_data: dict):
|
||||
showlist = []
|
||||
data = shows_data["data"]
|
||||
@ -66,6 +85,7 @@ def process_shows_data_to_text(shows_data: dict):
|
||||
showlist.append(text)
|
||||
return showlist
|
||||
|
||||
|
||||
def process_shows_data_to_template(shows_data: dict):
|
||||
showlist = []
|
||||
data = shows_data["data"]
|
||||
|
@ -1,3 +1,5 @@
|
||||
import traceback
|
||||
|
||||
from nonebot.typing import T_State
|
||||
from typing import Optional
|
||||
from .acgnapis import *
|
||||
@ -8,6 +10,7 @@ from arclet.alconna import Alconna, Args
|
||||
from .config import RES_PATH, TEMPLATE_NAME, config
|
||||
from .util import *
|
||||
from .__init__ import __plugin_meta__
|
||||
|
||||
showcmd = on_alconna(
|
||||
Alconna(
|
||||
"展览",
|
||||
@ -23,31 +26,31 @@ showcmd.shortcut(
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@showcmd.handle()
|
||||
async def find_show(
|
||||
state: T_State, region: Optional[str] = None, page: Optional[int] = None, date: Optional[str] = None,
|
||||
region: Optional[str] = None, page: Optional[int] = None, date: Optional[str] = None,
|
||||
):
|
||||
if not region: await UniMessage(__plugin_meta__.usage).send(); return
|
||||
if not page: page = 1
|
||||
if not date: date = ""
|
||||
regions_dict = get_regions_dict()
|
||||
regions_dict = await get_regions_dict()
|
||||
regionid = regions_dict.get(region, None)
|
||||
if regionid == None: await UniMessage("不支持此地区").send(); return
|
||||
# await showcmd.send("日期:"+ date)
|
||||
shows = get_shows_data(regionid,page=page,pagesize=config.acgnshow_pagesize)
|
||||
shows = await get_shows_data(regionid, page=page, pagesize=config.acgnshow_pagesize)
|
||||
# print(shows)
|
||||
try:
|
||||
showsdata = process_shows_data_to_template(shows)
|
||||
shows_data = process_shows_data_to_template(shows)
|
||||
template = {
|
||||
"shows": showsdata[0],
|
||||
"shows" : shows_data[0],
|
||||
"bgimage" : choose_random_bgimage(),
|
||||
"global_data": showsdata[1]
|
||||
"global_data": shows_data[1]
|
||||
}
|
||||
pic = await template_to_pic(RES_PATH, TEMPLATE_NAME, template)
|
||||
except:
|
||||
except Exception as e:
|
||||
await UniMessage("发生错误").send()
|
||||
traceback.print_exc()
|
||||
return
|
||||
# print(pic)
|
||||
# a = Image.open(io.BytesIO(pic))
|
||||
# a.save("template2pic.png", format="PNG")
|
||||
await UniMessage.image(pic).send()
|
||||
|
||||
await UniMessage.image(raw=pic).send()
|
||||
|
54
nonebot_plugin_acgnshow/models.py
Normal file
54
nonebot_plugin_acgnshow/models.py
Normal file
@ -0,0 +1,54 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
|
||||
@Time : 2024/8/15 下午5:50
|
||||
@Author : snowykami
|
||||
@Email : snowykami@outlook.com
|
||||
@File : models.py
|
||||
@Software: PyCharm
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class City(BaseModel):
|
||||
"""
|
||||
热门城市
|
||||
"""
|
||||
id: int
|
||||
type: int
|
||||
first_letter: str
|
||||
name: str
|
||||
fullname: str
|
||||
num: int
|
||||
parent_id: int
|
||||
booked: bool
|
||||
|
||||
|
||||
class CityDataList(BaseModel):
|
||||
"""
|
||||
城市首字母
|
||||
"""
|
||||
letter: str
|
||||
city_list: List[City]
|
||||
|
||||
|
||||
class CityData(BaseModel):
|
||||
"""
|
||||
城市数据
|
||||
"""
|
||||
hot: List[City]
|
||||
list: List[CityDataList]
|
||||
located_id: int
|
||||
|
||||
|
||||
class CityResp(BaseModel):
|
||||
"""
|
||||
城市数据
|
||||
"""
|
||||
errno: int
|
||||
errtag: int
|
||||
msg: str
|
||||
data: CityData
|
@ -8,11 +8,11 @@ authors = [
|
||||
{ name = "Asankilp", email = "asankilp@outlook.com" },
|
||||
]
|
||||
dependencies = [
|
||||
"nonebot2>=2.2.0",
|
||||
"nonebot2[fastapi, websockets]>=2.2.0",
|
||||
"nonebot-plugin-alconna>=0.48.0",
|
||||
"nonebot-plugin-htmlrender>=0.3.2",
|
||||
"jinja2>=3.1.4",
|
||||
"requests>=2.32.3"
|
||||
"aiohttp>=4.0.0a1",
|
||||
]
|
||||
license = { text = "MIT" }
|
||||
|
||||
@ -23,7 +23,8 @@ Homepage = "https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow"
|
||||
[tool.nonebot]
|
||||
plugins = ["nonebot_plugin_acgnshow"]
|
||||
adapters = [
|
||||
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" }
|
||||
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" },
|
||||
|
||||
]
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user