2021-11-17 15:25:16 +00:00
|
|
|
import re
|
2021-11-16 14:30:49 +00:00
|
|
|
from datetime import datetime
|
2021-10-28 15:44:45 +00:00
|
|
|
from typing import Union
|
2022-03-28 15:59:55 +00:00
|
|
|
from urllib.parse import urlparse
|
2021-11-15 16:26:11 +00:00
|
|
|
|
2021-08-29 16:53:43 +00:00
|
|
|
import ujson as json
|
2021-11-15 16:26:11 +00:00
|
|
|
from tenacity import retry, stop_after_attempt
|
2021-08-07 07:56:48 +00:00
|
|
|
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins import MessageSession
|
2021-11-03 11:27:21 +00:00
|
|
|
from database import session, auto_rollback_error
|
2022-09-06 15:30:39 +00:00
|
|
|
from modules.wiki.utils.orm import WikiTargetSetInfo, WikiInfo, WikiAllowList, WikiBlockList
|
2021-09-12 12:51:13 +00:00
|
|
|
|
2021-07-28 18:51:24 +00:00
|
|
|
|
|
|
|
class WikiTargetInfo:
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-08-23 14:17:04 +00:00
|
|
|
def __init__(self, msg: [MessageSession, str]):
|
|
|
|
if isinstance(msg, MessageSession):
|
2021-11-17 15:25:16 +00:00
|
|
|
if msg.target.targetFrom != 'QQ|Guild':
|
|
|
|
targetId = msg.target.targetId
|
|
|
|
else:
|
2021-11-17 16:17:41 +00:00
|
|
|
targetId = re.match(r'(QQ\|Guild\|.*?)\|.*', msg.target.targetId).group(1)
|
2021-08-23 14:17:04 +00:00
|
|
|
else:
|
|
|
|
targetId = msg
|
2021-11-03 11:27:21 +00:00
|
|
|
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=targetId).first()
|
|
|
|
if self.query is None:
|
|
|
|
session.add_all([WikiTargetSetInfo(targetId=targetId, iws='{}', headers='{}')])
|
|
|
|
session.commit()
|
2021-09-16 15:55:46 +00:00
|
|
|
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=targetId).first()
|
2021-07-28 18:51:24 +00:00
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-11-01 15:54:12 +00:00
|
|
|
def add_start_wiki(self, url):
|
2021-11-03 11:27:21 +00:00
|
|
|
self.query.link = url
|
|
|
|
session.commit()
|
|
|
|
session.expire_all()
|
|
|
|
return True
|
2021-07-28 18:51:24 +00:00
|
|
|
|
2021-10-28 15:44:45 +00:00
|
|
|
def get_start_wiki(self) -> Union[str, None]:
|
2021-07-30 19:32:58 +00:00
|
|
|
if self.query is not None:
|
2021-10-28 15:44:45 +00:00
|
|
|
return self.query.link if self.query.link is not None else None
|
2021-07-28 18:51:24 +00:00
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-11-01 15:54:12 +00:00
|
|
|
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
|
2021-11-03 11:27:21 +00:00
|
|
|
interwikis = json.loads(self.query.iws)
|
|
|
|
if let_it:
|
|
|
|
interwikis[iw] = iwlink
|
|
|
|
else:
|
|
|
|
if iw in interwikis:
|
|
|
|
del interwikis[iw]
|
|
|
|
self.query.iws = json.dumps(interwikis)
|
|
|
|
session.commit()
|
|
|
|
session.expire_all()
|
|
|
|
return True
|
2021-07-28 18:51:24 +00:00
|
|
|
|
2021-10-28 15:44:45 +00:00
|
|
|
def get_interwikis(self) -> dict:
|
2021-07-28 18:51:24 +00:00
|
|
|
q = self.query.iws
|
|
|
|
if q is not None:
|
|
|
|
iws = json.loads(q)
|
|
|
|
return iws
|
|
|
|
else:
|
2021-10-28 15:44:45 +00:00
|
|
|
return {}
|
2021-07-28 18:51:24 +00:00
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-07-28 18:51:24 +00:00
|
|
|
def config_headers(self, headers, let_it: [bool, None] = True):
|
2021-11-03 11:27:21 +00:00
|
|
|
headers = json.loads(headers)
|
|
|
|
headers_ = json.loads(self.query.headers)
|
|
|
|
if let_it:
|
|
|
|
for x in headers:
|
|
|
|
headers_[x] = headers[x]
|
|
|
|
elif let_it is None:
|
|
|
|
headers_ = {}
|
|
|
|
else:
|
|
|
|
for x in headers:
|
|
|
|
if x in headers_:
|
|
|
|
del headers_[x]
|
|
|
|
self.query.headers = json.dumps(headers_)
|
|
|
|
session.commit()
|
|
|
|
return True
|
2021-07-28 18:51:24 +00:00
|
|
|
|
|
|
|
def get_headers(self):
|
2021-07-30 18:06:04 +00:00
|
|
|
if self.query is not None:
|
|
|
|
q = self.query.headers
|
2021-07-28 18:51:24 +00:00
|
|
|
headers = json.loads(q)
|
|
|
|
else:
|
|
|
|
headers = {'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'}
|
|
|
|
return headers
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-12-19 08:23:24 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def set_prefix(self, prefix: str):
|
|
|
|
self.query.prefix = prefix
|
|
|
|
session.commit()
|
|
|
|
return True
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-12-19 08:23:24 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def del_prefix(self):
|
|
|
|
self.query.prefix = None
|
|
|
|
session.commit()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_prefix(self):
|
|
|
|
return self.query.prefix
|
|
|
|
|
2021-07-28 18:51:24 +00:00
|
|
|
|
|
|
|
class WikiSiteInfo:
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-07-28 18:51:24 +00:00
|
|
|
def __init__(self, api_link):
|
2021-11-03 11:27:21 +00:00
|
|
|
self.api_link = api_link
|
|
|
|
self.query = session.query(WikiInfo).filter_by(apiLink=api_link).first()
|
2021-07-28 18:51:24 +00:00
|
|
|
|
|
|
|
def get(self):
|
|
|
|
if self.query is not None:
|
|
|
|
return self.query.siteInfo, self.query.timestamp
|
|
|
|
return False
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-03 11:27:21 +00:00
|
|
|
@auto_rollback_error
|
2021-07-28 18:51:24 +00:00
|
|
|
def update(self, info: dict):
|
2021-11-03 11:27:21 +00:00
|
|
|
if self.query is None:
|
|
|
|
session.add_all([WikiInfo(apiLink=self.api_link, siteInfo=json.dumps(info))])
|
|
|
|
else:
|
|
|
|
self.query.siteInfo = json.dumps(info)
|
2021-11-16 14:30:49 +00:00
|
|
|
self.query.timestamp = datetime.now()
|
2021-11-03 11:27:21 +00:00
|
|
|
session.commit()
|
|
|
|
return True
|
2021-11-15 16:26:11 +00:00
|
|
|
|
2022-09-01 15:05:17 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_like_this(t: str):
|
|
|
|
return session.query(WikiInfo).filter(WikiInfo.apiLink.like(f"%{t}%")).first()
|
|
|
|
|
2021-11-15 16:26:11 +00:00
|
|
|
|
|
|
|
class Audit:
|
|
|
|
def __init__(self, api_link):
|
|
|
|
self.api_link = api_link
|
|
|
|
|
|
|
|
@property
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-15 16:26:11 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def inAllowList(self) -> bool:
|
|
|
|
session.expire_all()
|
2022-03-28 15:59:55 +00:00
|
|
|
apilink = urlparse(self.api_link).netloc
|
2022-07-31 08:33:20 +00:00
|
|
|
return True if session.query(WikiAllowList).filter(
|
|
|
|
WikiAllowList.apiLink.like(f'%{apilink}%')).first() else False
|
2021-11-15 16:26:11 +00:00
|
|
|
|
2021-11-17 13:17:13 +00:00
|
|
|
@property
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-17 13:17:13 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def inBlockList(self) -> bool:
|
|
|
|
session.expire_all()
|
|
|
|
return True if session.query(WikiBlockList).filter_by(apiLink=self.api_link).first() else False
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-15 16:26:11 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def add_to_AllowList(self, op) -> bool:
|
|
|
|
if self.inAllowList:
|
|
|
|
return False
|
2021-11-15 16:34:36 +00:00
|
|
|
session.add_all([WikiAllowList(apiLink=self.api_link, operator=op)])
|
2021-11-15 16:26:11 +00:00
|
|
|
session.commit()
|
|
|
|
session.expire_all()
|
|
|
|
return True
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-15 16:26:11 +00:00
|
|
|
@auto_rollback_error
|
2023-04-02 12:53:32 +00:00
|
|
|
def remove_from_AllowList(self) -> Union[bool, None]:
|
2021-11-15 16:26:11 +00:00
|
|
|
if not self.inAllowList:
|
|
|
|
return False
|
2023-04-02 12:53:32 +00:00
|
|
|
if (query := session.query(WikiAllowList).filter_by(apiLink=self.api_link).first()) is not None:
|
|
|
|
session.delete(query)
|
|
|
|
session.commit()
|
|
|
|
session.expire_all()
|
|
|
|
return True
|
|
|
|
return None
|
2021-11-17 13:17:13 +00:00
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-17 13:17:13 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def add_to_BlockList(self, op) -> bool:
|
|
|
|
if self.inBlockList:
|
|
|
|
return False
|
|
|
|
session.add_all([WikiBlockList(apiLink=self.api_link, operator=op)])
|
|
|
|
session.commit()
|
|
|
|
session.expire_all()
|
|
|
|
return True
|
|
|
|
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-17 13:17:13 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def remove_from_BlockList(self) -> bool:
|
|
|
|
if not self.inBlockList:
|
|
|
|
return False
|
|
|
|
session.delete(session.query(WikiBlockList).filter_by(apiLink=self.api_link).first())
|
|
|
|
session.commit()
|
2021-11-15 16:26:11 +00:00
|
|
|
session.expire_all()
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-17 13:17:13 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def get_allow_list() -> list:
|
2021-11-15 16:34:36 +00:00
|
|
|
return session.query(WikiAllowList.apiLink, WikiAllowList.operator)
|
2021-11-17 13:17:13 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-19 17:02:23 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), reraise=True)
|
2021-11-17 13:17:13 +00:00
|
|
|
@auto_rollback_error
|
|
|
|
def get_block_list() -> list:
|
|
|
|
return session.query(WikiBlockList.apiLink, WikiBlockList.operator)
|