54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import re
|
|
from astrbot.api import logger
|
|
class Pattern:
|
|
_pattern = r'<wt>((?!wt).)*</wt>'
|
|
|
|
_MAX_SEC = 1800
|
|
_MIN_SEC = 10
|
|
# 定义最大与最小的范围,固定输入的规范
|
|
def __init__(self,o_str:str) -> None:
|
|
res = self.extract(o_str)
|
|
self.count:int|None = res['second']
|
|
self.string:str = res["content"]
|
|
|
|
|
|
def sec_range(self,i)->int:
|
|
if i > Pattern._MAX_SEC:
|
|
return Pattern._MAX_SEC
|
|
if i < Pattern._MIN_SEC :
|
|
return Pattern._MIN_SEC
|
|
return i
|
|
|
|
|
|
def load(self,pattern_str:str)->int|None:
|
|
content = pattern_str.strip().removeprefix("<wt>").removesuffix("</wt>").strip()
|
|
print(content)
|
|
try:
|
|
num = int(content)
|
|
return self.sec_range(num)
|
|
except:
|
|
logger.debug ("模板内容错误:{}".format(content))
|
|
return None
|
|
|
|
def extract(self,o_str:str):
|
|
# 多次匹配防止重复模板
|
|
timerSec :None|int = None
|
|
res=re.finditer(Pattern._pattern,o_str)
|
|
for i in res :
|
|
# 对结果和源字符串处理
|
|
o_str = o_str.replace(i.group(),"",1)
|
|
num = self.load(i.group())
|
|
logger.info("获得模板:{0}".format(i.group()))
|
|
|
|
if num and not timerSec:
|
|
timerSec = num
|
|
#消除不完整/非法模板
|
|
o_str= o_str.replace("<wt>","")
|
|
o_str=o_str.replace("</wt>","")
|
|
|
|
return {
|
|
"content":o_str,
|
|
"second":timerSec
|
|
}
|
|
print(Pattern("awdwadawd<wt>ada</wt>"))
|