mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 11:49:59 +08:00
modified: basic_plugins/admin_bot_manage/_data_source.py
modified: basic_plugins/admin_bot_manage/admin_config.py modified: basic_plugins/admin_bot_manage/custom_welcome_message.py modified: basic_plugins/admin_bot_manage/timing_task.py modified: basic_plugins/apscheduler/__init__.py modified: basic_plugins/ban/__init__.py modified: basic_plugins/ban/data_source.py modified: basic_plugins/chat_history/chat_message.py modified: basic_plugins/chat_history/chat_message_handle.py modified: basic_plugins/group_handle/__init__.py modified: basic_plugins/hooks/_utils.py modified: basic_plugins/hooks/ban_hook.py modified: basic_plugins/hooks/chkdsk_hook.py modified: basic_plugins/init_plugin_config/__init__.py deleted: basic_plugins/init_plugin_config/init_group_manager.py modified: basic_plugins/invite_manager/__init__.py new file: basic_plugins/invite_manager/utils.py modified: basic_plugins/nickname.py modified: basic_plugins/plugin_shop/__init__.py modified: basic_plugins/plugin_shop/data_source.py modified: basic_plugins/scripts.py modified: basic_plugins/shop/__init__.py modified: basic_plugins/shop/buy.py modified: basic_plugins/shop/gold.py modified: basic_plugins/shop/my_props/__init__.py deleted: basic_plugins/shop/reset_today_gold.py modified: basic_plugins/shop/shop_handle/__init__.py modified: basic_plugins/shop/shop_handle/data_source.py modified: basic_plugins/shop/use/__init__.py modified: basic_plugins/shop/use/data_source.py modified: basic_plugins/super_cmd/__init__.py modified: basic_plugins/super_cmd/bot_friend_group.py modified: basic_plugins/super_cmd/clear_data.py modified: basic_plugins/super_cmd/exec_sql.py modified: basic_plugins/super_cmd/manager_group.py modified: basic_plugins/super_cmd/reload_setting.py modified: basic_plugins/super_cmd/set_admin_permissions.py deleted: basic_plugins/super_cmd/super_task_switch.py modified: basic_plugins/super_cmd/update_friend_group_info.py modified: basic_plugins/super_help/__init__.py modified: basic_plugins/update_info.py modified: configs/config.py modified: configs/utils/__init__.py modified: models/bag_user.py modified: models/ban_user.py modified: models/chat_history.py modified: models/friend_user.py modified: models/goods_info.py modified: models/group_info.py modified: models/group_member_info.py modified: models/level_user.py modified: models/sign_group_user.py modified: models/user_shop_gold_log.py modified: plugins/aconfig/__init__.py modified: plugins/ai/__init__.py modified: plugins/ai/data_source.py modified: plugins/bilibili_sub/__init__.py modified: plugins/bilibili_sub/data_source.py modified: plugins/bilibili_sub/model.py modified: plugins/black_word/__init__.py modified: plugins/black_word/model.py modified: plugins/black_word/utils.py modified: plugins/bt/data_source.py modified: plugins/genshin/almanac/__init__.py modified: plugins/genshin/material_remind/__init__.py modified: plugins/genshin/query_user/_models/__init__.py modified: plugins/genshin/query_user/_utils/__init__.py modified: plugins/genshin/query_user/bind/__init__.py modified: plugins/genshin/query_user/genshin_sign/__init__.py modified: plugins/genshin/query_user/genshin_sign/data_source.py modified: plugins/genshin/query_user/genshin_sign/init_task.py modified: plugins/genshin/query_user/mihoyobbs_sign/__init__.py modified: plugins/genshin/query_user/query_memo/__init__.py modified: plugins/genshin/query_user/query_memo/data_source.py modified: plugins/genshin/query_user/query_role/__init__.py modified: plugins/genshin/query_user/query_role/data_source.py modified: plugins/genshin/query_user/reset_today_query_user_data/__init__.py modified: plugins/genshin/query_user/resin_remind/__init__.py modified: plugins/genshin/query_user/resin_remind/init_task.py modified: plugins/gold_redbag/model.py modified: plugins/image_management/send_image/__init__.py modified: plugins/my_info/__init__.py modified: plugins/open_cases/models/buff_prices.py modified: plugins/open_cases/models/open_cases_user.py modified: plugins/open_cases/open_cases_c.py modified: plugins/open_cases/utils.py modified: plugins/parse_bilibili_json.py modified: plugins/pid_search.py modified: plugins/pix_gallery/__init__.py modified: plugins/pix_gallery/_data_source.py modified: plugins/pix_gallery/_model/omega_pixiv_illusts.py modified: plugins/pix_gallery/_model/pixiv.py modified: plugins/pix_gallery/_model/pixiv_keyword_user.py modified: plugins/pix_gallery/pix_add_keyword.py modified: plugins/pix_gallery/pix_pass_del_keyword.py modified: plugins/pix_gallery/pix_show_info.py modified: plugins/pix_gallery/pix_update.py modified: plugins/pixiv_rank_search/data_source.py modified: plugins/poke/__init__.py modified: plugins/russian/__init__.py modified: plugins/russian/data_source.py modified: plugins/russian/model.py modified: plugins/send_dinggong_voice/__init__.py modified: plugins/send_setu_/_model.py modified: plugins/send_setu_/send_setu/__init__.py modified: plugins/send_setu_/send_setu/data_source.py modified: plugins/send_setu_/update_setu/data_source.py modified: plugins/sign_in/goods_register.py modified: plugins/sign_in/group_user_checkin.py modified: plugins/sign_in/random_event.py modified: plugins/sign_in/utils.py modified: plugins/statistics/_model.py modified: plugins/statistics/statistics_handle.py modified: plugins/statistics/statistics_hook.py modified: plugins/update_picture.py modified: plugins/web_ui/api/request.py modified: plugins/word_bank/_model.py deleted: plugins/word_bank/_old_model.py modified: plugins/word_bank/_rule.py modified: plugins/word_bank/word_handle.py modified: plugins/word_clouds/data_source.py modified: resources/image/sign/sign_res/bar.png modified: resources/image/sign/sign_res/bar_white.png modified: services/db_context.py modified: services/log.py modified: utils/browser.py modified: utils/data_utils.py modified: utils/depends/__init__.py modified: utils/http_utils.py modified: utils/image_utils.py modified: utils/manager/admin_manager.py modified: utils/message_builder.py modified: utils/utils.py
This commit is contained in:
+48
-161
@@ -1,64 +1,44 @@
|
||||
from services.db_context import db
|
||||
from typing import List, Optional
|
||||
|
||||
from tortoise import fields
|
||||
from tortoise.contrib.postgres.functions import Random
|
||||
from tortoise.expressions import Q
|
||||
|
||||
class Setu(db.Model):
|
||||
__tablename__ = "setu"
|
||||
__table_args__ = {'extend_existing': True}
|
||||
from services.db_context import Model
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
local_id = db.Column(db.Integer(), nullable=False)
|
||||
title = db.Column(db.String(), nullable=False)
|
||||
author = db.Column(db.String(), nullable=False)
|
||||
pid = db.Column(db.BigInteger(), nullable=False)
|
||||
img_hash = db.Column(db.String(), nullable=False)
|
||||
img_url = db.Column(db.String(), nullable=False)
|
||||
is_r18 = db.Column(db.Boolean(), nullable=False)
|
||||
tags = db.Column(db.String())
|
||||
|
||||
_idx1 = db.Index("setu_pid_img_url_idx1", "pid", "img_url", unique=True)
|
||||
class Setu(Model):
|
||||
|
||||
@classmethod
|
||||
async def add_setu_data(
|
||||
cls,
|
||||
local_id: int,
|
||||
title: str,
|
||||
author: str,
|
||||
pid: int,
|
||||
img_hash: str,
|
||||
img_url: str,
|
||||
tags: str,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
添加一份色图数据
|
||||
参数:
|
||||
:param local_id: 本地存储id
|
||||
:param title: 标题
|
||||
:param author: 作者
|
||||
:param pid: 图片pid
|
||||
:param img_hash: 图片hash值
|
||||
:param img_url: 图片链接
|
||||
:param tags: 图片标签
|
||||
"""
|
||||
if not await cls._check_exists(pid, img_url):
|
||||
await cls.create(
|
||||
local_id=local_id,
|
||||
title=title,
|
||||
author=author,
|
||||
pid=pid,
|
||||
img_hash=img_hash,
|
||||
img_url=img_url,
|
||||
is_r18=True if "R-18" in tags else False,
|
||||
tags=tags,
|
||||
)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
local_id = fields.IntField()
|
||||
"""本地存储下标"""
|
||||
title = fields.CharField(255)
|
||||
"""标题"""
|
||||
author = fields.CharField(255)
|
||||
"""作者"""
|
||||
pid = fields.BigIntField()
|
||||
"""pid"""
|
||||
img_hash = fields.TextField()
|
||||
"""图片hash"""
|
||||
img_url = fields.CharField(255)
|
||||
"""pixiv url链接"""
|
||||
is_r18 = fields.BooleanField()
|
||||
"""是否r18"""
|
||||
tags = fields.TextField()
|
||||
"""tags"""
|
||||
|
||||
class Meta:
|
||||
table = "setu"
|
||||
table_description = "色图数据表"
|
||||
unique_together = ("pid", "img_url")
|
||||
|
||||
@classmethod
|
||||
async def query_image(
|
||||
cls,
|
||||
local_id: Optional[int] = None,
|
||||
tags: Optional[List[str]] = None,
|
||||
r18: int = 0,
|
||||
r18: bool = False,
|
||||
limit: int = 50,
|
||||
):
|
||||
"""
|
||||
@@ -71,58 +51,17 @@ class Setu(db.Model):
|
||||
:param limit: 获取数量
|
||||
"""
|
||||
if local_id:
|
||||
flag = True if r18 == 1 else False
|
||||
return await cls.query.where(
|
||||
(cls.local_id == local_id) & (cls.is_r18 == flag)
|
||||
).gino.first()
|
||||
if r18 == 0:
|
||||
query = cls.query.where(cls.is_r18 == False)
|
||||
elif r18 == 1:
|
||||
query = cls.query.where(cls.is_r18 == True)
|
||||
else:
|
||||
query = cls.query
|
||||
return await cls.filter(is_r18=r18, local_id=local_id).first()
|
||||
query = cls.filter(is_r18=r18)
|
||||
if tags:
|
||||
for tag in tags:
|
||||
query = query.where(cls.tags.contains(tag) | cls.title.contains(tag) | cls.author.contains(tag))
|
||||
query = query.order_by(db.func.random()).limit(limit)
|
||||
return await query.gino.all()
|
||||
|
||||
@classmethod
|
||||
async def get_image_count(cls, r18: int = 0) -> int:
|
||||
"""
|
||||
说明:
|
||||
查询图片数量
|
||||
"""
|
||||
flag = False if r18 == 0 else True
|
||||
setattr(Setu, 'count', db.func.count(cls.local_id).label('count'))
|
||||
count = await cls.select('count').where(cls.is_r18 == flag).gino.first()
|
||||
return count[0]
|
||||
|
||||
@classmethod
|
||||
async def get_image_in_hash(cls, img_hash: str) -> "Setu":
|
||||
"""
|
||||
说明:
|
||||
通过图像hash获取图像信息
|
||||
参数:
|
||||
:param img_hash: = 图像hash值
|
||||
"""
|
||||
query = await cls.query.where(cls.img_hash == img_hash).gino.first()
|
||||
return query
|
||||
|
||||
@classmethod
|
||||
async def _check_exists(cls, pid: int, img_url: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
检测图片是否存在
|
||||
参数:
|
||||
:param pid: 图片pid
|
||||
:param img_url: 图片链接
|
||||
"""
|
||||
return bool(
|
||||
await cls.query.where(
|
||||
(cls.pid == pid) & (cls.img_url == img_url)
|
||||
).gino.first()
|
||||
)
|
||||
query = query.filter(
|
||||
Q(tags__contains=tag)
|
||||
| Q(title__contains=tag)
|
||||
| Q(author__contains=tag)
|
||||
)
|
||||
query = query.annotate(rand=Random()).limit(limit)
|
||||
return await query.all()
|
||||
|
||||
@classmethod
|
||||
async def delete_image(cls, pid: int) -> int:
|
||||
@@ -132,66 +71,14 @@ class Setu(db.Model):
|
||||
参数:
|
||||
:param pid: 图片pid
|
||||
"""
|
||||
query = await cls.query.where(cls.pid == pid).gino.first()
|
||||
return_id = -1
|
||||
query = await cls.get_or_none(pid=pid)
|
||||
if query:
|
||||
is_r18 = query.is_r18
|
||||
num = await cls.get_image_count(is_r18)
|
||||
x = await cls.query.where((cls.is_r18 == is_r18) & (cls.local_id == num - 1)).gino.first()
|
||||
_tmp_local_id = x.local_id
|
||||
if x:
|
||||
x.update(local_id=query.local_id).apply()
|
||||
await cls.delete.where(cls.pid == pid).gino.status()
|
||||
return _tmp_local_id
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
async def update_setu_data(
|
||||
cls,
|
||||
pid: int,
|
||||
*,
|
||||
local_id: Optional[int] = None,
|
||||
title: Optional[str] = None,
|
||||
author: Optional[str] = None,
|
||||
img_hash: Optional[str] = None,
|
||||
img_url: Optional[str] = None,
|
||||
tags: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
根据PID修改图片数据
|
||||
参数:
|
||||
:param local_id: 本地id
|
||||
:param pid: 图片pid
|
||||
:param title: 标题
|
||||
:param author: 作者
|
||||
:param img_hash: 图片hash值
|
||||
:param img_url: 图片链接
|
||||
:param tags: 图片标签
|
||||
"""
|
||||
query = cls.query.where(cls.pid == pid).with_for_update()
|
||||
image_list = await query.gino.all()
|
||||
if image_list:
|
||||
for image in image_list:
|
||||
if local_id:
|
||||
await image.update(local_id=local_id).apply()
|
||||
if title:
|
||||
await image.update(title=title).apply()
|
||||
if author:
|
||||
await image.update(author=author).apply()
|
||||
if img_hash:
|
||||
await image.update(img_hash=img_hash).apply()
|
||||
if img_url:
|
||||
await image.update(img_url=img_url).apply()
|
||||
if tags:
|
||||
await image.update(tags=tags).apply()
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_all_setu(cls) -> List["Setu"]:
|
||||
"""
|
||||
说明:
|
||||
获取所有图片对象
|
||||
"""
|
||||
return await cls.query.gino.all()
|
||||
|
||||
num = await cls.filter(is_r18=query.is_r18).count()
|
||||
last_image = await cls.get_or_none(is_r18=query.is_r18, local_id=num - 1)
|
||||
if last_image:
|
||||
return_id = last_image.local_id
|
||||
last_image.local_id = query.local_id
|
||||
await last_image.save(update_fields=["local_id"])
|
||||
await query.delete()
|
||||
return return_id
|
||||
|
||||
@@ -1,42 +1,40 @@
|
||||
import random
|
||||
from nonebot import on_command, on_regex
|
||||
from services.log import logger
|
||||
from models.sign_group_user import SignGroupUser
|
||||
from nonebot.message import run_postprocessor
|
||||
from nonebot.matcher import Matcher
|
||||
from typing import Optional, Type, Any
|
||||
from gino.exceptions import UninitializedError
|
||||
import re
|
||||
from typing import Any, Optional, Tuple, Type
|
||||
|
||||
from utils.message_builder import custom_forward_msg
|
||||
from utils.utils import (
|
||||
is_number,
|
||||
)
|
||||
from nonebot.typing import T_State
|
||||
from nonebot import on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
ActionFailed,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
PrivateMessageEvent,
|
||||
Message,
|
||||
Bot,
|
||||
Event,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
MessageEvent,
|
||||
PrivateMessageEvent,
|
||||
)
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_postprocessor
|
||||
from nonebot.params import Command, CommandArg, RegexGroup
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from models.sign_group_user import SignGroupUser
|
||||
from services.log import logger
|
||||
from utils.manager import withdraw_message_manager
|
||||
from utils.message_builder import custom_forward_msg
|
||||
from utils.utils import is_number
|
||||
|
||||
from .._model import Setu
|
||||
from .data_source import (
|
||||
get_setu_list,
|
||||
get_luoxiang,
|
||||
search_online_setu,
|
||||
get_setu_urls,
|
||||
add_data_to_database,
|
||||
check_local_exists_or_download,
|
||||
find_img_index,
|
||||
gen_message,
|
||||
check_local_exists_or_download,
|
||||
add_data_to_database,
|
||||
get_setu_count,
|
||||
get_luoxiang,
|
||||
get_setu_list,
|
||||
get_setu_urls,
|
||||
search_online_setu,
|
||||
)
|
||||
from configs.config import Config, NICKNAME
|
||||
from utils.manager import withdraw_message_manager
|
||||
from nonebot.params import CommandArg, Command, RegexGroup
|
||||
from typing import Tuple
|
||||
import re
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -126,7 +124,7 @@ async def do_something(
|
||||
await add_data_to_database(setu_data_list)
|
||||
logger.info("色图数据自动存储数据库成功...")
|
||||
setu_data_list = []
|
||||
except UninitializedError:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -146,17 +144,18 @@ async def _(
|
||||
):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
impression = (
|
||||
await SignGroupUser.ensure(event.user_id, event.group_id)
|
||||
).impression
|
||||
user, _ = await SignGroupUser.get_or_create(
|
||||
user_qq=event.user_id, group_id=event.group_id
|
||||
)
|
||||
impression = user.impression
|
||||
luox = get_luoxiang(impression)
|
||||
if luox:
|
||||
await setu.finish(luox)
|
||||
r18 = 0
|
||||
r18 = False
|
||||
num = 1
|
||||
# 是否看r18
|
||||
if cmd[0] == "色图r" and isinstance(event, PrivateMessageEvent):
|
||||
r18 = 1
|
||||
r18 = True
|
||||
num = 10
|
||||
elif cmd[0] == "色图r" and isinstance(event, GroupMessageEvent):
|
||||
if not Config.get_config("send_setu", "ALLOW_GROUP_R18"):
|
||||
@@ -164,18 +163,20 @@ async def _(
|
||||
random.choice(["这种不好意思的东西怎么可能给这么多人看啦", "羞羞脸!给我滚出克私聊!", "变态变态变态变态大变态!"])
|
||||
)
|
||||
else:
|
||||
r18 = 1
|
||||
r18 = False
|
||||
# 有 数字 的话先尝试本地色图id
|
||||
if msg and is_number(msg):
|
||||
setu_list, code = await get_setu_list(int(msg), r18=r18)
|
||||
if code != 200:
|
||||
await setu.finish(setu_list[0], at_sender=True)
|
||||
setu_img, code = await check_local_exists_or_download(setu_list[0])
|
||||
msg_id = await setu.send(gen_message(setu_list[0]) + setu_img, at_sender=True)
|
||||
msg_id = await setu.send(
|
||||
Message(gen_message(setu_list[0])) + setu_img, at_sender=True
|
||||
)
|
||||
logger.info(
|
||||
f"(USER {event.user_id}, GROUP "
|
||||
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 发送色图 {setu_list[0].local_id}.png"
|
||||
f" 发送色图 {setu_list[0].local_id}.jpd"
|
||||
)
|
||||
if msg_id:
|
||||
withdraw_message_manager.withdraw_message(
|
||||
@@ -205,9 +206,10 @@ num_key = {
|
||||
@setu_reg.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, reg_group: Tuple[Any, ...] = RegexGroup()):
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
impression = (
|
||||
await SignGroupUser.ensure(event.user_id, event.group_id)
|
||||
).impression
|
||||
user, _ = await SignGroupUser.get_or_create(
|
||||
user_qq=event.user_id, group_id=event.group_id
|
||||
)
|
||||
impression = user.impression
|
||||
luox = get_luoxiang(impression)
|
||||
if luox:
|
||||
await setu.finish(luox, at_sender=True)
|
||||
@@ -220,7 +222,7 @@ async def _(bot: Bot, event: MessageEvent, reg_group: Tuple[Any, ...] = RegexGro
|
||||
num = int(num)
|
||||
except ValueError:
|
||||
num = 1
|
||||
await send_setu_handle(bot, setu_reg, event, "色图", tags, num, 0)
|
||||
await send_setu_handle(bot, setu_reg, event, "色图", tags, num, False)
|
||||
|
||||
|
||||
async def send_setu_handle(
|
||||
@@ -230,7 +232,7 @@ async def send_setu_handle(
|
||||
command: str,
|
||||
msg: str,
|
||||
num: int,
|
||||
r18: int,
|
||||
r18: bool,
|
||||
):
|
||||
global setu_data_list
|
||||
# 非 id,在线搜索
|
||||
@@ -240,7 +242,7 @@ async def send_setu_handle(
|
||||
await matcher.finish("咳咳咳,虽然我很可爱,但是我木有自己的色图~~~有的话记得发我一份呀")
|
||||
# 本地先拿图,下载失败补上去
|
||||
setu_list, code = None, 200
|
||||
setu_count = await get_setu_count(r18)
|
||||
setu_count = await Setu.filter(is_r18=r18).count()
|
||||
if (
|
||||
not Config.get_config("send_setu", "ONLY_USE_LOCAL_SETU") and tags
|
||||
) or setu_count <= 0:
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
from configs.path_config import IMAGE_PATH, TEMP_PATH
|
||||
from utils.message_builder import image
|
||||
from services.log import logger
|
||||
from utils.image_utils import get_img_hash, compressed_image
|
||||
from utils.utils import change_img_md5
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from typing import List, Optional
|
||||
from configs.config import NICKNAME, Config
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from .._model import Setu
|
||||
import asyncio
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from typing import List, Optional, Tuple, Union
|
||||
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from nonebot.adapters.onebot.v11 import Message, MessageSegment
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from configs.path_config import IMAGE_PATH, TEMP_PATH
|
||||
from services.log import logger
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.image_utils import compressed_image, get_img_hash
|
||||
from utils.message_builder import image
|
||||
from utils.utils import change_img_md5
|
||||
|
||||
from .._model import Setu
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -28,11 +32,11 @@ host_pattern = re.compile(r"https?://([^/]+)")
|
||||
|
||||
# 获取url
|
||||
async def get_setu_urls(
|
||||
tags: List[str], num: int = 1, r18: int = 0, command: str = ""
|
||||
) -> "List[str], List[str], List[tuple], int":
|
||||
tags: List[str], num: int = 1, r18: bool = False, command: str = ""
|
||||
) -> Tuple[List[str], List[str], List[tuple], int]:
|
||||
tags = tags[:3] if len(tags) > 3 else tags
|
||||
params = {
|
||||
"r18": r18, # 添加r18参数 0为否,1为是,2为混合
|
||||
"r18": 1 if r18 else 0, # 添加r18参数 0为否,1为是,2为混合
|
||||
"tag": tags, # 若指定tag
|
||||
"num": 20, # 一次返回的结果数量
|
||||
"size": ["original"],
|
||||
@@ -82,7 +86,7 @@ headers = {
|
||||
|
||||
async def search_online_setu(
|
||||
url_: str, id_: Optional[int] = None, path_: Optional[str] = None
|
||||
) -> "MessageSegment, int":
|
||||
) -> Tuple[Union[MessageSegment, str], int]:
|
||||
"""
|
||||
下载色图
|
||||
:param url_: 色图url
|
||||
@@ -108,10 +112,7 @@ async def search_online_setu(
|
||||
):
|
||||
continue
|
||||
if id_ is not None:
|
||||
if (
|
||||
os.path.getsize(path_ / f"{index}.jpg")
|
||||
> 1024 * 1024 * 1.5
|
||||
):
|
||||
if os.path.getsize(path_ / f"{index}.jpg") > 1024 * 1024 * 1.5:
|
||||
compressed_image(
|
||||
path_ / f"{index}.jpg",
|
||||
)
|
||||
@@ -126,7 +127,9 @@ async def search_online_setu(
|
||||
|
||||
|
||||
# 检测本地是否有id涩图,无的话则下载
|
||||
async def check_local_exists_or_download(setu_image: Setu) -> "MessageSegment, int":
|
||||
async def check_local_exists_or_download(
|
||||
setu_image: Setu,
|
||||
) -> Tuple[MessageSegment, int]:
|
||||
path_ = None
|
||||
id_ = None
|
||||
if Config.get_config("send_setu", "DOWNLOAD_SETU"):
|
||||
@@ -148,27 +151,28 @@ async def add_data_to_database(lst: List[tuple]):
|
||||
if tmp:
|
||||
for x in tmp:
|
||||
try:
|
||||
r18 = 1 if "R-18" in x[5] else 0
|
||||
idx = await Setu.get_image_count(r18)
|
||||
await Setu.add_setu_data(
|
||||
idx,
|
||||
x[0],
|
||||
x[1],
|
||||
x[2],
|
||||
x[3],
|
||||
x[4],
|
||||
x[5],
|
||||
)
|
||||
idx = await Setu.filter(is_r18="R-18" in x[5]).count()
|
||||
if not await Setu.exists(pid=x[2], img_url=x[4]):
|
||||
await Setu.create(
|
||||
local_id=idx,
|
||||
title=x[0],
|
||||
author=x[1],
|
||||
pid=x[2],
|
||||
img_hash=x[3],
|
||||
img_url=x[4],
|
||||
tags=x[5],
|
||||
is_r18="R-18" in x[5],
|
||||
)
|
||||
except UniqueViolationError:
|
||||
pass
|
||||
|
||||
|
||||
# 拿到本地色图列表
|
||||
async def get_setu_list(
|
||||
index: Optional[int] = None, tags: Optional[List[str]] = None, r18: int = 0
|
||||
) -> "list, int":
|
||||
index: Optional[int] = None, tags: Optional[List[str]] = None, r18: bool = False
|
||||
) -> Tuple[list, int]:
|
||||
if index:
|
||||
image_count = await Setu.get_image_count(r18) - 1
|
||||
image_count = await Setu.filter(is_r18=r18).count() - 1
|
||||
if index < 0 or index > image_count:
|
||||
return [f"超过当前上下限!({image_count})"], 999
|
||||
image_list = [await Setu.query_image(index, r18=r18)]
|
||||
@@ -182,44 +186,44 @@ async def get_setu_list(
|
||||
|
||||
|
||||
# 初始化消息
|
||||
def gen_message(setu_image: Setu, img_msg: bool = False) -> str:
|
||||
def gen_message(
|
||||
setu_image: Setu, img_msg: bool = False
|
||||
) -> Union[Message, MessageSegment]:
|
||||
local_id = setu_image.local_id
|
||||
title = setu_image.title
|
||||
author = setu_image.author
|
||||
pid = setu_image.pid
|
||||
path_ = r18_path if setu_image.is_r18 else path
|
||||
image_path = IMAGE_PATH / path_ / f"{local_id}.jpg"
|
||||
if Config.get_config("send_setu", "SHOW_INFO"):
|
||||
return (
|
||||
return Message(
|
||||
f"id:{local_id}\n"
|
||||
f"title:{title}\n"
|
||||
f"author:{author}\n"
|
||||
f"PID:{pid}\n"
|
||||
f"{image(f'{local_id}', f'{r18_path if setu_image.is_r18 else path}') if img_msg else ''}"
|
||||
f"PID:{pid}\n" + image(image_path)
|
||||
)
|
||||
return f"{image(f'{local_id}', f'{r18_path if setu_image.is_r18 else path}') if img_msg else ''}"
|
||||
return image(image_path)
|
||||
|
||||
|
||||
# 罗翔老师!
|
||||
def get_luoxiang(impression):
|
||||
probability = (
|
||||
impression + Config.get_config("send_setu", "INITIAL_SETU_PROBABILITY") * 100
|
||||
float(impression)
|
||||
+ Config.get_config("send_setu", "INITIAL_SETU_PROBABILITY") * 100
|
||||
)
|
||||
if probability < random.randint(1, 101):
|
||||
return (
|
||||
"我为什么要给你发这个?"
|
||||
+ image(random.choice(os.listdir(IMAGE_PATH / "luoxiang")), "luoxiang")
|
||||
+ image(
|
||||
IMAGE_PATH
|
||||
/ "luoxiang"
|
||||
/ random.choice(os.listdir(IMAGE_PATH / "luoxiang"))
|
||||
)
|
||||
+ f"\n(快向{NICKNAME}签到提升好感度吧!)"
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
async def get_setu_count(r18: int) -> int:
|
||||
"""
|
||||
获取色图数量
|
||||
:param r18: r18类型
|
||||
"""
|
||||
return await Setu.get_image_count(r18)
|
||||
|
||||
|
||||
async def find_img_index(img_url, user_id):
|
||||
if not await AsyncHttpx.download_file(
|
||||
img_url,
|
||||
@@ -228,8 +232,7 @@ async def find_img_index(img_url, user_id):
|
||||
):
|
||||
return "检索图片下载上失败..."
|
||||
img_hash = str(get_img_hash(TEMP_PATH / f"{user_id}_find_setu_index.jpg"))
|
||||
setu_img = await Setu.get_image_in_hash(img_hash)
|
||||
if setu_img:
|
||||
if setu_img := await Setu.get_or_none(img_hash=img_hash):
|
||||
return (
|
||||
f"id:{setu_img.local_id}\n"
|
||||
f"title:{setu_img.title}\n"
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
from configs.path_config import IMAGE_PATH, TEXT_PATH, TEMP_PATH
|
||||
from services.log import logger
|
||||
from datetime import datetime
|
||||
from utils.image_utils import compressed_image, get_img_hash
|
||||
from utils.utils import get_bot
|
||||
from PIL import UnidentifiedImageError
|
||||
from .._model import Setu
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from configs.config import Config
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from nonebot import Driver
|
||||
import nonebot
|
||||
import os
|
||||
import ujson as json
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
import nonebot
|
||||
import ujson as json
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from nonebot import Driver
|
||||
from PIL import UnidentifiedImageError
|
||||
|
||||
from configs.config import Config
|
||||
from configs.path_config import IMAGE_PATH, TEMP_PATH, TEXT_PATH
|
||||
from services.log import logger
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.image_utils import compressed_image, get_img_hash
|
||||
from utils.utils import change_pixiv_image_links, get_bot
|
||||
|
||||
from .._model import Setu
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
@@ -47,15 +50,17 @@ async def update_old_setu_data():
|
||||
)
|
||||
# idx = r18_index if 'R-18' in data[x]["tags"] else index
|
||||
try:
|
||||
await Setu.add_setu_data(
|
||||
idx,
|
||||
data[x]["title"],
|
||||
data[x]["author"],
|
||||
data[x]["pid"],
|
||||
data[x]["img_hash"],
|
||||
img_url,
|
||||
",".join(data[x]["tags"]),
|
||||
)
|
||||
if not await Setu.exists(pid=data[x]["pid"], url=img_url):
|
||||
await Setu.create(
|
||||
local_id=idx,
|
||||
title=data[x]["title"],
|
||||
author=data[x]["author"],
|
||||
pid=data[x]["pid"],
|
||||
img_hash=data[x]["img_hash"],
|
||||
img_url=img_url,
|
||||
is_r18="R-18" in data[x]["tags"],
|
||||
tags=",".join(data[x]["tags"]),
|
||||
)
|
||||
count += 1
|
||||
if "R-18" in data[x]["tags"]:
|
||||
r18_index += 1
|
||||
@@ -94,7 +99,7 @@ async def update_setu_img(flag: bool = False):
|
||||
更新色图
|
||||
:param flag: 是否手动更新
|
||||
"""
|
||||
image_list = await Setu.get_all_setu()
|
||||
image_list = await Setu.all().order_by("local_id")
|
||||
image_list.reverse()
|
||||
_success = 0
|
||||
error_info = []
|
||||
@@ -110,12 +115,7 @@ async def update_setu_img(flag: bool = False):
|
||||
temp_file = TEMP_PATH / f"{image.local_id}.jpg"
|
||||
if temp_file.exists():
|
||||
temp_file.unlink()
|
||||
url_ = image.img_url
|
||||
ws_url = Config.get_config("pixiv", "PIXIV_NGINX_URL")
|
||||
if ws_url:
|
||||
url_ = url_.replace("i.pximg.net", ws_url).replace(
|
||||
"i.pixiv.cat", ws_url
|
||||
)
|
||||
url_ = change_pixiv_image_links(image.img_url)
|
||||
try:
|
||||
if not await AsyncHttpx.download_file(
|
||||
url_, TEMP_PATH / f"{image.local_id}.jpg"
|
||||
@@ -146,15 +146,20 @@ async def update_setu_img(flag: bool = False):
|
||||
logger.warning(f"文件 {image.local_id}.jpg 不存在,跳过...")
|
||||
continue
|
||||
img_hash = str(get_img_hash(f"{path}/{image.local_id}.jpg"))
|
||||
await Setu.update_setu_data(image.pid, img_hash=img_hash)
|
||||
image.img_hash = img_hash
|
||||
await image.save(update_fields=["img_hash"])
|
||||
# await Setu.update_setu_data(image.pid, img_hash=img_hash)
|
||||
except UnidentifiedImageError:
|
||||
# 图片已删除
|
||||
with open(local_image, 'r') as f:
|
||||
if '404 Not Found' in f.read():
|
||||
max_num = await Setu.delete_image(image.pid)
|
||||
local_image.unlink()
|
||||
os.rename(path / f"{max_num}.jpg", local_image)
|
||||
logger.warning(f"更新色图 PID:{image.pid} 404,已删除并替换")
|
||||
unlink = False
|
||||
with open(local_image, "r") as f:
|
||||
if "404 Not Found" in f.read():
|
||||
unlink = True
|
||||
if unlink:
|
||||
local_image.unlink()
|
||||
max_num = await Setu.delete_image(image.pid)
|
||||
os.rename(path / f"{max_num}.jpg", local_image)
|
||||
logger.warning(f"更新色图 PID:{image.pid} 404,已删除并替换")
|
||||
except Exception as e:
|
||||
_success -= 1
|
||||
logger.error(f"更新色图 {image.local_id}.jpg 错误 {type(e)}: {e}")
|
||||
|
||||
Reference in New Issue
Block a user