From 5e1305fed63d667d96e9c4bd2a026529421eec0a Mon Sep 17 00:00:00 2001 From: HornCopper Date: Tue, 20 Aug 2024 00:23:36 +0800 Subject: [PATCH] [feat]dilu_stastic rewrite --- src/plugins/jx3/dungeon/__init__.py | 2 +- src/plugins/jx3/horse/__init__.py | 14 ++- src/plugins/jx3/horse/dilu.py | 163 ++++++++++++++++++++++++++++ src/plugins/jx3/jx3.py | 1 - src/plugins/jx3/private/README.md | 5 - src/plugins/jx3/private/__init__.py | 37 ------- src/plugins/jx3/private/api.py | 92 ---------------- 7 files changed, 176 insertions(+), 138 deletions(-) create mode 100644 src/plugins/jx3/horse/dilu.py delete mode 100644 src/plugins/jx3/private/README.md delete mode 100644 src/plugins/jx3/private/__init__.py delete mode 100644 src/plugins/jx3/private/api.py diff --git a/src/plugins/jx3/dungeon/__init__.py b/src/plugins/jx3/dungeon/__init__.py index 0021ec5cb..464a2e435 100644 --- a/src/plugins/jx3/dungeon/__init__.py +++ b/src/plugins/jx3/dungeon/__init__.py @@ -68,7 +68,7 @@ async def _(event: GroupMessageEvent, args: Message = CommandArg()): data = get_content_local(data) await item.finish(ms.image(data)) -monsters = on_command("jx3_monsters_v2", aliases={"百战v2"}, force_whitespace=True, priority=5) +monsters = on_command("jx3_monsters_v2", aliases={"百战v2", "百战"}, force_whitespace=True, priority=5) @monsters.handle() async def _(event: GroupMessageEvent, args: Message = CommandArg()): diff --git a/src/plugins/jx3/horse/__init__.py b/src/plugins/jx3/horse/__init__.py index d859991be..c0e8ff4fe 100644 --- a/src/plugins/jx3/horse/__init__.py +++ b/src/plugins/jx3/horse/__init__.py @@ -1,9 +1,10 @@ from nonebot import on_command from nonebot.adapters import Message from nonebot.params import CommandArg -from nonebot.adapters.onebot.v11 import GroupMessageEvent +from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment as ms from .api import * +from .dilu import get_dilu_data jx3_cmd_horse = on_command("jx3_horse_v1", aliases={"抓马v1", "马场v1"}, force_whitespace=True, priority=5) @@ -19,4 +20,13 @@ async def _(event: GroupMessageEvent, args: Message = CommandArg()): async def _(event: GroupMessageEvent, args: Message = CommandArg()): server = args.extract_plain_text() msg = await get_horse_next_spawn(server, str(event.group_id)) - await jx3_cmd_horse_v2.finish(msg) \ No newline at end of file + await jx3_cmd_horse_v2.finish(msg) + +dilu = on_command("jx3_dilu", aliases={"的卢统计"}, force_whitespace=True, priority=5) + +@dilu.handle() +async def _(event: GroupMessageEvent, args: Message = CommandArg()): + if args.extract_plain_text() != "": + return + img = await get_dilu_data() + await dilu.finish(ms.image(img)) \ No newline at end of file diff --git a/src/plugins/jx3/horse/dilu.py b/src/plugins/jx3/horse/dilu.py new file mode 100644 index 000000000..7bbc735ce --- /dev/null +++ b/src/plugins/jx3/horse/dilu.py @@ -0,0 +1,163 @@ +from datetime import datetime, timedelta +from typing import List, Dict, Any +from pathlib import Path + +from src.constant.jx3 import brickl, goldl + +from src.tools.file import read, write +from src.tools.utils.request import get_api +from src.tools.utils.common import convert_time, getCurrentTime +from src.tools.utils.path import ASSETS, CACHE, TOOLS, VIEWS +from src.tools.generate import get_uuid, generate + +import json +import re +import time + +template = """ + + $server + 刷新:$flush
捕获:$captured
竞拍:$sell + $map + $capturer
$ci$cc + $auctioner
$bi$bc + $price + +""" + +bad = "" +good = "" + +class DiluData: + def __init__(self, raw_data: Dict[str, Any]): + with open(TOOLS + "/basic/server.json", mode="r", encoding="utf8") as servers_raw_data: + servers = json.loads(servers_raw_data.read()) + self.raw_data = raw_data + self.all_servers = list(servers) + self.servers_data = {server: [] for server in self.all_servers} + + def convert_timestamp(self, ts: int) -> datetime: + + return datetime.fromtimestamp(ts) + + def format_time(self, dt: datetime) -> str: + + return dt.strftime("%Y-%m-%d %H:%M:%S") + + def is_within_current_week(self, dt: datetime) -> bool: + + now = datetime.now() + if now.hour < 7: + now -= timedelta(days=1) + + week_start = now - timedelta(days=now.weekday()) + week_start = week_start.replace(hour=7, minute=0, second=0, microsecond=0) + + return week_start <= dt <= now + + def as_jx3api(self) -> List[Dict[str, Any]]: + + for item in self.raw_data['rows']: + server = item['Srv'] + if server not in self.servers_data: + continue + + row = { + "server": server, + "name": item["horsename"], + "map_name": item["mapname"], + "refresh_time": self.format_time(self.convert_timestamp(item["refreshtime"])), + "capture_role_name": item["capturerolename"] or "尚未捕捉", + "capture_camp_name": item["capturecampname"] or "未知", + "capture_time": self.format_time(self.convert_timestamp(item["capturetime"])) if item["capturetime"] else "尚未捕捉", + "auction_role_name": item["auctionrolename"] or "尚未竞拍", + "auction_camp_name": item["auctioncampname"] or "未知", + "auction_time": self.format_time(self.convert_timestamp(item["auctiontime"])) if item["auctiontime"] else "尚未竞拍", + "auction_amount": item["auctionamount"] or "尚未竞拍" + } + if self.is_within_current_week(self.convert_timestamp(item["refreshtime"])): + self.servers_data[server].append(row) + + final_data = [] + for server in self.all_servers: + final_data.append({ + "server": server, + "data": self.servers_data[server] + }) + + return final_data + +async def get_dilu_raw_data(): + filter = { + "Zone": "", + "Srv": "" + } + + params = { + "sort": "id", + "order": "desc", + "limit": "21", + "offset": "0", + "filter": json.dumps(filter, ensure_ascii=False), + "op": '{"Zone":"LIKE","Srv":"LIKE"}', + "_": int(time.time()) * 1000 + } + + headers = { + "Accept": "application/json, text/javascript, */*; q=0.01", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", + "Cache-Control": "no-cache", + "Connection": "keep-alive", + "Content-Type": "application/json", + "Pragma": "no-cache", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82", + "X-Requested-With": "XMLHttpRequest", + "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Microsoft Edge\";v=\"114\"", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "\"Windows\"", + "Referer": "https://www.jx3mm.com/jx3fun/jevent/jcitem.html" + } + data = await get_api("https://www.jx3mm.com/jx3fun/jevent/delu", headers=headers, params=params) + return data + +async def get_dilu_data(): + raw_data = await get_dilu_raw_data() + data = DiluData(raw_data).as_jx3api() + table = [] + for i in data: + if i["data"] == []: + table.append(re.sub(r"\$.+<", "暂无信息<", + template.replace("$server", i["server"]).replace("$img", ""))) + else: + data_ = i["data"][0] + server = i["server"] + flush = "尚未刷新" if data_["refresh_time"] is None else data_["refresh_time"] + capture = "尚未捕捉" if data_["capture_time"] is None else data_["capture_time"] + auction = "尚未竞拍" if data_["auction_time"] is None else data_["auction_time"] + map = data_["map_name"] + capturer = "尚未捕捉" if data_["capture_role_name"] is None else data_["capture_role_name"] + capturer_camp = "未知" if data_["capture_camp_name"] is None else data_["capture_camp_name"] + bidder = "尚未竞拍" if data_["auction_role_name"] is None else data_["auction_role_name"] + bidder_camp = "未知" if data_["auction_camp_name"] is None else data_["auction_camp_name"] + ci = good if capturer_camp == "浩气盟" else bad + bi = good if bidder_camp == "浩气盟" else bad + price = "尚未竞拍" if data_["auction_amount"] is None else data_["auction_amount"].replace("万金","万0金").replace("万", f"").replace("金", f"") + replace_string = [["$server", server], ["$flush", flush], ["$captured", capture], ["$sell", auction], ["$map", map], ["$capturer", capturer], ["$bi", bi], ["$ci", ci], ["$price", price], ["$auctioner", bidder], ["$bc", bidder_camp], ["$cc", capturer_camp]] + t = template + for x in replace_string: + t = t.replace(x[0], x[1]) + table.append(t) + content = "\n".join(table) + html = read(VIEWS + "/jx3/dilu/dilu.html") + font = ASSETS + "/font/custom.ttf" + saohua = "严禁将蓉蓉机器人与音卡共存,一经发现永久封禁!蓉蓉是抄袭音卡的劣质机器人!" + appinfo_time = convert_time(getCurrentTime(), "%H:%M:%S") + html = html.replace("$customfont", font).replace("$tablecontent", content).replace( + "$randomsaohua", saohua).replace("$appinfo", f"的卢统计 · {appinfo_time}") + final_html = CACHE + "/" + get_uuid() + ".html" + write(final_html, html) + final_path = await generate(final_html, False, "table", False) + return Path(final_path).as_uri() \ No newline at end of file diff --git a/src/plugins/jx3/jx3.py b/src/plugins/jx3/jx3.py index 9021e5245..8a4c9c40c 100644 --- a/src/plugins/jx3/jx3.py +++ b/src/plugins/jx3/jx3.py @@ -22,7 +22,6 @@ from .equip import * from .emoji import * from .detail import * -from .private import * from .trade import * from .events import * from .calendar import * diff --git a/src/plugins/jx3/private/README.md b/src/plugins/jx3/private/README.md deleted file mode 100644 index ac1009aaa..000000000 --- a/src/plugins/jx3/private/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# 闭源系列 - -请注意,本插件下的功能均为闭源功能,实现原理应数据方要求不便公开,很抱歉! - -通过`inkarsuki_offcial_apitoken`进行隐藏。 \ No newline at end of file diff --git a/src/plugins/jx3/private/__init__.py b/src/plugins/jx3/private/__init__.py deleted file mode 100644 index fa021acd6..000000000 --- a/src/plugins/jx3/private/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -from nonebot import on_command -from nonebot.adapters import Message -from nonebot.params import CommandArg -from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment as ms - -from src.tools.utils.request import get_content - -from .api import * - -import os - -dilu = on_command("jx3_dilu", aliases={"的卢统计"}, force_whitespace=True, priority=5) - - -@dilu.handle() -async def _(event: GroupMessageEvent, args: Message = CommandArg()): - if args.extract_plain_text() != "": - return - img = await get_dilu_data() - await dilu.finish(ms.image(img)) - - -monsters_v1 = on_command("jx3_monsters_v1", aliases={"百战"}, force_whitespace=True, priority=5) - -@monsters_v1.handle() -async def _(event: GroupMessageEvent, args: Message = CommandArg()): - if args.extract_plain_text() != "": - return - correct_path = ASSETS + "/jx3/monsters.jpg" - if os.path.exists(correct_path): - await monsters_v1.finish(ms.image(Path(correct_path).as_uri())) - else: - url = await get_baizhan_img() - data = await get_content(url) - with open(correct_path, mode="wb") as bz: - bz.write(data) - await monsters_v1.finish(ms.image(Path(correct_path).as_uri())) \ No newline at end of file diff --git a/src/plugins/jx3/private/api.py b/src/plugins/jx3/private/api.py deleted file mode 100644 index 54d805da5..000000000 --- a/src/plugins/jx3/private/api.py +++ /dev/null @@ -1,92 +0,0 @@ -from pathlib import Path - -from src.constant.jx3 import brickl, goldl - -from src.tools.config import Config -from src.tools.utils.request import get_api, post_url -from src.tools.utils.common import convert_time, getCurrentTime -from src.tools.file import read, write -from src.tools.utils.path import ASSETS, CACHE, VIEWS -from src.tools.generate import generate, get_uuid - -import json -import re - -bot_name = Config.bot_basic.bot_name_argument -ikst = Config.hidden.offcial_token - - -def get_headers(application_type: str) -> dict: - headers = { - "token": ikst, - "type": application_type - } - return headers - - -async def get_url_with_token(app: str) -> dict: - if ikst == "": - raise KeyError("Unmatched the token to Inkar-Suki offical API.") - api = "https://inkar-suki.codethink.cn/api" - data = await post_url(url=api, headers=get_headers(app)) - return json.loads(data)["url"] - -template = """ - - $server - 刷新:$flush
捕获:$captured
竞拍:$sell - $map - $capturer
$ci$cc - $auctioner
$bi$bc - $price - -""" - -bad = "" -good = "" - - -async def get_baizhan_img(): - url = await get_url_with_token("baizhan") - data = await get_api(url + f"&nickname={bot_name}") - return data["data"]["url"] - -async def get_dilu_data(): - url = await get_url_with_token("dilu") - data = await get_api(url) - table = [] - for i in data["data"]: - if i["data"] == []: - table.append(re.sub(r"\$.+<", "暂无信息<", - template.replace("$server", i["server"]).replace("$img", ""))) - else: - data_ = i["data"] - server = i["server"] - flush = "尚未刷新" if data_["refresh_time"] is None else convert_time(data_["refresh_time"]) - capture = "尚未捕捉" if data_["capture_time"] is None else convert_time(data_["capture_time"]) - auction = "尚未竞拍" if data_["auction_time"] is None else convert_time(data_["auction_time"]) - map = data_["map_name"] - capturer = "尚未捕捉" if data_["capture_role_name"] is None else data_["capture_role_name"] - capturer_camp = "未知" if data_["capture_camp_name"] is None else data_["capture_camp_name"] - bidder = "尚未竞拍" if data_["auction_role_name"] is None else data_["auction_role_name"] - bidder_camp = "未知" if data_["auction_camp_name"] is None else data_["auction_camp_name"] - ci = good if capturer_camp == "浩气盟" else bad - bi = good if bidder_camp == "浩气盟" else bad - price = "尚未竞拍" if data_["auction_amount"] is None else data_["auction_amount"].replace("万金","万0金").replace("万", f"").replace("金", f"") - replace_string = [["$server", server], ["$flush", flush], ["$captured", capture], ["$sell", auction], ["$map", map], ["$capturer", capturer], ["$bi", bi], ["$ci", ci], ["$price", price], ["$auctioner", bidder], ["$bc", bidder_camp], ["$cc", capturer_camp]] - t = template - for x in replace_string: - t = t.replace(x[0], x[1]) - table.append(t) - content = "\n".join(table) - html = read(VIEWS + "/jx3/dilu/dilu.html") - font = ASSETS + "/font/custom.ttf" - saohua = "严禁将蓉蓉机器人与音卡共存,一经发现永久封禁!蓉蓉是抄袭音卡的劣质机器人!" - - appinfo_time = convert_time(getCurrentTime(), "%H:%M:%S") - html = html.replace("$customfont", font).replace("$tablecontent", content).replace( - "$randomsaohua", saohua).replace("$appinfo", f"的卢统计 · {appinfo_time}") - final_html = CACHE + "/" + get_uuid() + ".html" - write(final_html, html) - final_path = await generate(final_html, False, "table", False) - return Path(final_path).as_uri() \ No newline at end of file