Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/wiki/utils/dbutils.py

211 lines
6.9 KiB
Python
Raw Normal View History

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
import ujson as json
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
from database import session, auto_rollback_error
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:
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
2021-08-23 14:17:04 +00:00
def __init__(self, msg: [MessageSession, str]):
if isinstance(msg, MessageSession):
2023-09-01 14:38:32 +00:00
if msg.target.target_from != 'QQ|Guild':
target_id = msg.target.target_id
2021-11-17 15:25:16 +00:00
else:
2023-09-01 14:38:32 +00:00
target_id = re.match(r'(QQ\|Guild\|.*?)\|.*', msg.target.target_id).group(1)
2021-08-23 14:17:04 +00:00
else:
2023-09-01 14:38:32 +00:00
target_id = msg
2023-09-03 08:58:46 +00:00
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=target_id).first()
if self.query is None:
2023-09-03 08:58:46 +00:00
session.add_all([WikiTargetSetInfo(targetId=target_id, iws='{}', headers='{}')])
session.commit()
2023-09-03 08:58:46 +00:00
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=target_id).first()
2021-07-28 18:51:24 +00:00
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
def add_start_wiki(self, url):
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
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
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
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
2021-07-28 18:51:24 +00:00
def config_headers(self, headers, let_it: [bool, None] = True):
2023-11-13 04:39:22 +00:00
try:
headers_ = json.loads(self.query.headers)
if let_it:
headers = json.loads(headers)
2023-11-13 04:39:22 +00:00
for x in headers:
headers_[x] = headers[x]
elif let_it is None:
headers_ = {}
else:
headers = {k: v for k, v in headers_.items() if k not in headers}
2023-11-13 04:39:22 +00:00
self.query.headers = json.dumps(headers_)
session.commit()
return True
except TypeError:
2023-11-13 04:39:22 +00:00
return False
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
@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
@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:
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
2021-07-28 18:51:24 +00:00
def __init__(self, api_link):
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
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
2021-07-28 18:51:24 +00:00
def update(self, info: dict):
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()
session.commit()
return True
@staticmethod
def get_like_this(t: str):
return session.query(WikiInfo).filter(WikiInfo.apiLink.like(f"%{t}%")).first()
class Audit:
def __init__(self, api_link):
self.api_link = api_link
@property
@retry(stop=stop_after_attempt(3), reraise=True)
@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-17 13:17:13 +00:00
@property
@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
@retry(stop=stop_after_attempt(3), reraise=True)
@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)])
session.commit()
session.expire_all()
return True
@retry(stop=stop_after_attempt(3), reraise=True)
@auto_rollback_error
2023-04-02 12:53:32 +00:00
def remove_from_AllowList(self) -> Union[bool, None]:
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
@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
@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()
session.expire_all()
return True
@staticmethod
@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
@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)