mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-12 23:40:03 +08:00
update code
This commit is contained in:
@@ -1,14 +1,76 @@
|
||||
from configs.config import HIBIAPI
|
||||
from services.log import logger
|
||||
from models.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from pathlib import Path
|
||||
from nonebot import Driver
|
||||
from typing import List
|
||||
from datetime import datetime
|
||||
import nonebot
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
illust_url = f"{HIBIAPI}/api/pixiv/illust"
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _init_omega_pixiv_illusts():
|
||||
omega_pixiv_illusts = None
|
||||
for file in os.listdir("."):
|
||||
if "omega_pixiv_illusts" in file and ".sql" in file:
|
||||
omega_pixiv_illusts = Path() / file
|
||||
if omega_pixiv_illusts:
|
||||
with open(omega_pixiv_illusts, "r", encoding="utf8") as f:
|
||||
lines = f.readlines()
|
||||
tasks = []
|
||||
length = len([x for x in lines if "INSERT INTO" in x.upper()])
|
||||
all_pid = await OmegaPixivIllusts.get_all_pid()
|
||||
index = 0
|
||||
logger.info('检测到OmegaPixivIllusts数据库,准备开始更新....')
|
||||
for line in lines:
|
||||
if "INSERT INTO" in line.upper():
|
||||
index += 1
|
||||
tasks.append(asyncio.ensure_future(_tasks(line, all_pid, length, index)))
|
||||
await asyncio.gather(*tasks)
|
||||
omega_pixiv_illusts.unlink()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async def _tasks(line: str, all_pid: List[int], length: int, index: int):
|
||||
data = line.split("VALUES", maxsplit=1)[-1].strip()
|
||||
if data.startswith("("):
|
||||
data = data[1:]
|
||||
if data.endswith(");"):
|
||||
data = data[:-2]
|
||||
x = data.split(maxsplit=3)
|
||||
pid = int(x[1][:-1].strip())
|
||||
if pid in all_pid:
|
||||
logger.info(f'添加OmegaPixivIllusts图库数据已存在 ---> pid:{pid}')
|
||||
return
|
||||
uid = int(x[2][:-1].strip())
|
||||
x = x[3].split(", '")
|
||||
title = x[0].strip()[1:-1]
|
||||
tmp = x[1].split(', ')
|
||||
author = tmp[0].strip()[:-1]
|
||||
nsfw_tag = int(tmp[1])
|
||||
width = int(tmp[2])
|
||||
height = int(tmp[3])
|
||||
tags = x[2][:-1]
|
||||
url = x[3][:-1]
|
||||
if await OmegaPixivIllusts.add_image_data(
|
||||
pid,
|
||||
title,
|
||||
width,
|
||||
height,
|
||||
url,
|
||||
uid,
|
||||
author,
|
||||
nsfw_tag,
|
||||
tags,
|
||||
datetime.min,
|
||||
datetime.min
|
||||
):
|
||||
logger.info(f"成功添加OmegaPixivIllusts图库数据 pid:{pid} 本次预计存储 {length} 张,已更新第 {index} 张")
|
||||
else:
|
||||
logger.info(f"添加OmegaPixivIllusts图库数据已存在 ---> pid:{pid}")
|
||||
|
||||
|
||||
@@ -4,16 +4,17 @@ from aiohttp.client_exceptions import (
|
||||
ClientConnectorError,
|
||||
)
|
||||
from asyncpg.exceptions import UniqueViolationError
|
||||
from models.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 typing import List
|
||||
from utils.utils import get_local_proxy
|
||||
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
|
||||
from configs.config import HIBIAPI_BOOKMARKS, HIBIAPI, PIX_IMAGE_SIZE
|
||||
from configs.path_config import TEMP_PATH
|
||||
import platform
|
||||
import aiohttp
|
||||
@@ -215,19 +216,45 @@ async def download_image(img_url: str, session: ClientSession, _count: int = 1):
|
||||
|
||||
|
||||
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)
|
||||
async with aiohttp.ClientSession(headers=headers) as session:
|
||||
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,
|
||||
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']
|
||||
else:
|
||||
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)
|
||||
for _ in range(3):
|
||||
try:
|
||||
async with session.get(
|
||||
img_url,
|
||||
proxy=get_local_proxy(),
|
||||
) as response:
|
||||
if response.status == 404:
|
||||
img_url = old_img_url
|
||||
continue
|
||||
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):
|
||||
pass
|
||||
|
||||
|
||||
# 检测UID或PID是否有效
|
||||
@@ -248,7 +275,9 @@ async def uid_pid_exists(id_: str) -> bool:
|
||||
|
||||
|
||||
async def get_keyword_num(keyword: str) -> "int , int":
|
||||
return await Pixiv.get_keyword_num(keyword.split())
|
||||
count, r18_count = await Pixiv.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:
|
||||
|
||||
+92
-22
@@ -1,17 +1,30 @@
|
||||
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 configs.config import PIX_OMEGA_PIXIV_RATIO, WITHDRAW_PIX_TIME
|
||||
from models.omega_pixiv_illusts import OmegaPixivIllusts
|
||||
from utils.message_builder import image
|
||||
from services.log import logger
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent
|
||||
from nonebot.adapters.cqhttp import (
|
||||
Bot,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
PrivateMessageEvent,
|
||||
)
|
||||
from utils.static_data import withdraw_message_id_manager
|
||||
from nonebot.typing import T_State
|
||||
from .data_source import get_image
|
||||
from models.pixiv import Pixiv
|
||||
from nonebot import on_command
|
||||
import random
|
||||
|
||||
|
||||
pix = on_command("pix", aliases={"PIX"}, priority=5, block=True)
|
||||
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.handle()
|
||||
@@ -24,40 +37,97 @@ async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
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))
|
||||
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)
|
||||
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 > 11:
|
||||
num = random.randint(1, 10)
|
||||
await pix.send(f"太贪心了,就给你发 {num}张 好了")
|
||||
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)
|
||||
all_image = await Pixiv.query_images(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)
|
||||
tmp_ = []
|
||||
all_image = []
|
||||
for x in tmp:
|
||||
if x.pid not in tmp_:
|
||||
all_image.append(x)
|
||||
tmp_.append(x.pid)
|
||||
if not all_image:
|
||||
await pix.finish(f"未在图库中找到与 {keyword} 相关Tag/UID/PID的图片...", at_sender=True)
|
||||
for _ in range(num):
|
||||
img_url = None
|
||||
author = None
|
||||
if not all_image:
|
||||
await pix.finish("坏了...发完了,没图了...")
|
||||
img = random.choice(all_image)
|
||||
all_image.remove(img)
|
||||
img_url = img.img_url
|
||||
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
|
||||
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')}"
|
||||
_img = await get_image(img_url, event.user_id)
|
||||
if _img:
|
||||
msg_id = await pix.send(
|
||||
Message(
|
||||
f"title:{title}\n"
|
||||
f"author:{author}\n"
|
||||
f"PID:{pid}\nUID:{uid}\n"
|
||||
f"{image(_img, 'temp')}"
|
||||
)
|
||||
)
|
||||
logger.info(
|
||||
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 查看PIX图库PID: {pid}"
|
||||
)
|
||||
else:
|
||||
msg_id = await pix.send(f"下载图片似乎出了一点问题,PID:{pid}")
|
||||
logger.info(
|
||||
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"])
|
||||
|
||||
|
||||
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_id_manager["message_id"].append(
|
||||
(id_, WITHDRAW_PIX_TIME[0])
|
||||
)
|
||||
)
|
||||
logger.info(
|
||||
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 查看PIX图库PID: {pid}"
|
||||
)
|
||||
|
||||
@@ -56,10 +56,16 @@ async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
@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)
|
||||
count, r18_count, count_, setu_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}"
|
||||
f"R18:{r18_count}\n"
|
||||
f"---------------\n"
|
||||
f"Omega图库:{keyword}\n"
|
||||
f"总数:{count_ + setu_count + r18_count_}\n"
|
||||
f"美图:{count_}\n"
|
||||
f"色图:{setu_count}\n"
|
||||
f"R18:{r18_count_}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user