🐛 Fix cover is too big

This commit is contained in:
xtaodada 2023-08-24 18:02:05 +08:00
parent 8f34ecbb5b
commit c5db80b54d
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 38 additions and 32 deletions

View File

@ -28,29 +28,6 @@ class Hyperion:
def get_headers(self, referer: str = "https://www.miyoushe.com/ys/"):
return {"User-Agent": self.USER_AGENT, "Referer": referer}
@staticmethod
def get_images_params(
resize: int = 600,
quality: int = 80,
auto_orient: int = 0,
interlace: int = 1,
images_format: str = "jpg",
) -> str:
"""
image/resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,jpg
:param resize: 图片大小
:param quality: 图片质量
:param auto_orient: 自适应
:param interlace: 未知
:param images_format: 图片格式
:return:
"""
params = (
f"image/resize,s_{resize}/quality,q_{quality}/auto-orient,"
f"{auto_orient}/interlace,{interlace}/format,{images_format}"
)
return f"?x-oss-process={params}"
async def get_official_recommended_posts(self, gids: int) -> List[PostRecommend]:
params = {"gids": gids}
response = await self.client.get(

View File

@ -11,6 +11,8 @@ __all__ = (
"GAME_ID_MAP",
"GAME_STR_MAP",
"CHANNEL_MAP",
"clean_url",
"get_images_params",
"PostStat",
"PostType",
"HoYoPostMultiLang",
@ -19,6 +21,37 @@ __all__ = (
)
def clean_url(url: str) -> str:
if not url:
return ""
return url.replace(
"hoyolab-upload-private.hoyolab.com", "upload-os-bbs.hoyolab.com"
).split("?")[0]
def get_images_params(
resize: int = 600,
quality: int = 80,
auto_orient: int = 0,
interlace: int = 1,
images_format: str = "jpg",
) -> str:
"""
image/resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,jpg
:param resize: 图片大小
:param quality: 图片质量
:param auto_orient: 自适应
:param interlace: 未知
:param images_format: 图片格式
:return:
"""
params = (
f"image/resize,s_{resize}/quality,q_{quality}/auto-orient,"
f"{auto_orient}/interlace,{interlace}/format,{images_format}"
)
return f"?x-oss-process={params}"
class PostStat(BaseModel):
reply_num: int = 0
forward_num: int = Field(
@ -136,6 +169,8 @@ class PostInfo(BaseModel):
cover = cover_list[0]["url"]
if (not cover) and image_urls:
cover = image_urls[0]
if cover:
cover = clean_url(cover) + get_images_params()
game_id = post["game_id"]
topics = [
PostTopic(game_id_=game_id, hoyolab=hoyolab, **topic)

View File

@ -13,6 +13,8 @@ from src.api.models import (
PostRecommend,
CHANNEL_MAP,
GAME_ID_MAP,
get_images_params,
clean_url,
)
from src.env import DOMAIN, MIYOUSHE
from src.error import ArticleNotFoundError
@ -42,17 +44,9 @@ def get_description(soup: BeautifulSoup) -> str:
return post_text
def clean_url(url: str) -> str:
if not url:
return ""
return url.replace(
"hoyolab-upload-private.hoyolab.com", "upload-os-bbs.hoyolab.com"
).split("?")[0]
def format_image_url(url: str) -> str:
if url.endswith(".png") or url.endswith(".jpg"):
url += Hyperion.get_images_params()
url += get_images_params()
return f'<img src="{url}"/>'