pepperbot/apps/riic_report_analysis.py

78 lines
2.6 KiB
Python
Raw Normal View History

2023-08-14 10:40:47 +08:00
from pepperbot.core.message.chain import MessageChain, Image, Text, At
2023-08-14 09:24:03 +08:00
from rapidocr_onnxruntime import RapidOCR
2023-08-13 19:42:17 +08:00
import os
2023-08-13 20:18:55 +08:00
import asyncio
import functools
2023-08-14 09:24:03 +08:00
import re
2023-08-13 18:31:28 +08:00
2023-08-14 09:24:03 +08:00
rapid_ocr = None
if not rapid_ocr:
rapid_ocr = RapidOCR("rapid_ocr.yml")
2023-08-13 20:05:54 +08:00
2023-08-13 18:31:28 +08:00
class RIICReportAnalysis:
async def group_message(self, chain: MessageChain):
2023-08-13 19:50:02 +08:00
if len(chain) != 1 or len(chain.images) != 1:
2023-08-13 19:42:17 +08:00
return
img_seg: Image = chain[0]
img_path = await img_seg.download()
2023-08-13 20:18:55 +08:00
loop = asyncio.get_running_loop()
2023-08-14 09:24:03 +08:00
result, elapse = await loop.run_in_executor(
None, functools.partial(rapid_ocr, img_path)
2023-08-13 20:18:55 +08:00
)
if not result or not "副手简报" in [i[1] for i in result]:
os.remove(img_path)
2023-08-14 09:24:03 +08:00
return
2023-08-14 09:24:03 +08:00
lmb_height = 0
lmb_list = []
2023-08-14 09:24:03 +08:00
date_list = []
exp_list = []
gold_list = []
for i in result:
if i[1] == "龙门币":
lmb_height = i[0][0][1]
break
for i in result:
if m := re.search(r"([0-9]+\.[0-9]+)", i[1]):
2023-08-14 10:40:47 +08:00
if float(i[2]) < 0.6:
continue
2023-08-14 09:24:03 +08:00
date_list.append([int(i[0][0][0]), m.group(1)])
continue
if m := re.search(r"EXP([0-9]+)的作战记录", i[1]):
exp_list.append([int(i[0][0][0]), m.group(1)])
continue
if m := re.search(r"([0-9]+)的贵金属", i[1]):
gold_list.append([int(i[0][0][0]), m.group(1)])
continue
if lmb_height - 20 < i[0][0][1] < lmb_height + 20:
if m := re.search(r"([0-9]+)", i[1]):
lmb_list.append([int(i[0][0][0]), m.group(1)])
2023-08-14 09:24:03 +08:00
lmb_list.sort(key=lambda x: x[0])
2023-08-14 09:24:03 +08:00
gold_list.sort(key=lambda x: x[0])
exp_list.sort(key=lambda x: x[0])
output = ""
2023-08-14 10:40:47 +08:00
try:
for i in range(len(date_list)):
output += f"{date_list[i][1]}\n"
output += f"💵 {lmb_list[2 * i][1]}订单({lmb_list[2 * i + 1][1]})\n"
output += f"🧈 {gold_list[i][1]}赤金\n"
output += f"📼 {exp_list[i][1]}经验\n\n"
2023-08-14 09:24:03 +08:00
2023-08-14 10:40:47 +08:00
await chain.onebot_reply(Text(output.strip()))
except:
err_msg = "出错啦!\n"
err_msg += f"date_list = {date_list}\n"
err_msg += f"lmb_list = {lmb_list}\n"
err_msg += f"gold_list = {gold_list}\n"
err_msg += f"exp_list = {exp_list}"
await chain.onebot_reply(Text(err_msg), At("1040110848"))
2023-08-13 19:42:17 +08:00
os.remove(img_path)