fix: hoyolab new post type cover
All checks were successful
Docker Build / docker build and publish (push) Successful in 2m21s

This commit is contained in:
xtaodada 2024-06-29 00:36:16 +08:00
parent c5ceebc487
commit 8354f4bafb
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
5 changed files with 65 additions and 22 deletions

View File

@ -111,6 +111,7 @@ class PostInfo(BaseModel):
video_urls: List[str]
content: str
cover: Optional[str]
has_cover: Optional[bool] = False
game_id: int
topics: List[PostTopic]
view_type: PostType
@ -191,6 +192,7 @@ class PostInfo(BaseModel):
):
content = PostInfo.parse_structured_content(json.loads(structured_content))
cover = post["cover"]
has_cover = post.get("has_cover", False)
cover_list = _data_post.get("cover_list", [])
if (not cover) and cover_list:
cover = cover_list[0]["url"]
@ -221,6 +223,7 @@ class PostInfo(BaseModel):
created_at=created_at,
content=content,
cover=cover,
has_cover=has_cover,
game_id=game_id,
topics=topics,
view_type=view_type,

View File

@ -3,23 +3,23 @@
{
"id": "2",
"icon": "https://fastcdn.hoyoverse.com/static-resource-v2/2023/11/08/9db76fb146f82c045bc276956f86e047_6878380451593228482.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/04/22/77b5112a9c554e32a52ea5bae092b88b_4884704048190391064.jpg",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/05/30/45f550e5d5bf4f216011450466453a74_2229377313645390599.jpg?x-oss-process=image%2Fauto-orient%2C0%2Finterlace%2C1%2Fformat%2Cwebp%2Fquality%2Cq_70",
"name": "原神",
"bg_color": "#203852",
"bg_color": "#9DAF77",
"focus_channel_id": "30"
},
{
"id": "6",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/starrail_hoyoverse.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/05/11/ba42496135bd7d21433c2edfc8193696_879130898720120555.jpg",
"icon": "https://hyl-static-res-prod.hoyolab.com/communityweb/business/starrail_hoyoverse.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/06/07/97ff3a94b16fc6623851731407eefff1_3539500584346988147.jpg?x-oss-process=image%2Fauto-orient%2C0%2Finterlace%2C1%2Fformat%2Cwebp%2Fquality%2Cq_70",
"name": "崩坏:星穹铁道",
"bg_color": "#4E3EC0",
"bg_color": "#5D0AC9",
"focus_channel_id": "42"
},
{
"id": "8",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/nap.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/03/14/5954031fdaa7605823671807d5784637_5991613770476069848.jpg",
"icon": "https://hyl-static-res-prod.hoyolab.com/communityweb/business/nap.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/06/28/1f817e30accd6c117aba10760c52552f_1609948843932447239.jpg?x-oss-process=image/auto-orient,0/interlace,1/format,webp/quality,q_70",
"name": "绝区零",
"bg_color": "#F5F6FB",
"focus_channel_id": "46"
@ -27,14 +27,14 @@
{
"id": "1",
"icon": "https://fastcdn.hoyoverse.com/static-resource-v2/2024/02/29/3d96534fd7a35a725f7884e6137346d1_3942255444511793944.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/05/06/21a02ff136818aaf7359d38f59b29193_1656723357122759447.jpg",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2024/06/04/28e9275e92f4362d4909770009304971_5774434829021569250.jpg?x-oss-process=image%2Fauto-orient%2C0%2Finterlace%2C1%2Fformat%2Cwebp%2Fquality%2Cq_70",
"name": "Honkai Impact 3rd",
"bg_color": "#652273",
"bg_color": "#0A2966",
"focus_channel_id": "0"
},
{
"id": "4",
"icon": "https://webstatic-sea.hoyolab.com/communityweb/business/nxx_hoyoverse.png",
"icon": "https://hyl-static-res-prod.hoyolab.com/communityweb/business/nxx_hoyoverse.png",
"bg": "https://upload-os-bbs.hoyolab.com/upload/2023/10/24/d97b3eb5763dba18bc137929ce53ba24_8118580296436267822.png",
"name": "未定事件簿",
"bg_color": "#2E3857",

View File

@ -77,7 +77,7 @@ async def process_link_func(markdown_text: str, message: Message):
return
for link in link_text[:MAX_LINKS]:
await process_single_link_func(message, link)
await sleep(.5)
await sleep(0.5)
@bot.on_message(

View File

@ -54,6 +54,22 @@ def format_image_url(url: str) -> str:
return f'<img src="{url}"/>'
def exists_image_url(post_info: PostInfo, url: str) -> bool:
def get_image_key(_url: str) -> str:
# 获取 url 中 / 后面的内容 _ 前面的内容
try:
return _url.split("/")[-1].split("_")[0]
except IndexError:
return ""
if url:
old_key = get_image_key(url)
for __url in post_info.image_urls:
if get_image_key(__url) == old_key:
return True
return False
def parse_tag(tag: Union[Tag, PageElement], post_info: PostInfo) -> str:
if tag.name == "a":
href = tag.get("href")
@ -63,11 +79,7 @@ def parse_tag(tag: Union[Tag, PageElement], post_info: PostInfo) -> str:
return f'<a href="{href}">{tag.get_text()}</a>'
elif tag.name == "img":
src = clean_url(tag.get("src"))
if (
src
and ("upload-bbs.miyoushe.com" in src or "upload-os-bbs.hoyolab.com" in src)
and src in post_info.image_urls
):
if exists_image_url(post_info, src):
return format_image_url(src)
return ""
elif tag.name in ["h1", "h2", "h3", "h4", "h5", "h6"]:
@ -91,7 +103,11 @@ def parse_tag(tag: Union[Tag, PageElement], post_info: PostInfo) -> str:
elif tag.name == "div":
post_text = []
for tag_ in tag.children:
if isinstance(tag_, Tag) and tag_.name == "div" and "ql-image-mask" in tag_["class"]:
if (
isinstance(tag_, Tag)
and tag_.name == "div"
and "ql-image-mask" in tag_["class"]
):
continue
if text := parse_tag(tag_, post_info):
post_text.append(text)
@ -99,11 +115,23 @@ def parse_tag(tag: Union[Tag, PageElement], post_info: PostInfo) -> str:
return replace_br(tag.get_text().strip())
def parse_content(soup: BeautifulSoup, post_info: PostInfo) -> str:
post_text = f"<h1>{post_info.subject}</h1>\n"
def parse_content_pre(_: BeautifulSoup, post_info: PostInfo) -> str:
post_text = ""
if post_info.has_cover and post_info.cover:
post_text += format_image_url(post_info.cover) + "\n"
post_text += f"<h1>{post_info.subject}</h1>\n"
if post_info.video_urls:
for url in post_info.video_urls:
post_text += f'<video controls="controls" src="{url}"></video>\n'
return post_text
def parse_content(soup: BeautifulSoup, post_info: PostInfo) -> str:
post_text = parse_content_pre(soup, post_info)
for tag in soup.find("body").children:
if text := parse_tag(tag, post_info):
post_text += f"{text}\n"

View File

@ -14,11 +14,23 @@ class TestHoyolabArticle:
content = await process_article(21097937, lang="zh")
assert content is not None
assert "【有奖活动】枫丹冒险之旅,参与赢原石等奖励" in content
assert (
"2023/08/22/cabb57e3aab8e212c035ee5dcca79540_7635259955618222979" in content
)
assert "2023/08/22/cabb57e3aab8e212c035ee5dcca79540" in content
assert "参与资格(仅针对现金奖励)" in content
@staticmethod
async def test_get_hoyo_article_text_image():
content = await process_article(30373160, lang="zh")
assert content is not None
assert "2024/06/26/e8f01c57b1444f41ff661697d82eb142" in content
assert "「纺坠终久之梦」系列壁纸现已上架!" in content
@staticmethod
async def test_get_hoyo_article_text_cover():
content = await process_article(30382861, lang="zh")
assert content is not None
assert "<!-- article content -->\n <img src" in content
assert "【菲林奖励】绝区零即将上线,参与前瞻直播讨论赢菲林!" in content
@staticmethod
async def test_get_hoyo_lang_article():
content = await process_article(21124034, lang="en")