refactor code

This commit is contained in:
hibiki
2021-07-30 21:21:51 +08:00
parent 2ad891aa1e
commit cc24822dca
165 changed files with 7815 additions and 8174 deletions
+14
View File
@@ -0,0 +1,14 @@
+353
View File
@@ -0,0 +1,353 @@
from aiohttp.client_exceptions import (
ClientOSError,
ServerDisconnectedError,
ClientConnectorError,
)
from asyncpg.exceptions import UniqueViolationError
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 typing import List
from utils.utils import get_local_proxy
from utils.image_utils import CreateImg
from services.log import logger
from configs.config import HIBIAPI_BOOKMARKS, HIBIAPI
from configs.path_config import TEMP_PATH
import platform
import aiohttp
import asyncio
import aiofiles
import math
try:
import ujson as json
except ModuleNotFoundError:
import json
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",
}
# 开始更新
async def start_update_image_url(current_keyword: List[str], black_pid: List[str]):
pid_count = 0
pic_count = 0
tasks = []
semaphore = asyncio.Semaphore(10)
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
params = {"id": keyword[4:], "page": page}
if page == 30:
break
elif keyword.startswith("pid:"):
url = illust_url
params = {"id": keyword[4:]}
else:
url = search_url
params = {"word": keyword, "page": page}
tasks.append(
asyncio.ensure_future(
search_image(
url, keyword, params, semaphore, session, page, black_pid
)
)
)
if keyword.startswith("pid:"):
break
result = await asyncio.gather(*tasks)
for x in result:
pid_count += x[0]
pic_count += x[1]
return pid_count, pic_count
async def search_image(
url: str,
keyword: str,
params: dict,
semaphore: Semaphore,
session: ClientSession,
page: int = 1,
black: List[str] = None,
):
tmp_pid = []
pic_count = 0
pid_count = 0
async with semaphore:
try:
async with session.get(
url,
params=params,
proxy=get_local_proxy(),
) as response:
data = await response.json()
if (
not data
or data.get("error")
or (not data.get("illusts") and not data.get("illust"))
):
return 0, 0
if url != illust_url:
logger.info(f'{keyword}: 获取数据成功...数据总量:{len(data["illusts"])}')
data = data["illusts"]
else:
logger.info(f'获取数据成功...PID:{params.get("id")}')
data = [data["illust"]]
img_data = {}
for x in data:
pid = x["id"]
title = x["title"]
width = x["width"]
height = x["height"]
view = x["total_view"]
bookmarks = x["total_bookmarks"]
uid = x["user"]["id"]
author = x["user"]["name"]
tags = []
for tag in x["tags"]:
for i in tag:
if tag[i]:
tags.append(tag[i])
img_urls = []
if x["page_count"] == 1:
img_urls.append(x["meta_single_page"]["original_image_url"])
else:
for urls in x["meta_pages"]:
img_urls.append(urls["image_urls"]["original"])
if (
(
bookmarks >= HIBIAPI_BOOKMARKS
or (url == member_illust_url and bookmarks >= 1500)
or (url == illust_url)
)
and len(img_urls) < 10
and _check_black(img_urls, black)
):
img_data[pid] = {
"pid": pid,
"title": title,
"width": width,
"height": height,
"view": view,
"bookmarks": bookmarks,
"img_urls": img_urls,
"uid": uid,
"author": author,
"tags": tags,
}
else:
continue
for x in img_data.keys():
data = img_data[x]
for img_url in data["img_urls"]:
img_p = img_url[img_url.rfind("_") + 1 : img_url.rfind(".")]
try:
if await Pixiv.add_image_data(
data["pid"],
data["title"],
data["width"],
data["height"],
data["view"],
data["bookmarks"],
img_url,
img_p,
data["uid"],
data["author"],
",".join(data["tags"]),
):
if data["pid"] not in tmp_pid:
pid_count += 1
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)
return pid_count, pic_count
# 下载图片
async def download_image(img_url: str, session: ClientSession, _count: int = 1):
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
)
except TimeoutError:
logger.warning(f"下载或写入超时,第 {_count} 次调用....")
await download_image(img_url, session, _count + 1)
except ClientPayloadError:
pass
async def get_image(img_url: str, user_id: int) -> str:
try:
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(
img_url,
proxy=get_local_proxy(),
) as response:
async with aiofiles.open(
f"{TEMP_PATH}/pix_{user_id}_{img_url[-10:-4]}.jpg", "wb"
) as f:
await f.write(await response.read())
return f"pix_{user_id}_{img_url[-10:-4]}.jpg"
except (ClientConnectorError, TimeoutError):
return await get_image(img_url, user_id)
# 检测UID或PID是否有效
async def uid_pid_exists(id_: str) -> bool:
if id_.startswith("uid:"):
url = f"{HIBIAPI}/api/pixiv/member"
elif id_.startswith("pid:"):
url = illust_url
else:
return False
params = {"id": int(id_[4:])}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url, params=params, proxy=get_local_proxy()) as response:
data = await response.json()
if data.get("error"):
return False
return True
async def get_keyword_num(keyword: str) -> "int , int":
return await Pixiv.get_keyword_num(keyword.split())
async def remove_image(pid: int, img_p: str) -> bool:
if img_p:
if "p" not in img_p:
img_p = f"p{img_p}"
return await Pixiv.remove_image_data(pid, img_p)
def gen_keyword_pic(
_pass_keyword: List[str], not_pass_keyword: List[str], is_superuser: bool
):
_keyword = [
x
for x in _pass_keyword
if not x.startswith("uid:")
and not x.startswith("pid:")
and not x.startswith("black:")
]
_uid = [x for x in _pass_keyword if x.startswith("uid:")]
_pid = [x for x in _pass_keyword if x.startswith("pid:")]
_n_keyword = [
x
for x in not_pass_keyword
if not x.startswith("uid:")
and not x.startswith("pid:")
and not x.startswith("black:")
]
_n_uid = [
x
for x in not_pass_keyword
if x.startswith("uid:") and not x.startswith("black:")
]
_n_pid = [
x
for x in not_pass_keyword
if x.startswith("pid:") and not x.startswith("black:")
]
img_width = 0
img_data = {
"_keyword": {"width": 0, "data": _keyword},
"_uid": {"width": 0, "data": _uid},
"_pid": {"width": 0, "data": _pid},
"_n_keyword": {"width": 0, "data": _n_keyword},
"_n_uid": {"width": 0, "data": _n_uid},
"_n_pid": {"width": 0, "data": _n_pid},
}
for x in list(img_data.keys()):
img_data[x]["width"] = math.ceil(len(img_data[x]["data"]) / 40)
img_width += img_data[x]["width"] * 200
if not is_superuser:
img_width = (
img_width
- (
img_data["_n_keyword"]["width"]
+ img_data["_n_uid"]["width"]
+ img_data["_n_pid"]["width"]
)
* 200
)
del img_data["_n_keyword"]
del img_data["_n_pid"]
del img_data["_n_uid"]
current_width = 0
A = CreateImg(img_width, 1100)
for x in list(img_data.keys()):
if img_data[x]["data"]:
img = CreateImg(img_data[x]["width"] * 200, 1100, 200, 1100, font_size=40)
start_index = 0
end_index = 40
total_index = img_data[x]["width"] * 40
for _ in range(img_data[x]["width"]):
tmp = CreateImg(198, 1100, font_size=20)
text_img = CreateImg(198, 100, font_size=50)
key_str = "\n".join(
[key for key in img_data[x]["data"][start_index:end_index]]
)
tmp.text((10, 100), key_str)
if x.find("_n") == -1:
text_img.text((24, 24), "已收录")
else:
text_img.text((24, 24), "待收录")
tmp.paste(text_img, (0, 0))
start_index += 40
end_index = (
end_index + 40 if end_index + 40 <= total_index else total_index
)
background_img = CreateImg(200, 1100, color="#FFE4C4")
background_img.paste(tmp, (1, 1))
img.paste(background_img)
A.paste(img, (current_width, 0))
current_width += img_data[x]["width"] * 200
return A.pic2bs4()
def _check_black(img_urls: List[str], black: List[str]):
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
+63
View File
@@ -0,0 +1,63 @@
from nonebot import on_command
from nonebot.adapters.cqhttp.event import GroupMessageEvent
from nonebot.adapters.cqhttp.message import Message
from utils.utils import get_message_text, is_number
from utils.message_builder import image
from services.log import logger
from nonebot.adapters.cqhttp import Bot, MessageEvent
from nonebot.typing import T_State
from .data_source import get_image
from models.pixiv import Pixiv
import random
pix = on_command("pix", aliases={'PIX'}, priority=5, block=True)
@pix.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
num = 1
keyword = get_message_text(event.json())
if is_number(keyword):
all_image = await Pixiv.query_images(uid=int(keyword))
elif keyword.lower().startswith("pid"):
pid = keyword.replace("pid", "").replace(":", "")
if not is_number(pid):
await pix.finish("PID必须是数字...", at_sender=True)
all_image = await Pixiv.query_images(pid=int(pid))
else:
x = keyword.split()
if len(x) > 1:
if is_number(x[-1]):
num = int(x[-1])
if num > 11:
num = random.randint(1, 10)
await pix.send(f"太贪心了,就给你发 {num}张 好了")
x = x[:-1]
keyword = " ".join(x)
all_image = await Pixiv.query_images(x)
if not all_image:
await pix.finish(f"未在图库中找到与 {keyword} 相关Tag/UID/PID的图片...", at_sender=True)
for _ in range(num):
if not all_image:
await pix.finish("坏了...发完了,没图了...")
img = random.choice(all_image)
all_image.remove(img)
img_url = img.img_url
pid = img.pid
title = img.title
author = img.author
uid = img.uid
# tags = img.tags
await pix.send(
Message(
f"title:{title}\n"
f"author:{author}\n"
f"PID:{pid}\nUID:{uid}\n"
f"{image(await get_image(img_url, event.user_id), 'temp')}"
)
)
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 查看PIX图库PID: {pid}"
)
+115
View File
@@ -0,0 +1,115 @@
from nonebot import on_command
from utils.utils import get_message_text, is_number
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 nonebot.permission import SUPERUSER
__plugin_name__ = "添加PIX关键词/UID/PID | 添加PIX黑名单"
__plugin_usage__ = """
关键词:添加搜图的关键词(效果拉胯)
UID:搜集该画师的图片,可指定收藏数
PID:收录单张图片
PIX黑名单:可以是当个pid,也可以是pid_p0等
"""
add_keyword = on_command("添加pix关键词", aliases={"添加pix关键字"}, priority=1, block=True)
add_black_pid = on_command("添加pix黑名单", permission=SUPERUSER, priority=1, block=True)
# 超级用户可以通过字符 -f 来强制收录不检查是否存在
add_uid_pid = on_command(
"添加pixuid",
aliases={
"添加pixpid",
},
priority=1,
block=True,
)
@add_keyword.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = get_message_text(event.json())
group_id = -1
if isinstance(event, GroupMessageEvent):
group_id = event.group_id
if msg:
if await PixivKeywordUser.add_keyword(
event.user_id, group_id, msg, bot.config.superusers
):
await add_keyword.send(
f"已成功添加pixiv搜图关键词:{msg},请等待管理员通过该关键词!", at_sender=True
)
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 添加了pixiv搜图关键词:" + msg
)
else:
await add_keyword.finish(f"该关键词 {msg} 已存在...")
else:
await add_keyword.finish(f"虚空关键词?.?.?.?")
@add_uid_pid.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = get_message_text(event.json())
exists_flag = True
if msg.find("-f") != -1 and str(event.user_id) in bot.config.superusers:
exists_flag = False
msg = msg.replace("-f", "").strip()
if msg:
for msg in msg.split():
if not is_number(msg):
await add_uid_pid.finish("UID只能是数字的说...", at_sender=True)
if state["_prefix"]["raw_command"].lower().endswith("uid"):
msg = f"uid:{msg}"
else:
msg = f"pid:{msg}"
if await Pixiv.check_exists(int(msg[4:]), "p0"):
await add_uid_pid.finish(f"该PID:{msg[4:]}已存在...", at_sender=True)
if not await uid_pid_exists(msg) and exists_flag:
await add_uid_pid.finish("画师或作品不存在或搜索正在CD,请稍等...", at_sender=True)
group_id = -1
if isinstance(event, GroupMessageEvent):
group_id = event.group_id
if await PixivKeywordUser.add_keyword(
event.user_id, group_id, msg, bot.config.superusers
):
await add_uid_pid.send(
f"已成功添加pixiv搜图UID/PID:{msg[4:]},请等待管理员通过!", at_sender=True
)
else:
await add_uid_pid.finish(f"该UID/PID:{msg[4:]} 已存在...")
else:
await add_uid_pid.finish("湮灭吧!虚空的UID!")
@add_black_pid.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
pid = get_message_text(event.json())
img_p = ""
if "p" in pid:
img_p = pid.split("p")[-1]
pid = pid.replace("_", "")
pid = pid[: pid.find("p")]
if not is_number(pid):
await add_black_pid.finish("PID必须全部是数字!", at_sender=True)
if await PixivKeywordUser.add_keyword(
114514,
114514,
f"black:{pid}{f'_p{img_p}' if img_p else ''}",
bot.config.superusers,
):
await add_black_pid.send(f"已添加PID:{pid} 至黑名单中...")
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 添加了pixiv搜图黑名单 PID:{pid}"
)
else:
await add_black_pid.send(f"PID:{pid} 已添加黑名单中,添加失败...")
+167
View File
@@ -0,0 +1,167 @@
from nonebot import on_command
from nonebot.adapters.cqhttp.message import Message
from utils.utils import get_message_text, is_number
from utils.message_builder import at
from services.log import logger
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
__plugin_name__ = "通过/取消/删除pix关键词 | 删除pix图片"
__plugin_usage__ = """
通过/取消/删除pix关键词 [关键词/pid/uid] 示例:通过pix关键词萝莉,通过pix关键词uid:123456,通过pix关键词pid:123456
删除pix图片 *[pid] 示例:删除pix图片4223442
"""
pass_keyword = on_command(
"通过pix关键词",
aliases={"通过pix关键字", "取消pix关键词", "取消pix关键字"},
permission=SUPERUSER,
priority=1,
block=True,
)
del_keyword = on_command(
"删除pix关键词", aliases={"删除pix关键字"}, permission=SUPERUSER, priority=1, block=True
)
del_pic = on_command(
"删除pix图片", permission=SUPERUSER, priority=1, block=True
)
@del_keyword.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = get_message_text(event.json())
if not msg:
await del_keyword.finish("好好输入要删除什么关键字啊笨蛋!")
if is_number(msg):
msg = f"uid:{msg}"
if msg.lower().startswith("pid"):
msg = "pid:" + msg.replace("pid", "").replace(":", "")
if await PixivKeywordUser.delete_keyword(msg):
await del_keyword.send(f"删除搜图关键词/UID:{msg} 成功...")
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 删除了pixiv搜图关键词:" + msg
)
else:
await del_keyword.send(f"未查询到搜索关键词/UID/PID:{msg},删除失败!")
@del_pic.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
pid_arr = get_message_text(event.json())
if pid_arr:
msg = ""
black_pid = ""
flag = False
pid_arr = pid_arr.split()
if pid_arr[-1] in ["-black", "-b"]:
flag = True
pid_arr = pid_arr[:-1]
for pid in pid_arr:
img_p = None
if "p" in pid or "ugoira" in pid:
if "p" in pid:
img_p = pid.split("p")[-1]
pid = pid.replace("_", "")
pid = pid[: pid.find("p")]
elif "ugoira" in pid:
img_p = pid.split("ugoira")[-1]
pid = pid.replace("_", "")
pid = pid[: pid.find("ugoira")]
if is_number(pid):
if await Pixiv.query_images(pid=int(pid), r18=2):
if await remove_image(int(pid), img_p):
msg += f'{pid}{f"_p{img_p}" if img_p else ""},'
if flag:
if await PixivKeywordUser.add_keyword(
114514,
114514,
f"black:{pid}{f'_p{img_p}' if img_p else ''}",
bot.config.superusers,
):
black_pid += f'{pid}{f"_p{img_p}" if img_p else ""},'
logger.info(
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 删除了PIX图片 PID:{pid}{f'_p{img_p}' if img_p else ''}"
)
else:
await del_pic.send(
f"PIX:删除pid:{pid}{f'_p{img_p}' if img_p else ''} 失败.."
)
else:
await del_pic.send(
f"PIX:图片pix:{pid}{f'_p{img_p}' if img_p else ''} 不存在...无法删除.."
)
else:
await del_pic.send(f"PID必须为数字!pid:{pid}", at_sender=True)
await del_pic.send(f"PIX:成功删除图片:{msg[:-1]}")
if flag:
await del_pic.send(f"成功图片PID加入黑名单:{black_pid[:-1]}")
else:
await del_pic.send("虚空删除?")
@pass_keyword.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
tmp = {"group": {}, "private": {}}
msg = get_message_text(event.json())
if not msg:
await pass_keyword.finish("通过虚空的关键词/UID?离谱...")
msg = msg.split()
flag = True if state["_prefix"]["raw_command"][:2] == "通过" else False
for x in msg:
if x.lower().startswith("uid"):
x = x.replace("uid", "").replace(":", "")
x = f"uid:{x}"
elif x.lower().startswith("pid"):
x = x.replace("pid", "").replace(":", "")
x = f"pid:{x}"
if x.lower().find("pid") != -1 or x.lower().find("uid") != -1:
if not is_number(x[4:]):
await pass_keyword.send(f"UID/PID:{x} 非全数字,跳过该关键词...")
continue
user_id, group_id = await PixivKeywordUser.set_keyword_pass(x, flag)
if not user_id:
await pass_keyword.send(f"未找到关键词/UID:{x},请检查关键词/UID是否存在...")
continue
if flag:
if group_id == -1:
if not tmp["private"].get(user_id):
tmp["private"][user_id] = {"keyword": [x]}
else:
tmp["private"][user_id]["keyword"].append(x)
else:
if not tmp["group"].get(group_id):
tmp["group"][group_id] = {}
if not tmp["group"][group_id].get(user_id):
tmp["group"][group_id][user_id] = {"keyword": [x]}
else:
tmp["group"][group_id][user_id]["keyword"].append(x)
msg = " ".join(msg)
await pass_keyword.send(f'已成功{state["_prefix"]["raw_command"][:2]}搜图关键词:{msg}....')
for user in tmp["private"]:
x = ",".join(tmp["private"][user]["keyword"])
await bot.send_private_msg(
user_id=user, message=f"你的关键词/UID/PID {x} 已被管理员通过,将在下一次进行更新..."
)
for group in tmp["group"]:
for user in tmp["group"][group]:
x = ",".join(tmp["group"][group][user]["keyword"])
await bot.send_group_msg(
group_id=group,
message=Message(f"{at(user)}你的关键词/UID/PID {x} 已被管理员通过,将在下一次进行更新..."),
)
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 通过了pixiv搜图关键词/UID:" + msg
)
+65
View File
@@ -0,0 +1,65 @@
from nonebot import on_command
from utils.utils import get_message_text
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
import asyncio
__plugin_name__ = "查看pix图库"
__plugin_usage__ = """
查看pix图库 [keyword]:当keyword为空时检查所有图片
"""
my_keyword = on_command("我的pix关键词", aliases={"我的pix关键字"}, priority=1, block=True)
show_keyword = on_command("显示pix关键词", aliases={"显示pix关键字"}, priority=1, block=True)
show_pix = on_command("查看pix图库", priority=1, block=True)
@my_keyword.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
data = await PixivKeywordUser.get_all_user_dict()
if data.get(event.user_id) is None or not data[event.user_id]["keyword"]:
await my_keyword.finish("您目前没有提供任何Pixiv搜图关键字...", at_sender=True)
await my_keyword.send(
f"您目前提供的如下关键字:\n\t" + ",".join(data[event.user_id]["keyword"])
)
@show_keyword.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
_pass_keyword, not_pass_keyword = await PixivKeywordUser.get_current_keyword()
if _pass_keyword or not_pass_keyword:
await show_keyword.send(
image(
b64=await asyncio.get_event_loop().run_in_executor(
None,
gen_keyword_pic,
_pass_keyword,
not_pass_keyword,
str(event.user_id) in bot.config.superusers,
)
)
)
else:
if str(event.user_id) in bot.config.superusers:
await show_keyword.finish(f"目前没有已收录或待收录的搜索关键词...")
else:
await show_keyword.finish(f"目前没有已收录的搜索关键词...")
@show_pix.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
keyword = get_message_text(event.json())
count, r18_count = await get_keyword_num(keyword)
await show_pix.send(
f"PIX图库:{keyword}\n"
f"总数:{count + r18_count}\n"
f"美图:{count}\n"
f"r18:{r18_count}"
)
+126
View File
@@ -0,0 +1,126 @@
from nonebot import on_command
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 nonebot.adapters.cqhttp import Bot, MessageEvent
import time
__plugin_name__ = "更新pix关键字 | pix检查更新"
__plugin_usage__ = """
更新pix收录的所有或指定数量的关键词/uid/pid
更新pix关键词 [keyword/uid/pid] [num]=all 示例:更新pix关键词uid 5
pix检测更新:检测从未更新过的uid和pid
"""
start_update = on_command(
"更新pix关键词", aliases={"更新pix关键字"}, permission=SUPERUSER, priority=1, block=True
)
check_not_update_uid_pid = on_command(
"pix检测更新",
aliases={"pix检查更新"},
permission=SUPERUSER,
priority=1,
block=True,
)
@start_update.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg_sp = get_message_text(event.json()).split()
_pass_keyword, _ = await PixivKeywordUser.get_current_keyword()
_pass_keyword.reverse()
black_pid = await PixivKeywordUser.get_black_pid()
_keyword = [
x
for x in _pass_keyword
if not x.startswith("uid:")
and not x.startswith("pid:")
and not x.startswith("black:")
]
_uid = [x for x in _pass_keyword if x.startswith("uid:")]
_pid = [x for x in _pass_keyword if x.startswith("pid:")]
num = 9999
msg = msg_sp[0] if len(msg_sp) else ""
if len(msg_sp) == 2:
if is_number(msg_sp[1]):
num = int(msg_sp[1])
else:
await start_update.finish("参数错误...第二参数必须为数字")
if num < 10000:
keyword_str = ",".join(
_keyword[: num if num < len(_keyword) else len(_keyword)]
)
uid_str = ",".join(_uid[: num if num < len(_uid) else len(_uid)])
pid_str = ",".join(_pid[: num if num < len(_pid) else len(_pid)])
if msg.lower() == "pid":
update_lst = _pid
info = f"开始更新Pixiv搜图PID:\n{pid_str}"
elif msg.lower() == "uid":
update_lst = _uid
info = f"开始更新Pixiv搜图UID:\n{uid_str}"
elif msg.lower() == "keyword":
update_lst = _keyword
info = f"开始更新Pixiv搜图关键词:\n{keyword_str}"
else:
update_lst = _pass_keyword
info = f"开始更新Pixiv搜图关键词:\n{keyword_str}\n更新UID:{uid_str}\n更新PID:{pid_str}"
num = num if num < len(update_lst) else len(update_lst)
else:
if msg.lower() == "pid":
update_lst = [f"pid:{num}"]
info = f"开始更新Pixiv搜图UID:\npid:{num}"
else:
update_lst = [f"uid:{num}"]
info = f"开始更新Pixiv搜图UID:\nuid:{num}"
await start_update.send(info)
start_time = time.time()
pid_count, pic_count = await start_update_image_url(update_lst[:num], black_pid)
await start_update.send(
f"Pixiv搜图关键词搜图更新完成...\n"
f"累计更新PID {pid_count} 个\n"
f"累计更新图片 {pic_count} 张" + "\n耗时:{:.2f}秒".format((time.time() - start_time))
)
@check_not_update_uid_pid.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = get_message_text(event.json())
flag = False
if msg == "update":
flag = True
_pass_keyword, _ = await PixivKeywordUser.get_current_keyword()
x_uid = []
x_pid = []
_uid = [int(x[4:]) for x in _pass_keyword if x.startswith("uid:")]
_pid = [int(x[4:]) for x in _pass_keyword if x.startswith("pid:")]
all_images = await Pixiv.query_images(r18=2)
for img in all_images:
if img.pid not in x_pid:
x_pid.append(img.pid)
if img.uid not in x_uid:
x_uid.append(img.uid)
await check_not_update_uid_pid.send(
"从未更新过的UID:"
+ ",".join([f"uid:{x}" for x in _uid if x not in x_uid])
+ "\n"
+ "从未更新过的PID:"
+ ",".join([f"pid:{x}" for x in _pid if x not in x_pid])
)
if flag:
await check_not_update_uid_pid.send("开始自动自动更新PID....")
update_lst = [f"pid:{x}" for x in _uid if x not in x_uid]
black_pid = await PixivKeywordUser.get_black_pid()
start_time = time.time()
pid_count, pic_count = await start_update_image_url(update_lst, black_pid)
await check_not_update_uid_pid.send(
f"Pixiv搜图关键词搜图更新完成...\n"
f"累计更新PID {pid_count} 个\n"
f"累计更新图片 {pic_count} 张" + "\n耗时:{:.2f}秒".format((time.time() - start_time))
)