Cateon Huo 9e5dcb3912
实现 Post.content 相关扩展协议 (#553)
*  `post` 新增 `get_content()` 方法

* 🚨 make linter happy

* 💄 auto fix by pre-commit hooks

* 🐛 fix: 调整函数使用

* 💄 auto fix by pre-commit hooks

*  转用函数处理文本

* 💄 auto fix by pre-commit hooks

* 🔨 使用`Dict`存储`content_handlers`

* 💄 auto fix by pre-commit hooks

* 🎨 fix

* :arts: 简化函数使用

* 🐛 移除`Theme`的过时参数

* 🗑️ 复用 `self.plain_content`

* 💄 auto fix by pre-commit hooks

*  注册式装饰器写法

* 💄 auto fix by pre-commit hooks

* 🐛 fix

* 💄 auto fix by pre-commit hooks

* :feat: 通用纯文本处理函数

* 💄 auto fix by pre-commit hooks

* :downgrade: 复用`==`处理标题

* 🎨 简化(?)写法

*  测试修复

* ♻️ via ContentSupport extensions

* 🐛 fix test

* 💄 auto fix by pre-commit hooks

* 🐛 for clean text

* 🐛 fix

* 💄 auto fix by pre-commit hooks

* fix: for xml

* 💄 auto fix by pre-commit hooks

* chore: art

* 💄 auto fix by pre-commit hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-08-04 18:39:12 +08:00

134 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from typing import Any
from httpx import AsyncClient
from ..post import Post
from ..utils import Site
from .platform import NewMessage
from ..types import Target, RawPost, ApiError
class NcmSite(Site):
name = "music.163.com"
schedule_type = "interval"
schedule_setting = {"minutes": 1}
class NcmArtist(NewMessage):
categories = {}
platform_name = "ncm-artist"
enable_tag = False
enabled = True
is_common = True
site = NcmSite
name = "网易云-歌手"
has_target = True
parse_target_promot = "请输入歌手主页包含数字ID的链接"
@classmethod
async def get_target_name(cls, client: AsyncClient, target: Target) -> str | None:
res = await client.get(
f"https://music.163.com/api/artist/albums/{target}",
headers={"Referer": "https://music.163.com/"},
)
res_data = res.json()
if res_data["code"] != 200:
raise ApiError(res.request.url)
return res_data["artist"]["name"]
@classmethod
async def parse_target(cls, target_text: str) -> Target:
if re.match(r"^\d+$", target_text):
return Target(target_text)
elif match := re.match(r"(?:https?://)?music\.163\.com/#/artist\?id=(\d+)", target_text):
return Target(match.group(1))
else:
raise cls.ParseTargetException("正确格式:\n1. 歌手数字ID\n2. https://music.163.com/#/artist?id=xxxx")
async def get_sub_list(self, target: Target) -> list[RawPost]:
client = await self.ctx.get_client()
res = await client.get(
f"https://music.163.com/api/artist/albums/{target}",
headers={"Referer": "https://music.163.com/"},
)
res_data = res.json()
if res_data["code"] != 200:
return []
else:
return res_data["hotAlbums"]
def get_id(self, post: RawPost) -> Any:
return post["id"]
def get_date(self, post: RawPost) -> int:
return post["publishTime"] // 1000
async def parse(self, raw_post: RawPost) -> Post:
text = "新专辑发布:{}".format(raw_post["name"])
target_name = raw_post["artist"]["name"]
pics = [raw_post["picUrl"]]
url = "https://music.163.com/#/album?id={}".format(raw_post["id"])
return Post(self, content=text, url=url, images=pics, nickname=target_name)
class NcmRadio(NewMessage):
categories = {}
platform_name = "ncm-radio"
enable_tag = False
enabled = True
is_common = False
site = NcmSite
name = "网易云-电台"
has_target = True
parse_target_promot = "请输入主播电台主页包含数字ID的链接"
@classmethod
async def get_target_name(cls, client: AsyncClient, target: Target) -> str | None:
res = await client.post(
"http://music.163.com/api/dj/program/byradio",
headers={"Referer": "https://music.163.com/"},
data={"radioId": target, "limit": 1000, "offset": 0},
)
res_data = res.json()
if res_data["code"] != 200 or res_data["programs"] == 0:
return
return res_data["programs"][0]["radio"]["name"]
@classmethod
async def parse_target(cls, target_text: str) -> Target:
if re.match(r"^\d+$", target_text):
return Target(target_text)
elif match := re.match(r"(?:https?://)?music\.163\.com/#/djradio\?id=(\d+)", target_text):
return Target(match.group(1))
else:
raise cls.ParseTargetException(
prompt="正确格式:\n1. 电台数字ID\n2. https://music.163.com/#/djradio?id=xxxx"
)
async def get_sub_list(self, target: Target) -> list[RawPost]:
client = await self.ctx.get_client()
res = await client.post(
"http://music.163.com/api/dj/program/byradio",
headers={"Referer": "https://music.163.com/"},
data={"radioId": target, "limit": 1000, "offset": 0},
)
res_data = res.json()
if res_data["code"] != 200:
return []
else:
return res_data["programs"]
def get_id(self, post: RawPost) -> Any:
return post["id"]
def get_date(self, post: RawPost) -> int:
return post["createTime"] // 1000
async def parse(self, raw_post: RawPost) -> Post:
text = "网易云电台更新:{}".format(raw_post["name"])
target_name = raw_post["radio"]["name"]
pics = [raw_post["coverUrl"]]
url = "https://music.163.com/#/program/{}".format(raw_post["id"])
return Post(self, content=text, url=url, images=pics, nickname=target_name)