-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from NekoAria/2.0
V2.4.3
- Loading branch information
Showing
6 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
ENVIRONMENT=prod | ||
VERSION='v2.4.2' | ||
VERSION='v2.4.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import nga, pixiv, south_plus, weibo # noqa | ||
from . import nga, pixiv, south_plus, weibo, danbooru # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import httpx | ||
import sqlite3 | ||
|
||
from nonebot import logger | ||
from pyquery import PyQuery as Pq | ||
|
||
from .Parsing import ( | ||
ParsingBase, | ||
get_proxy, | ||
write_item, | ||
cache_db_manage, | ||
duplicate_exists, | ||
) | ||
from .Parsing.handle_images import handle_img_combo | ||
from ..rss_class import Rss | ||
from ...config import DATA_PATH | ||
|
||
|
||
# 处理图片 | ||
@ParsingBase.append_handler(parsing_type="picture", rex="danbooru") | ||
async def handle_picture( | ||
rss: Rss, state: dict, item: dict, item_msg: str, tmp: str, tmp_state: dict | ||
) -> str: | ||
|
||
# 判断是否开启了只推送标题 | ||
if rss.only_title: | ||
return "" | ||
|
||
res = await handle_img( | ||
url=item["link"], | ||
img_proxy=rss.img_proxy, | ||
) | ||
|
||
# 判断是否开启了只推送图片 | ||
if rss.only_pic: | ||
return f"{res}\n" | ||
|
||
return f"{tmp + res}\n" | ||
|
||
|
||
# 处理图片、视频 | ||
async def handle_img(url: str, img_proxy: bool) -> str: | ||
img_str = "" | ||
|
||
# 处理图片 | ||
async with httpx.AsyncClient(proxies=get_proxy(img_proxy)) as client: | ||
response = await client.get(url) | ||
d = Pq(response.text) | ||
img = d("img#image") | ||
if img: | ||
url = img.attr("src") | ||
else: | ||
img_str += "视频封面:" | ||
url = d("meta[property='og:image']").attr("content") | ||
img_str += await handle_img_combo(url, img_proxy) | ||
|
||
return img_str | ||
|
||
|
||
# 如果启用了去重模式,对推送列表进行过滤 | ||
@ParsingBase.append_before_handler(priority=12, rex="danbooru") | ||
async def handle_check_update(rss: Rss, state: dict): | ||
change_data = state.get("change_data") | ||
conn = state.get("conn") | ||
db = state.get("tinydb") | ||
|
||
# 检查是否启用去重 使用 duplicate_filter_mode 字段 | ||
if not rss.duplicate_filter_mode: | ||
return {"change_data": change_data} | ||
|
||
if not conn: | ||
conn = sqlite3.connect(DATA_PATH / "cache.db") | ||
conn.set_trace_callback(logger.debug) | ||
|
||
await cache_db_manage(conn) | ||
|
||
delete = [] | ||
for index, item in enumerate(change_data): | ||
summary = await get_summary(item, rss.img_proxy) | ||
is_duplicate, image_hash = await duplicate_exists( | ||
rss=rss, | ||
conn=conn, | ||
link=item["link"], | ||
title=item["title"], | ||
summary=summary, | ||
) | ||
if is_duplicate: | ||
write_item(db, item) | ||
delete.append(index) | ||
else: | ||
change_data[index]["image_hash"] = str(image_hash) | ||
|
||
change_data = [ | ||
item for index, item in enumerate(change_data) if index not in delete | ||
] | ||
|
||
return { | ||
"change_data": change_data, | ||
"conn": conn, | ||
} | ||
|
||
|
||
# 获取正文 | ||
async def get_summary(item: dict, img_proxy: bool) -> str: | ||
summary = ( | ||
item["content"][0].get("value") if item.get("content") else item["summary"] | ||
) | ||
# 如果图片非视频封面,替换为更清晰的预览图 | ||
summary_doc = Pq(summary) | ||
async with httpx.AsyncClient(proxies=get_proxy(img_proxy)) as client: | ||
response = await client.get(item["link"]) | ||
d = Pq(response.text) | ||
img = d("img#image") | ||
if img: | ||
summary_doc("img").attr("src", img.attr("src")) | ||
return str(summary_doc) |