旧词条提供图片迁移

This commit is contained in:
HibiKier 2022-09-04 22:05:56 +08:00
parent 3cbd899daa
commit a36e8145a8
3 changed files with 83 additions and 13 deletions

View File

@ -279,6 +279,10 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能
## 更新
### 2022/9/4
* 旧词条提供图片迁移需要重新获取old_model文件并将数据库中user_qq为0的数据删除
### 2022/9/3
* 原神玩家查询增加须弥地区 [@pull/1053](https://github.com/HibiKier/zhenxun_bot/pull/1053)

View File

@ -1,3 +1,5 @@
import random
import time
from pathlib import Path
from nonebot.adapters.onebot.v11 import Message, MessageSegment
@ -189,6 +191,9 @@ async def _():
logger.info('开始迁移词条 纯文本 数据')
try:
word_list = await OldWordBank.get_all()
new_answer_path = Path() / 'data' / 'word_bank' / 'answer'
new_problem_path = Path() / 'data' / 'word_bank' / 'problem'
new_answer_path.mkdir(exist_ok=True, parents=True)
for word in word_list:
problem: str = word.problem
user_id = word.user_qq
@ -199,6 +204,25 @@ async def _():
if '[CQ' not in problem and '[CQ' not in answer and '[_to_me' not in problem:
if not format_:
await WordBank.add_problem_answer(user_id, group_id, 1, 0, problem, answer)
else:
placeholder = []
for m in format_.split('<format>'):
x = m.split('<_s>')
if x[0]:
idx, file_name = x[0], x[1]
if 'jpg' in file_name:
answer = answer.replace(f'[__placeholder_{idx}]', f'[image:placeholder_{idx}]')
file = Path() / 'data' / 'word_bank' / f'{group_id}' / file_name
rand = int(time.time()) + random.randint(1, 100000)
if file.exists():
new_file = new_answer_path / f'{group_id}' / f'{user_id}_{rand}.jpg'
new_file.parent.mkdir(exist_ok=True, parents=True)
with open(file, 'rb') as rb:
with open(new_file, 'wb') as wb:
wb.write(rb.read())
# file.rename(new_file)
placeholder.append(f'answer/{group_id}/{user_id}_{rand}.jpg')
await WordBank._move(user_id, group_id, problem, answer, ",".join(placeholder))
await WordBank.add_problem_answer(0, 0, 999, 0, '_[OK', '_[OK')
logger.info('词条 纯文本 数据迁移完成')
(Path() / 'plugins' / 'word_bank' / '_old_model.py').unlink()

View File

@ -48,6 +48,7 @@ class WordBank(db.Model):
user_id: Optional[int],
group_id: Optional[int],
problem: str,
answer: Optional[str],
word_scope: Optional[int] = None,
word_type: Optional[int] = None,
) -> bool:
@ -58,6 +59,7 @@ class WordBank(db.Model):
:param user_id: 用户id
:param group_id: 群号
:param problem: 问题
:param answer: 回答
:param word_scope: 词条范围
:param word_type: 词条类型
"""
@ -66,6 +68,8 @@ class WordBank(db.Model):
query = query.where(cls.user_qq == user_id)
if group_id:
query = query.where(cls.group_id == group_id)
if answer:
query = query.where(cls.answer == answer)
if word_type:
query = query.where(cls.word_type == word_type)
if word_scope:
@ -105,19 +109,20 @@ class WordBank(db.Model):
problem = str(get_img_hash(_file))
image_path = f"problem/{group_id}/{user_id}_{int(time.time())}.jpg"
answer, _list = await cls._answer2format(answer, user_id, group_id)
await cls.create(
user_qq=user_id,
group_id=group_id,
word_scope=word_scope,
word_type=word_type,
status=True,
problem=problem,
answer=answer,
image_path=image_path,
placeholder=",".join(_list),
create_time=datetime.now().replace(microsecond=0),
update_time=datetime.now().replace(microsecond=0),
)
if not await cls.exists(user_id, group_id, problem, answer, word_scope, word_type):
await cls.create(
user_qq=user_id,
group_id=group_id,
word_scope=word_scope,
word_type=word_type,
status=True,
problem=problem,
answer=answer,
image_path=image_path,
placeholder=",".join(_list),
create_time=datetime.now().replace(microsecond=0),
update_time=datetime.now().replace(microsecond=0),
)
@classmethod
async def _answer2format(
@ -479,3 +484,40 @@ class WordBank(db.Model):
problem_list.append(problem)
_tmp.append(q.problem)
return problem_list
@classmethod
async def _move(
cls,
user_id: int,
group_id: Optional[int],
problem: Union[str, Message],
answer: Union[str, Message],
placeholder: str,
):
"""
说明:
旧词条图片移动方法
参数:
:param user_id: 用户id
:param group_id: 群号
:param problem: 问题
:param answer: 回答
:param placeholder: 占位符
"""
word_scope = 0
word_type = 0
# 对图片做额外处理
if not await cls.exists(user_id, group_id, problem, answer, word_scope, word_type):
await cls.create(
user_qq=user_id,
group_id=group_id,
word_scope=word_scope,
word_type=word_type,
status=True,
problem=problem,
answer=answer,
image_path=None,
placeholder=placeholder,
create_time=datetime.now().replace(microsecond=0),
update_time=datetime.now().replace(microsecond=0),
)