zhenxun_bot/plugins/send_setu/data_source.py

266 lines
9.7 KiB
Python
Raw Normal View History

2021-07-30 21:21:51 +08:00
from configs.path_config import IMAGE_PATH
from utils.message_builder import image
2021-05-20 19:23:32 +08:00
from services.log import logger
2021-07-30 21:21:51 +08:00
from aiohttp.client_exceptions import ClientConnectorError
2021-08-04 15:19:45 +08:00
from utils.image_utils import get_img_hash, compressed_image
2021-07-30 21:21:51 +08:00
from asyncpg.exceptions import UniqueViolationError
from utils.utils import get_local_proxy
2021-05-20 19:23:32 +08:00
from asyncio.exceptions import TimeoutError
2021-07-30 21:21:51 +08:00
from typing import List, Optional
2021-10-03 14:24:07 +08:00
from configs.config import INITIAL_SETU_PROBABILITY, NICKNAME, DOWNLOAD_SETU
2021-07-30 21:21:51 +08:00
from models.setu import Setu
import aiohttp
import aiofiles
2021-08-04 15:19:45 +08:00
import asyncio
2021-07-30 21:21:51 +08:00
import os
import random
2021-05-20 19:23:32 +08:00
try:
import ujson as json
except ModuleNotFoundError:
import json
2021-07-30 21:21:51 +08:00
url = "https://api.lolicon.app/setu/v2"
path = "_setu/"
r18_path = "_r18/"
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
# 获取url
async def get_setu_urls(
2021-08-04 15:19:45 +08:00
tags: List[str], num: int = 1, r18: int = 0, command: str = ""
2021-07-30 21:21:51 +08:00
) -> "List[str], List[str], List[tuple], int":
2021-08-17 23:17:08 +08:00
tags = tags[:3] if len(tags) > 3 else tags
2021-05-20 19:23:32 +08:00
params = {
2021-07-30 21:21:51 +08:00
"r18": r18, # 添加r18参数 0为否1为是2为混合
2021-08-17 23:17:08 +08:00
"tag": tags, # 若指定tag
"num": 100, # 一次返回的结果数量
"size": ["original"],
2021-05-26 20:08:13 +08:00
}
2021-07-30 21:21:51 +08:00
async with aiohttp.ClientSession() as session:
for count in range(3):
logger.info(f"get_setu_url: count --> {count}")
2021-05-20 19:23:32 +08:00
try:
2021-07-30 21:21:51 +08:00
async with session.get(
url, proxy=get_local_proxy(), timeout=2, params=params
) as response:
2021-05-20 19:23:32 +08:00
if response.status == 200:
data = await response.json()
2021-07-30 21:21:51 +08:00
if not data["error"]:
data = data["data"]
2021-08-04 15:19:45 +08:00
(
urls,
text_list,
add_databases_list,
) = await asyncio.get_event_loop().run_in_executor(
None, _setu_data_process, data, command
)
2021-07-31 21:21:47 +08:00
num = num if num < len(data) else len(data)
2021-07-30 21:21:51 +08:00
random_idx = random.sample(range(len(data)), num)
2021-06-15 10:57:08 +08:00
x_urls = []
x_text_lst = []
for x in random_idx:
x_urls.append(urls[x])
x_text_lst.append(text_list[x])
2021-09-05 02:21:38 +08:00
if not x_urls:
return ["没找到符合条件的色图..."], [], [], 401
2021-07-30 21:21:51 +08:00
return x_urls, x_text_lst, add_databases_list, 200
2021-05-20 19:23:32 +08:00
else:
2021-07-30 21:21:51 +08:00
return ["没找到符合条件的色图..."], [], [], 401
except (TimeoutError, ClientConnectorError):
2021-05-20 19:23:32 +08:00
pass
2021-07-30 21:21:51 +08:00
return ["我网线被人拔了..QAQ"], [], [], 999
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
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",
}
2021-08-04 15:19:45 +08:00
async def search_online_setu(
2021-10-03 14:24:07 +08:00
url_: str, id_: Optional[int] = None, path_: Optional[str] = None
2021-08-04 15:19:45 +08:00
) -> "MessageSegment, int":
2021-10-03 14:24:07 +08:00
"""
下载色图
:param url_: 色图url
:param id_: 本地id
:param path_: 存储路径
"""
2021-07-30 21:21:51 +08:00
if "i.pixiv.cat" in url_:
url_ = url_.replace("i.pixiv.cat", "i.pximg.net")
async with aiohttp.ClientSession(headers=headers) as session:
2021-05-20 19:23:32 +08:00
for i in range(3):
2021-07-30 21:21:51 +08:00
logger.info(f"search_online_setu --> {i}")
2021-05-20 19:23:32 +08:00
try:
2021-07-30 21:21:51 +08:00
async with session.get(url_, proxy=get_local_proxy(), timeout=3) as res:
2021-05-20 19:23:32 +08:00
if res.status == 200:
2021-08-04 15:19:45 +08:00
index = random.randint(1, 100000) if id_ is None else id_
path_ = "temp" if not path_ else path_
file = f"{index}_temp_setu.jpg" if not path_ else f"{index}.jpg"
2021-08-09 07:50:33 +08:00
if not os.path.exists(f'{IMAGE_PATH}/{path_}'):
os.mkdir(f'{IMAGE_PATH}/{path_}')
2021-08-04 15:19:45 +08:00
async with aiofiles.open(
f"{IMAGE_PATH}/{path_}/{file}", "wb"
) as f:
2021-05-20 19:23:32 +08:00
try:
await f.write(await res.read())
except TimeoutError:
continue
2021-08-04 15:19:45 +08:00
if id_ is not None:
if (
os.path.getsize(f"{IMAGE_PATH}/{path_}/{index}.jpg")
> 1024 * 1024 * 1.5
):
compressed_image(
2021-08-17 23:17:08 +08:00
f"{IMAGE_PATH}/{path_}/{index}.jpg",
2021-08-04 15:19:45 +08:00
)
2021-07-30 21:21:51 +08:00
logger.info(f"下载 lolicon图片 {url_} 成功, id{index}")
2021-08-02 19:19:22 +08:00
return image(file, path_), index
2021-05-20 19:23:32 +08:00
else:
2021-07-30 21:21:51 +08:00
logger.warning(f"访问 lolicon图片 {url_} 失败 status{res.status}")
2021-05-20 19:23:32 +08:00
# return '\n这图好难下载啊QAQ', -1, False
2021-07-30 21:21:51 +08:00
except (TimeoutError, ClientConnectorError):
2021-05-20 19:23:32 +08:00
pass
2021-07-30 21:21:51 +08:00
return "图片被小怪兽恰掉啦..!QAQ", -1
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
# 检测本地是否有id涩图无的话则下载
async def check_local_exists_or_download(setu_image: Setu) -> "MessageSegment, int":
2021-10-03 14:24:07 +08:00
path_ = None
id_ = None
if DOWNLOAD_SETU:
id_ = setu_image.local_id
if setu_image.is_r18:
path_ = "_r18"
else:
path_ = path
if os.path.exists(f"{IMAGE_PATH}/{path_}/{setu_image.local_id}.jpg"):
return image(f"{setu_image.local_id}.jpg", path_), 200
return await search_online_setu(setu_image.img_url, id_, path_)
2021-07-30 21:21:51 +08:00
# 添加涩图数据到数据库
async def add_data_to_database(lst: List[tuple]):
tmp = []
for x in lst:
if x not in tmp:
tmp.append(x)
if tmp:
for x in tmp:
try:
2021-08-04 15:19:45 +08:00
r18 = 1 if "R-18" in x[5] else 0
2021-07-30 21:21:51 +08:00
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],
)
except UniqueViolationError:
pass
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
# 拿到本地色图列表
async def get_setu_list(
index: Optional[int] = None, tags: Optional[List[str]] = None, r18: int = 0
) -> "list, int":
if index:
2021-09-09 22:49:45 +08:00
image_count = await Setu.get_image_count(r18) - 1
2021-07-30 21:21:51 +08:00
if index < 0 or index > image_count:
return [f"超过当前上下限!({image_count})"], 999
image_list = [await Setu.query_image(index, r18=r18)]
elif tags:
image_list = await Setu.query_image(tags=tags, r18=r18)
else:
image_list = await Setu.query_image(r18=r18)
if not image_list:
return ["没找到符合条件的色图..."], 998
return image_list, 200
# 初始化消息
def gen_message(setu_image: Setu, img_msg: bool = False):
local_id = setu_image.local_id
title = setu_image.title
author = setu_image.author
pid = setu_image.pid
return (
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 ''}"
)
# 罗翔老师!
2021-05-20 19:23:32 +08:00
def get_luoxiang(impression):
2021-08-17 23:17:08 +08:00
probability = impression + INITIAL_SETU_PROBABILITY * 100
2021-05-20 19:23:32 +08:00
if probability < random.randint(1, 101):
2021-07-30 21:21:51 +08:00
return (
"我为什么要给你发这个?"
+ image(random.choice(os.listdir(IMAGE_PATH + "luoxiang/")), "luoxiang")
2021-08-17 23:17:08 +08:00
+ f"\n(快向{NICKNAME}签到提升好感度吧!)"
2021-07-30 21:21:51 +08:00
)
2021-05-20 19:23:32 +08:00
return None
async def find_img_index(img_url, user_id):
async with aiohttp.ClientSession() as session:
async with session.get(img_url, proxy=get_local_proxy(), timeout=5) as res:
2021-07-30 21:21:51 +08:00
async with aiofiles.open(
IMAGE_PATH + f"temp/{user_id}_find_setu_index.jpg", "wb"
) as f:
2021-05-20 19:23:32 +08:00
await f.write(await res.read())
img_hash = str(get_img_hash(IMAGE_PATH + f"temp/{user_id}_find_setu_index.jpg"))
2021-07-30 21:21:51 +08:00
setu_img = await Setu.get_image_in_hash(img_hash)
if setu_img:
return (
f"id{setu_img.local_id}\n"
f"title{setu_img.title}\n"
f"author{setu_img.author}\n"
f"PID{setu_img.pid}"
)
2021-08-02 19:19:22 +08:00
return "该图不在色图库中或色图库未更新!"
2021-08-04 15:19:45 +08:00
# 处理色图数据
def _setu_data_process(data: dict, command: str) -> "list, list, list":
urls = []
text_list = []
add_databases_list = []
for i in range(len(data)):
img_url = data[i]["urls"]["original"]
img_url = (
img_url.replace("i.pixiv.cat", "i.pximg.net")
if "i.pixiv.cat" in img_url
else img_url
)
title = data[i]["title"]
author = data[i]["author"]
pid = data[i]["pid"]
urls.append(img_url)
text_list.append(f"title{title}\nauthor{author}\nPID{pid}")
tags = []
for j in range(len(data[i]["tags"])):
tags.append(data[i]["tags"][j])
if command != "色图r":
if "R-18" in tags:
tags.remove("R-18")
add_databases_list.append(
(
title,
author,
pid,
"",
img_url,
",".join(tags),
)
)
return urls, text_list, add_databases_list