mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-03 19:00:00 +08:00
update 0.0.6.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from configs.config import HIBIAPI
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from models.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from .model.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from pathlib import Path
|
||||
from nonebot import Driver
|
||||
from typing import List
|
||||
@@ -14,10 +14,60 @@ __zx_plugin_name__ = "更新扩展图库Omega [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
"hibiapi",
|
||||
"HIBIAPI",
|
||||
"https://api.obfs.dev",
|
||||
help_="如果没有自建或其他hibiapi请不要修改",
|
||||
default_value="https://api.obfs.dev",
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pixiv",
|
||||
"PIXIV_NGINX_URL",
|
||||
"i.pixiv.re",
|
||||
help_="Pixiv反向代理"
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pix",
|
||||
"PIX_IMAGE_SIZE",
|
||||
"master",
|
||||
name="PIX图库",
|
||||
help_="PIX图库下载的画质 可能的值:original:原图,master:缩略图(加快发送速度)",
|
||||
default_value="master"
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pix",
|
||||
"SEARCH_HIBIAPI_BOOKMARKS",
|
||||
5000,
|
||||
help_="最低收藏,PIX使用HIBIAPI搜索图片时达到最低收藏才会添加至图库",
|
||||
default_value=5000
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pix",
|
||||
"WITHDRAW_PIX_MESSAGE",
|
||||
(0, 1),
|
||||
help_="自动撤回,参1:延迟撤回色图时间(秒),0 为关闭 | 参2:监控聊天类型,0(私聊) 1(群聊) 2(群聊+私聊)",
|
||||
default_value=(0, 1)
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pix",
|
||||
"PIX_OMEGA_PIXIV_RATIO",
|
||||
(10, 0),
|
||||
help_="PIX图库 与 额外图库OmegaPixivIllusts 混合搜索的比例 参1:PIX图库 参2:OmegaPixivIllusts扩展图库(没有此图库请设置为0)",
|
||||
default_value=(10, 0)
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"pix",
|
||||
"TIMEOUT",
|
||||
10,
|
||||
help_="下载图片超时限制(秒)",
|
||||
default_value=10
|
||||
)
|
||||
|
||||
nonebot.load_plugins("plugins/pix_gallery")
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
illust_url = f"{HIBIAPI}/api/pixiv/illust"
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
|
||||
@@ -4,17 +4,17 @@ from aiohttp.client_exceptions import (
|
||||
ClientConnectorError,
|
||||
)
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from models.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from .model.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from asyncio.locks import Semaphore
|
||||
from aiohttp import ClientPayloadError
|
||||
from aiohttp.client import ClientSession
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from models.pixiv import Pixiv
|
||||
from .model.pixiv import Pixiv
|
||||
from typing import List
|
||||
from utils.utils import get_local_proxy, change_picture_links
|
||||
from utils.image_utils import CreateImg
|
||||
from services.log import logger
|
||||
from configs.config import HIBIAPI_BOOKMARKS, HIBIAPI, PIX_IMAGE_SIZE
|
||||
from configs.config import Config
|
||||
from configs.path_config import TEMP_PATH
|
||||
import platform
|
||||
import aiohttp
|
||||
@@ -31,36 +31,45 @@ if str(platform.system()).lower() == "windows":
|
||||
policy = asyncio.WindowsSelectorEventLoopPolicy()
|
||||
asyncio.set_event_loop_policy(policy)
|
||||
|
||||
search_url = f"{HIBIAPI}/api/pixiv/search"
|
||||
member_illust_url = f"{HIBIAPI}/api/pixiv/member_illust"
|
||||
illust_url = f"{HIBIAPI}/api/pixiv/illust"
|
||||
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6;"
|
||||
" rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
|
||||
"Referer": "https://www.pixiv.net",
|
||||
}
|
||||
|
||||
HIBIAPI = None
|
||||
|
||||
# 开始更新
|
||||
async def start_update_image_url(current_keyword: List[str], black_pid: List[str]):
|
||||
|
||||
async def start_update_image_url(
|
||||
current_keyword: List[str], black_pid: List[str]
|
||||
) -> "int, int":
|
||||
"""
|
||||
开始更新图片url
|
||||
:param current_keyword: 关键词
|
||||
:param black_pid: 黑名单pid
|
||||
:return: pid数量和图片数量
|
||||
"""
|
||||
global HIBIAPI
|
||||
pid_count = 0
|
||||
pic_count = 0
|
||||
tasks = []
|
||||
semaphore = asyncio.Semaphore(10)
|
||||
if not HIBIAPI:
|
||||
HIBIAPI = Config.get_config("hibiapi", "HIBIAPI")
|
||||
HIBIAPI = HIBIAPI[:-1] if HIBIAPI[-1] else HIBIAPI
|
||||
async with aiohttp.ClientSession(headers=headers) as session:
|
||||
for keyword in current_keyword:
|
||||
for page in range(1, 110):
|
||||
if keyword.startswith("uid:"):
|
||||
url = member_illust_url
|
||||
url = f"{HIBIAPI}/api/pixiv/member_illust"
|
||||
params = {"id": keyword[4:], "page": page}
|
||||
if page == 30:
|
||||
break
|
||||
elif keyword.startswith("pid:"):
|
||||
url = illust_url
|
||||
url = f"{HIBIAPI}/api/pixiv/illust"
|
||||
params = {"id": keyword[4:]}
|
||||
else:
|
||||
url = search_url
|
||||
url = f"{HIBIAPI}/api/pixiv/search"
|
||||
params = {"word": keyword, "page": page}
|
||||
tasks.append(
|
||||
asyncio.ensure_future(
|
||||
@@ -86,7 +95,18 @@ async def search_image(
|
||||
session: ClientSession,
|
||||
page: int = 1,
|
||||
black: List[str] = None,
|
||||
):
|
||||
) -> "int, int":
|
||||
"""
|
||||
搜索图片
|
||||
:param url: 搜索url
|
||||
:param keyword: 关键词
|
||||
:param params: params参数
|
||||
:param semaphore: semaphore
|
||||
:param session: session
|
||||
:param page: 页面
|
||||
:param black: pid黑名单
|
||||
:return: pid数量和图片数量
|
||||
"""
|
||||
tmp_pid = []
|
||||
pic_count = 0
|
||||
pid_count = 0
|
||||
@@ -104,7 +124,7 @@ async def search_image(
|
||||
or (not data.get("illusts") and not data.get("illust"))
|
||||
):
|
||||
return 0, 0
|
||||
if url != illust_url:
|
||||
if url != f"{HIBIAPI}/api/pixiv/illust":
|
||||
logger.info(f'{keyword}: 获取数据成功...数据总量:{len(data["illusts"])}')
|
||||
data = data["illusts"]
|
||||
else:
|
||||
@@ -133,9 +153,13 @@ async def search_image(
|
||||
img_urls.append(urls["image_urls"]["original"])
|
||||
if (
|
||||
(
|
||||
bookmarks >= HIBIAPI_BOOKMARKS
|
||||
or (url == member_illust_url and bookmarks >= 1500)
|
||||
or (url == illust_url)
|
||||
bookmarks
|
||||
>= Config.get_config("pix", "SEARCH_HIBIAPI_BOOKMARKS")
|
||||
or (
|
||||
url == f"{HIBIAPI}/api/pixiv/member_illust"
|
||||
and bookmarks >= 1500
|
||||
)
|
||||
or (url == f"{HIBIAPI}/api/pixiv/illust")
|
||||
)
|
||||
and len(img_urls) < 10
|
||||
and _check_black(img_urls, black)
|
||||
@@ -177,13 +201,8 @@ async def search_image(
|
||||
tmp_pid.append(data["pid"])
|
||||
pic_count += 1
|
||||
logger.info(f'存储图片PID:{data["pid"]} IMG_P:{img_p}')
|
||||
# await download_image(img_url, session)
|
||||
except UniqueViolationError:
|
||||
logger.warning(f'{data["pid"]} | {img_url} 已存在...')
|
||||
# 下载图片
|
||||
# if not os.path.exists(f'{image_path}/
|
||||
# {img_url[img_url.rfind("/") + 1:]}'):
|
||||
# await download_image(img_url, session)
|
||||
except (ServerDisconnectedError, ClientConnectorError, ClientOSError):
|
||||
logger.warning("搜索图片服务被关闭,再次调用....")
|
||||
await search_image(url, keyword, params, semaphore, session, page, black)
|
||||
@@ -192,22 +211,26 @@ async def search_image(
|
||||
|
||||
# 下载图片
|
||||
async def download_image(img_url: str, session: ClientSession, _count: int = 1):
|
||||
"""
|
||||
下载图片
|
||||
:param img_url: 图片url
|
||||
:param session: session
|
||||
:param _count: 次数
|
||||
"""
|
||||
try:
|
||||
async with session.get(img_url, proxy=get_local_proxy()) as response:
|
||||
logger.info(f"下载图片 --> {img_url}")
|
||||
async with aiofiles.open(f'tmp/{img_url.split("/")[-1]}', "wb") as f:
|
||||
await f.write(await response.read())
|
||||
# async with semaphore:
|
||||
# await asyncio.get_event_loop().run_in_executor(None,
|
||||
# upload_image, img_url[img_url.rfind("/")+1:])
|
||||
except ServerDisconnectedError:
|
||||
logger.warning(f"下载图片服务被关闭,第 {_count} 次调用....")
|
||||
await download_image(img_url, session, _count + 1)
|
||||
except ClientOSError:
|
||||
logger.warning(f"远程连接被关闭,第 {_count} 次调用....")
|
||||
await download_image(
|
||||
img_url.replace("i.pximg.net", "i.pixiv.cat"), session, _count + 1
|
||||
)
|
||||
ws_url = Config.get_config("pixiv", "PIXIV_NGINX_URL")
|
||||
if ws_url:
|
||||
img_url = img_url.replace("i.pximg.net", ws_url)
|
||||
await download_image(img_url, session, _count + 1)
|
||||
except TimeoutError:
|
||||
logger.warning(f"下载或写入超时,第 {_count} 次调用....")
|
||||
await download_image(img_url, session, _count + 1)
|
||||
@@ -216,34 +239,47 @@ async def download_image(img_url: str, session: ClientSession, _count: int = 1):
|
||||
|
||||
|
||||
async def get_image(img_url: str, user_id: int) -> str:
|
||||
"""
|
||||
下载图片
|
||||
:param img_url:
|
||||
:param user_id:
|
||||
:return: 图片名称
|
||||
"""
|
||||
async with aiohttp.ClientSession(headers=headers) as session:
|
||||
if 'https://www.pixiv.net/artworks' in img_url:
|
||||
pid = img_url.rsplit('/', maxsplit=1)[-1]
|
||||
if "https://www.pixiv.net/artworks" in img_url:
|
||||
pid = img_url.rsplit("/", maxsplit=1)[-1]
|
||||
params = {"id": pid}
|
||||
for _ in range(3):
|
||||
try:
|
||||
async with session.get(
|
||||
illust_url,
|
||||
f"{HIBIAPI}/api/pixiv/illust",
|
||||
params=params,
|
||||
proxy=get_local_proxy(),
|
||||
) as response:
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
if data.get('illust'):
|
||||
if data['illust']['page_count'] == 1:
|
||||
img_url = data['illust']['meta_single_page']['original_image_url']
|
||||
if data.get("illust"):
|
||||
if data["illust"]["page_count"] == 1:
|
||||
img_url = data["illust"]["meta_single_page"][
|
||||
"original_image_url"
|
||||
]
|
||||
else:
|
||||
img_url = data['illust']["meta_pages"][0]["image_urls"]["original"]
|
||||
img_url = data["illust"]["meta_pages"][0][
|
||||
"image_urls"
|
||||
]["original"]
|
||||
break
|
||||
except (ClientConnectorError, TimeoutError):
|
||||
pass
|
||||
old_img_url = img_url
|
||||
img_url = change_picture_links(img_url, PIX_IMAGE_SIZE)
|
||||
img_url = change_picture_links(
|
||||
img_url, Config.get_config("pix", "PIX_IMAGE_SIZE")
|
||||
)
|
||||
for _ in range(3):
|
||||
try:
|
||||
async with session.get(
|
||||
img_url,
|
||||
proxy=get_local_proxy(),
|
||||
timeout=Config.get_config("pix", "TIMEOUT"),
|
||||
) as response:
|
||||
if response.status == 404:
|
||||
img_url = old_img_url
|
||||
@@ -257,12 +293,15 @@ async def get_image(img_url: str, user_id: int) -> str:
|
||||
pass
|
||||
|
||||
|
||||
# 检测UID或PID是否有效
|
||||
async def uid_pid_exists(id_: str) -> bool:
|
||||
"""
|
||||
检测 pid/uid 是否有效
|
||||
:param id_: pid/uid
|
||||
"""
|
||||
if id_.startswith("uid:"):
|
||||
url = f"{HIBIAPI}/api/pixiv/member"
|
||||
elif id_.startswith("pid:"):
|
||||
url = illust_url
|
||||
url = f"{HIBIAPI}/api/pixiv/illust"
|
||||
else:
|
||||
return False
|
||||
params = {"id": int(id_[4:])}
|
||||
@@ -274,13 +313,24 @@ async def uid_pid_exists(id_: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def get_keyword_num(keyword: str) -> "int , int":
|
||||
async def get_keyword_num(keyword: str) -> "int, int, int, int, int":
|
||||
"""
|
||||
查看图片相关 tag 数量
|
||||
:param keyword: 关键词tag
|
||||
"""
|
||||
count, r18_count = await Pixiv.get_keyword_num(keyword.split())
|
||||
count_, setu_count, r18_count_ = await OmegaPixivIllusts.get_keyword_num(keyword.split())
|
||||
count_, setu_count, r18_count_ = await OmegaPixivIllusts.get_keyword_num(
|
||||
keyword.split()
|
||||
)
|
||||
return count, r18_count, count_, setu_count, r18_count_
|
||||
|
||||
|
||||
async def remove_image(pid: int, img_p: str) -> bool:
|
||||
"""
|
||||
删除置顶图片
|
||||
:param pid: pid
|
||||
:param img_p: 图片 p 如 p0,p1 等
|
||||
"""
|
||||
if img_p:
|
||||
if "p" not in img_p:
|
||||
img_p = f"p{img_p}"
|
||||
@@ -290,6 +340,12 @@ async def remove_image(pid: int, img_p: str) -> bool:
|
||||
def gen_keyword_pic(
|
||||
_pass_keyword: List[str], not_pass_keyword: List[str], is_superuser: bool
|
||||
):
|
||||
"""
|
||||
已通过或未通过的所有关键词/uid/pid
|
||||
:param _pass_keyword: 通过列表
|
||||
:param not_pass_keyword: 未通过列表
|
||||
:param is_superuser: 是否超级用户
|
||||
"""
|
||||
_keyword = [
|
||||
x
|
||||
for x in _pass_keyword
|
||||
@@ -373,10 +429,15 @@ def gen_keyword_pic(
|
||||
return A.pic2bs4()
|
||||
|
||||
|
||||
def _check_black(img_urls: List[str], black: List[str]):
|
||||
def _check_black(img_urls: List[str], black: List[str]) -> bool:
|
||||
"""
|
||||
检测pid是否在黑名单中
|
||||
:param img_urls: 图片img列表
|
||||
:param black: 黑名单
|
||||
:return:
|
||||
"""
|
||||
for b in black:
|
||||
for img_url in img_urls:
|
||||
# img_url = img_url[img_url.rfind('/')+1: img_url.rfind('.')]
|
||||
if b in img_url:
|
||||
return False
|
||||
return True
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
from .pixiv_keyword_user import *
|
||||
from .omega_pixiv_illusts import *
|
||||
from .pixiv import *
|
||||
@@ -0,0 +1,141 @@
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from services.db_context import db
|
||||
|
||||
|
||||
class OmegaPixivIllusts(db.Model):
|
||||
__tablename__ = "omega_pixiv_illusts"
|
||||
__table_args__ = {'extend_existing': True}
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
pid = db.Column(db.BigInteger(), nullable=False)
|
||||
uid = db.Column(db.BigInteger(), nullable=False)
|
||||
title = db.Column(db.String(), nullable=False)
|
||||
uname = db.Column(db.String(), nullable=False)
|
||||
nsfw_tag = db.Column(db.Integer(), nullable=False)
|
||||
width = db.Column(db.Integer(), nullable=False)
|
||||
height = db.Column(db.Integer(), nullable=False)
|
||||
tags = db.Column(db.String(), nullable=False)
|
||||
url = db.Column(db.String(), nullable=False)
|
||||
created_at = db.Column(db.DateTime(timezone=True))
|
||||
updated_at = db.Column(db.DateTime(timezone=True))
|
||||
|
||||
_idx1 = db.Index("omega_pixiv_illusts_idx1", "pid", "url", unique=True)
|
||||
|
||||
@classmethod
|
||||
async def add_image_data(
|
||||
cls,
|
||||
pid: int,
|
||||
title: str,
|
||||
width: int,
|
||||
height: int,
|
||||
url: str,
|
||||
uid: int,
|
||||
uname: str,
|
||||
nsfw_tag: int,
|
||||
tags: str,
|
||||
created_at: datetime,
|
||||
updated_at: datetime,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
添加图片信息
|
||||
参数:
|
||||
:param pid: pid
|
||||
:param title: 标题
|
||||
:param width: 宽度
|
||||
:param height: 长度
|
||||
:param url: url链接
|
||||
:param uid: 作者uid
|
||||
:param uname: 作者名称
|
||||
:param nsfw_tag: nsfw标签, 0=safe, 1=setu. 2=r18
|
||||
:param tags: 相关tag
|
||||
:param created_at: 创建日期
|
||||
:param updated_at: 更新日期
|
||||
"""
|
||||
if not await cls.check_exists(pid):
|
||||
await cls.create(
|
||||
pid=pid,
|
||||
title=title,
|
||||
width=width,
|
||||
height=height,
|
||||
url=url,
|
||||
uid=uid,
|
||||
uname=uname,
|
||||
nsfw_tag=nsfw_tag,
|
||||
tags=tags,
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def query_images(
|
||||
cls,
|
||||
keywords: Optional[List[str]] = None,
|
||||
uid: Optional[int] = None,
|
||||
pid: Optional[int] = None,
|
||||
nsfw_tag: Optional[int] = 0,
|
||||
num: int = 100
|
||||
) -> List[Optional["OmegaPixivIllusts"]]:
|
||||
"""
|
||||
说明:
|
||||
查找符合条件的图片
|
||||
参数:
|
||||
:param keywords: 关键词
|
||||
:param uid: 画师uid
|
||||
:param pid: 图片pid
|
||||
:param nsfw_tag: nsfw标签, 0=safe, 1=setu. 2=r18
|
||||
:param num: 获取图片数量
|
||||
"""
|
||||
if nsfw_tag is not None:
|
||||
query = cls.query.where(cls.nsfw_tag == nsfw_tag)
|
||||
else:
|
||||
query = cls.query
|
||||
if keywords:
|
||||
for keyword in keywords:
|
||||
query = query.where(cls.tags.contains(keyword))
|
||||
elif uid:
|
||||
query = query.where(cls.uid == uid)
|
||||
elif pid:
|
||||
query = query.where(cls.uid == pid)
|
||||
query = query.order_by(db.func.random()).limit(num)
|
||||
return await query.gino.all()
|
||||
|
||||
@classmethod
|
||||
async def check_exists(cls, pid: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
检测pid是否已存在
|
||||
参数:
|
||||
:param pid: 图片PID
|
||||
"""
|
||||
query = await cls.query.where(cls.pid == pid).gino.all()
|
||||
return bool(query)
|
||||
|
||||
@classmethod
|
||||
async def get_keyword_num(cls, tags: List[str] = None) -> "int, int, int":
|
||||
"""
|
||||
说明:
|
||||
获取相关关键词(keyword, tag)在图库中的数量
|
||||
参数:
|
||||
:param tags: 关键词/Tag
|
||||
"""
|
||||
setattr(OmegaPixivIllusts, 'count', db.func.count(cls.pid).label('count'))
|
||||
query = cls.select('count')
|
||||
if tags:
|
||||
for tag in tags:
|
||||
query = query.where(cls.tags.contains(tag))
|
||||
count = await query.where(cls.nsfw_tag == 0).gino.first()
|
||||
setu_count = await query.where(cls.nsfw_tag == 1).gino.first()
|
||||
r18_count = await query.where(cls.nsfw_tag == 2).gino.first()
|
||||
return count[0], setu_count[0], r18_count[0]
|
||||
|
||||
@classmethod
|
||||
async def get_all_pid(cls) -> List[int]:
|
||||
"""
|
||||
说明:
|
||||
获取所有图片PID
|
||||
"""
|
||||
data = await cls.select('pid').gino.all()
|
||||
return [x[0] for x in data]
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
from typing import Optional, List
|
||||
from services.db_context import db
|
||||
|
||||
|
||||
class Pixiv(db.Model):
|
||||
__tablename__ = "pixiv"
|
||||
__table_args__ = {'extend_existing': True}
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
pid = db.Column(db.BigInteger(), nullable=False)
|
||||
title = db.Column(db.String(), nullable=False)
|
||||
width = db.Column(db.Integer(), nullable=False)
|
||||
height = db.Column(db.Integer(), nullable=False)
|
||||
view = db.Column(db.Integer(), nullable=False)
|
||||
bookmarks = db.Column(db.Integer(), nullable=False)
|
||||
img_url = db.Column(db.String(), nullable=False)
|
||||
img_p = db.Column(db.String(), nullable=False)
|
||||
uid = db.Column(db.BigInteger(), nullable=False)
|
||||
author = db.Column(db.String(), nullable=False)
|
||||
is_r18 = db.Column(db.Boolean(), nullable=False)
|
||||
tags = db.Column(db.String(), nullable=False)
|
||||
|
||||
_idx1 = db.Index("pixiv_idx1", "pid", "img_url", unique=True)
|
||||
|
||||
@classmethod
|
||||
async def add_image_data(
|
||||
cls,
|
||||
pid: int,
|
||||
title: str,
|
||||
width: int,
|
||||
height: int,
|
||||
view: int,
|
||||
bookmarks: int,
|
||||
img_url: str,
|
||||
img_p: str,
|
||||
uid: int,
|
||||
author: str,
|
||||
tags: str,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
添加图片信息
|
||||
参数:
|
||||
:param pid: pid
|
||||
:param title: 标题
|
||||
:param width: 宽度
|
||||
:param height: 长度
|
||||
:param view: 被查看次数
|
||||
:param bookmarks: 收藏数
|
||||
:param img_url: url链接
|
||||
:param img_p: 张数
|
||||
:param uid: 作者uid
|
||||
:param author: 作者名称
|
||||
:param tags: 相关tag
|
||||
"""
|
||||
if not await cls.check_exists(pid, img_p):
|
||||
await cls.create(
|
||||
pid=pid,
|
||||
title=title,
|
||||
width=width,
|
||||
height=height,
|
||||
view=view,
|
||||
bookmarks=bookmarks,
|
||||
img_url=img_url,
|
||||
img_p=img_p,
|
||||
uid=uid,
|
||||
author=author,
|
||||
is_r18=True if "R-18" in tags else False,
|
||||
tags=tags,
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def remove_image_data(cls, pid: int, img_p: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
删除图片数据
|
||||
参数:
|
||||
:param pid: 图片pid
|
||||
:param img_p: 图片pid的张数,如:p0,p1
|
||||
"""
|
||||
try:
|
||||
if img_p:
|
||||
await cls.delete.where(
|
||||
(cls.pid == pid) & (cls.img_p == img_p)
|
||||
).gino.status()
|
||||
else:
|
||||
await cls.delete.where(cls.pid == pid).gino.status()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_all_pid(cls) -> List[int]:
|
||||
"""
|
||||
说明:
|
||||
获取所有PID
|
||||
"""
|
||||
query = await cls.query.select("pid").gino.first()
|
||||
pid = [x[0] for x in query]
|
||||
return list(set(pid))
|
||||
|
||||
# 0:非r18 1:r18 2:混合
|
||||
@classmethod
|
||||
async def query_images(
|
||||
cls,
|
||||
keywords: Optional[List[str]] = None,
|
||||
uid: Optional[int] = None,
|
||||
pid: Optional[int] = None,
|
||||
r18: Optional[int] = 0,
|
||||
num: int = 100
|
||||
) -> List[Optional["Pixiv"]]:
|
||||
"""
|
||||
说明:
|
||||
查找符合条件的图片
|
||||
参数:
|
||||
:param keywords: 关键词
|
||||
:param uid: 画师uid
|
||||
:param pid: 图片pid
|
||||
:param r18: 是否r18,0:非r18 1:r18 2:混合
|
||||
:param num: 查找图片的数量
|
||||
"""
|
||||
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
|
||||
if keywords:
|
||||
for keyword in keywords:
|
||||
query = query.where(cls.tags.contains(keyword))
|
||||
elif uid:
|
||||
query = query.where(cls.uid == uid)
|
||||
elif pid:
|
||||
query = query.where(cls.pid == pid)
|
||||
query = query.order_by(db.func.random()).limit(num)
|
||||
return await query.gino.all()
|
||||
|
||||
@classmethod
|
||||
async def check_exists(cls, pid: int, img_p: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
检测pid是否已存在
|
||||
参数:
|
||||
:param pid: 图片PID
|
||||
:param img_p: 张数
|
||||
"""
|
||||
query = await cls.query.where(
|
||||
(cls.pid == pid) & (cls.img_p == img_p)
|
||||
).gino.all()
|
||||
return bool(query)
|
||||
|
||||
@classmethod
|
||||
async def get_keyword_num(cls, tags: List[str] = None) -> "int, int":
|
||||
"""
|
||||
说明:
|
||||
获取相关关键词(keyword, tag)在图库中的数量
|
||||
参数:
|
||||
:param tags: 关键词/Tag
|
||||
"""
|
||||
setattr(Pixiv, 'count', db.func.count(cls.pid).label('count'))
|
||||
query = cls.select('count')
|
||||
if tags:
|
||||
for tag in tags:
|
||||
query = query.where(cls.tags.contains(tag))
|
||||
count = await query.where(cls.is_r18 == False).gino.first()
|
||||
r18_count = await query.where(cls.is_r18 == True).gino.first()
|
||||
return count[0], r18_count[0]
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
from services.db_context import db
|
||||
from typing import Set, List
|
||||
|
||||
|
||||
class PixivKeywordUser(db.Model):
|
||||
__tablename__ = "pixiv_keyword_users"
|
||||
__table_args__ = {'extend_existing': True}
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger(), nullable=False)
|
||||
keyword = db.Column(db.String(), nullable=False)
|
||||
is_pass = db.Column(db.Boolean(), default=False)
|
||||
|
||||
_idx1 = db.Index("pixiv_keyword_users_idx1", "keyword", unique=True)
|
||||
|
||||
@classmethod
|
||||
async def add_keyword(
|
||||
cls, user_qq: int, group_id: int, keyword: str, superusers: Set[str]
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
添加搜图的关键词
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
:param keyword: 关键词
|
||||
:param superusers: 是否为超级用户
|
||||
"""
|
||||
is_pass = True if str(user_qq) in superusers else False
|
||||
if not await cls._check_keyword_exists(keyword):
|
||||
await cls.create(
|
||||
user_qq=user_qq, group_id=group_id, keyword=keyword, is_pass=is_pass
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def delete_keyword(cls, keyword: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
删除关键词
|
||||
参数:
|
||||
:param keyword: 关键词
|
||||
"""
|
||||
if await cls._check_keyword_exists(keyword):
|
||||
query = cls.query.where(cls.keyword == keyword).with_for_update()
|
||||
query = await query.gino.first()
|
||||
await query.delete()
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def set_keyword_pass(cls, keyword: str, is_pass: bool) -> "int, int":
|
||||
"""
|
||||
说明:
|
||||
通过或禁用关键词
|
||||
参数:
|
||||
:param keyword: 关键词
|
||||
:param is_pass: 通过状态
|
||||
"""
|
||||
if await cls._check_keyword_exists(keyword):
|
||||
query = cls.query.where(cls.keyword == keyword).with_for_update()
|
||||
query = await query.gino.first()
|
||||
await query.update(
|
||||
is_pass=is_pass,
|
||||
).apply()
|
||||
return query.user_qq, query.group_id
|
||||
return 0, 0
|
||||
|
||||
@classmethod
|
||||
async def get_all_user_dict(cls) -> dict:
|
||||
"""
|
||||
说明:
|
||||
获取关键词数据库各个用户贡献的关键词字典
|
||||
"""
|
||||
tmp = {}
|
||||
query = await cls.query.gino.all()
|
||||
for user in query:
|
||||
if not tmp.get(user.user_qq):
|
||||
tmp[user.user_qq] = {"keyword": []}
|
||||
tmp[user.user_qq]["keyword"].append(user.keyword)
|
||||
return tmp
|
||||
|
||||
@classmethod
|
||||
async def get_current_keyword(cls) -> "List[str], List[str]":
|
||||
"""
|
||||
说明:
|
||||
获取当前通过与未通过的关键词
|
||||
"""
|
||||
pass_keyword = []
|
||||
not_pass_keyword = []
|
||||
query = await cls.query.gino.all()
|
||||
for user in query:
|
||||
if user.is_pass:
|
||||
pass_keyword.append(user.keyword)
|
||||
else:
|
||||
not_pass_keyword.append(user.keyword)
|
||||
return pass_keyword, not_pass_keyword
|
||||
|
||||
@classmethod
|
||||
async def get_black_pid(cls) -> List[str]:
|
||||
"""
|
||||
说明:
|
||||
获取黑名单PID
|
||||
"""
|
||||
black_pid = []
|
||||
query = await cls.query.where(cls.user_qq == 114514).gino.all()
|
||||
for image in query:
|
||||
black_pid.append(image.keyword[6:])
|
||||
return black_pid
|
||||
|
||||
@classmethod
|
||||
async def _check_keyword_exists(cls, keyword: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
检测关键词是否已存在
|
||||
参数:
|
||||
:param keyword: 关键词
|
||||
"""
|
||||
current_keyword = []
|
||||
query = await cls.query.gino.all()
|
||||
for user in query:
|
||||
current_keyword.append(user.keyword)
|
||||
if keyword in current_keyword:
|
||||
return True
|
||||
return False
|
||||
+50
-50
@@ -1,19 +1,18 @@
|
||||
from nonebot.adapters.cqhttp.message import Message
|
||||
from utils.utils import get_message_text, is_number
|
||||
from configs.config import PIX_OMEGA_PIXIV_RATIO, WITHDRAW_PIX_TIME
|
||||
from models.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from configs.config import Config
|
||||
from .model.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from utils.message_builder import image
|
||||
from utils.manager import withdraw_message_manager
|
||||
from services.log import logger
|
||||
from nonebot.adapters.cqhttp import (
|
||||
Bot,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
PrivateMessageEvent,
|
||||
)
|
||||
from utils.manager import withdraw_message_manager
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import get_image
|
||||
from models.pixiv import Pixiv
|
||||
from .model.pixiv import Pixiv
|
||||
from nonebot import on_command
|
||||
import random
|
||||
|
||||
@@ -55,54 +54,66 @@ __plugin_block_limit__ = {"rst": "您有PIX图片正在处理,请稍等..."}
|
||||
pix = on_command("pix", aliases={"PIX", "Pix"}, priority=5, block=True)
|
||||
|
||||
|
||||
PIX_RATIO = PIX_OMEGA_PIXIV_RATIO[0] / (
|
||||
PIX_OMEGA_PIXIV_RATIO[0] + PIX_OMEGA_PIXIV_RATIO[1]
|
||||
)
|
||||
OMEGA_RATIO = 1 - PIX_RATIO
|
||||
PIX_RATIO = None
|
||||
OMEGA_RATIO = None
|
||||
|
||||
|
||||
@pix.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
global PIX_RATIO, OMEGA_RATIO
|
||||
if PIX_RATIO is None:
|
||||
pix_omega_pixiv_ratio = Config.get_config("pix", "PIX_OMEGA_PIXIV_RATIO")
|
||||
PIX_RATIO = pix_omega_pixiv_ratio[0] / (
|
||||
pix_omega_pixiv_ratio[0] + pix_omega_pixiv_ratio[1]
|
||||
)
|
||||
OMEGA_RATIO = 1 - PIX_RATIO
|
||||
num = 1
|
||||
keyword = get_message_text(event.json())
|
||||
x = keyword.split(" ")
|
||||
if "-s" in x:
|
||||
x.remove("-s")
|
||||
nsfw_tag = 1
|
||||
elif "-r" in x:
|
||||
x.remove("-r")
|
||||
nsfw_tag = 2
|
||||
else:
|
||||
nsfw_tag = 0
|
||||
if nsfw_tag != 0 and str(event.user_id) not in bot.config.superusers:
|
||||
await pix.finish("你不能看这些噢,这些都是是留给管理员看的...")
|
||||
if len(x) > 1:
|
||||
if is_number(x[-1]):
|
||||
num = int(x[-1])
|
||||
if num > 10:
|
||||
if str(event.user_id) not in bot.config.superusers or (
|
||||
str(event.user_id) in bot.config.superusers and num > 30
|
||||
):
|
||||
num = random.randint(1, 10)
|
||||
await pix.send(f"太贪心了,就给你发 {num}张 好了")
|
||||
x = x[:-1]
|
||||
keyword = " ".join(x)
|
||||
pix_num = int(num * PIX_RATIO) + 15 if PIX_RATIO != 0 else 0
|
||||
omega_num = num - pix_num + 15
|
||||
if is_number(keyword):
|
||||
all_image = await Pixiv.query_images(uid=int(keyword))
|
||||
if num == 1:
|
||||
pix_num = 15
|
||||
omega_num = 15
|
||||
all_image = await Pixiv.query_images(
|
||||
uid=int(keyword), num=pix_num, r18=1 if nsfw_tag == 2 else 0
|
||||
) + await OmegaPixivIllusts.query_images(
|
||||
uid=int(keyword), num=omega_num, nsfw_tag=nsfw_tag
|
||||
)
|
||||
elif keyword.lower().startswith("pid"):
|
||||
pid = keyword.replace("pid", "").replace(":", "")
|
||||
pid = keyword.replace("pid", "").replace(":", "").replace(":", "")
|
||||
if not is_number(pid):
|
||||
await pix.finish("PID必须是数字...", at_sender=True)
|
||||
isr18 = 0 if isinstance(event, GroupMessageEvent) else 2
|
||||
nsfw_tag = 0 if isinstance(event, GroupMessageEvent) else None
|
||||
all_image = await Pixiv.query_images(pid=int(pid), r18=isr18)
|
||||
all_image = await Pixiv.query_images(
|
||||
pid=int(pid), r18=1 if nsfw_tag == 2 else 0
|
||||
)
|
||||
if not all_image:
|
||||
all_image = await OmegaPixivIllusts.query_images(
|
||||
pid=int(pid), nsfw_tag=nsfw_tag
|
||||
)
|
||||
else:
|
||||
x = keyword.split()
|
||||
if "-s" in x:
|
||||
x.remove("-s")
|
||||
nsfw_tag = 1
|
||||
elif "-r" in x:
|
||||
x.remove("-r")
|
||||
nsfw_tag = 2
|
||||
else:
|
||||
nsfw_tag = 0
|
||||
if nsfw_tag != 0 and str(event.user_id) not in bot.config.superusers:
|
||||
await pix.finish("你不能看这些噢,这些都是是留给管理员看的...")
|
||||
if len(x) > 1:
|
||||
if is_number(x[-1]):
|
||||
num = int(x[-1])
|
||||
if num > 10:
|
||||
if str(event.user_id) not in bot.config.superusers or (
|
||||
str(event.user_id) in bot.config.superusers and num > 30
|
||||
):
|
||||
num = random.randint(1, 10)
|
||||
await pix.send(f"太贪心了,就给你发 {num}张 好了")
|
||||
x = x[:-1]
|
||||
keyword = " ".join(x)
|
||||
pix_num = int(num * PIX_RATIO) + 15 if PIX_RATIO != 0 else 0
|
||||
omega_num = num - pix_num + 15
|
||||
tmp = await Pixiv.query_images(
|
||||
x, r18=1 if nsfw_tag == 2 else 0, num=pix_num
|
||||
) + await OmegaPixivIllusts.query_images(x, nsfw_tag=nsfw_tag, num=omega_num)
|
||||
@@ -124,14 +135,12 @@ async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if isinstance(img, OmegaPixivIllusts):
|
||||
img_url = img.url
|
||||
author = img.uname
|
||||
print(img.nsfw_tag)
|
||||
elif isinstance(img, Pixiv):
|
||||
img_url = img.img_url
|
||||
author = img.author
|
||||
pid = img.pid
|
||||
title = img.title
|
||||
uid = img.uid
|
||||
# tags = img.tags
|
||||
_img = await get_image(img_url, event.user_id)
|
||||
if _img:
|
||||
msg_id = await pix.send(
|
||||
@@ -152,14 +161,5 @@ async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 查看PIX图库PID: {pid},下载图片出错"
|
||||
)
|
||||
withdraw_message(event, msg_id["message_id"])
|
||||
withdraw_message_manager.withdraw_message(event, msg_id, Config.get_config("pix", "WITHDRAW_PIX_MESSAGE"))
|
||||
|
||||
|
||||
def withdraw_message(event: MessageEvent, id_: int):
|
||||
if WITHDRAW_PIX_TIME[0]:
|
||||
if (
|
||||
(WITHDRAW_PIX_TIME[1] == 0 and isinstance(event, PrivateMessageEvent))
|
||||
or (WITHDRAW_PIX_TIME[1] == 1 and isinstance(event, GroupMessageEvent))
|
||||
or WITHDRAW_PIX_TIME[1] == 2
|
||||
):
|
||||
withdraw_message_manager.append((id_, WITHDRAW_PIX_TIME[0]))
|
||||
|
||||
@@ -4,8 +4,8 @@ from services.log import logger
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import uid_pid_exists
|
||||
from models.pixiv_keyword_user import PixivKeywordUser
|
||||
from models.pixiv import Pixiv
|
||||
from .model.pixiv_keyword_user import PixivKeywordUser
|
||||
from .model.pixiv import Pixiv
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
__zx_plugin_name__ = "PIX关键词/UID/PID添加管理 [Superuser]"
|
||||
|
||||
@@ -7,8 +7,8 @@ from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import remove_image
|
||||
from models.pixiv_keyword_user import PixivKeywordUser
|
||||
from models.pixiv import Pixiv
|
||||
from .model.pixiv_keyword_user import PixivKeywordUser
|
||||
from .model.pixiv import Pixiv
|
||||
|
||||
|
||||
__zx_plugin_name__ = "PIX关键词/UID/PID删除管理 [Superuser]"
|
||||
|
||||
@@ -4,7 +4,7 @@ from utils.message_builder import image
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import gen_keyword_pic, get_keyword_num
|
||||
from models.pixiv_keyword_user import PixivKeywordUser
|
||||
from .model.pixiv_keyword_user import PixivKeywordUser
|
||||
import asyncio
|
||||
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ from utils.utils import get_message_text, is_number
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import start_update_image_url
|
||||
from models.pixiv_keyword_user import PixivKeywordUser
|
||||
from models.pixiv import Pixiv
|
||||
from .model.pixiv_keyword_user import PixivKeywordUser
|
||||
from .model.pixiv import Pixiv
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent
|
||||
import time
|
||||
|
||||
|
||||
Reference in New Issue
Block a user