mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow.git
synced 2025-09-05 19:56:25 +00:00
⚡ 在请求中使用异步代替同步防止进程阻塞,优化部分代码样式
This commit is contained in:
@ -1,50 +1,69 @@
|
||||
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"
|
||||
|
||||
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():
|
||||
'''
|
||||
"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"
|
||||
}
|
||||
|
||||
|
||||
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,
|
||||
"page": page,
|
||||
"pagesize": pagesize,
|
||||
"platform": "web",
|
||||
"p_type": "展览",
|
||||
"style": 1
|
||||
"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)
|
||||
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"]
|
||||
@ -55,7 +74,7 @@ def process_shows_data_to_text(shows_data: dict):
|
||||
venue_name = i["venue_name"]
|
||||
project_id = i["project_id"]
|
||||
sale_flag = i["sale_flag"]
|
||||
#start_time = i["start_time"]
|
||||
# start_time = i["start_time"]
|
||||
start_unix = i["start_unix"]
|
||||
start_time = convert_timestamp(start_unix)
|
||||
end_time = i["end_time"]
|
||||
@ -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"]
|
||||
@ -79,7 +99,7 @@ def process_shows_data_to_template(shows_data: dict):
|
||||
venue_name = i["venue_name"]
|
||||
project_id = i["project_id"]
|
||||
sale_flag = i["sale_flag"]
|
||||
#start_time = i["start_time"]
|
||||
# start_time = i["start_time"]
|
||||
start_unix = i["start_unix"]
|
||||
start_time = convert_timestamp(start_unix)
|
||||
end_time = i["end_time"]
|
||||
@ -88,30 +108,30 @@ def process_shows_data_to_template(shows_data: dict):
|
||||
district_name = i["district_name"]
|
||||
wish = i["wish"]
|
||||
cover = "https:" + i["cover"]
|
||||
if district_name == None : district_name = ""
|
||||
if district_name == None: district_name = ""
|
||||
guests_list = i["guests"]
|
||||
guests = ""
|
||||
if guests_list != None:
|
||||
for n in guests_list:
|
||||
guests += n["name"] + ","
|
||||
item_dict = {
|
||||
"name": name,
|
||||
"location": district_name + venue_name,
|
||||
"sale_flag": sale_flag,
|
||||
"id": project_id,
|
||||
"price": price_low,
|
||||
"start_time": start_time,
|
||||
"end_time": end_time,
|
||||
"wish": wish,
|
||||
"image_url": cover,
|
||||
"guests": guests,
|
||||
"page": page,
|
||||
"total_pages": total_pages
|
||||
}
|
||||
"name" : name,
|
||||
"location" : district_name + venue_name,
|
||||
"sale_flag" : sale_flag,
|
||||
"id" : project_id,
|
||||
"price" : price_low,
|
||||
"start_time" : start_time,
|
||||
"end_time" : end_time,
|
||||
"wish" : wish,
|
||||
"image_url" : cover,
|
||||
"guests" : guests,
|
||||
"page" : page,
|
||||
"total_pages": total_pages
|
||||
}
|
||||
showlist.append(item_dict)
|
||||
global_data_dict = {
|
||||
"page": page,
|
||||
"total_pages": total_pages,
|
||||
"total_results": total_results
|
||||
"page" : page,
|
||||
"total_pages" : total_pages,
|
||||
"total_results": total_results
|
||||
}
|
||||
return [showlist, global_data_dict]
|
||||
|
Reference in New Issue
Block a user