add target parser && add parser for bilibili

This commit is contained in:
felinae98
2022-03-20 11:13:44 +08:00
parent 3d9523f5f3
commit 9c3dc7059e
6 changed files with 139 additions and 3 deletions
+11 -1
View File
@@ -1,11 +1,12 @@
import json
import re
from typing import Any, Optional
import httpx
from ..post import Post
from ..types import Category, RawPost, Tag, Target
from .platform import CategoryNotSupport, NewMessage
from .platform import CategoryNotSupport, NewMessage, Platform
class Bilibili(NewMessage):
@@ -26,6 +27,7 @@ class Bilibili(NewMessage):
schedule_kw = {"seconds": 10}
name = "B站"
has_target = True
parse_target_promot = "请输入用户主页的链接"
async def get_target_name(self, target: Target) -> Optional[str]:
async with httpx.AsyncClient() as client:
@@ -37,6 +39,14 @@ class Bilibili(NewMessage):
return None
return res_data["data"]["name"]
async def parse_target(self, target_text: str) -> Target:
if re.match(r"\d+", target_text):
return Target(target_text)
elif match := re.match(r"(?:https://)?space.bilibili.com/(\d+)", target_text):
return Target(match.group(1))
else:
raise Platform.ParseTargetException()
async def get_sub_list(self, target: Target) -> list[RawPost]:
async with httpx.AsyncClient() as client:
params = {"host_uid": target, "offset": 0, "need_top": 0}
@@ -47,6 +47,7 @@ class Platform(metaclass=RegistryABCMeta, base=True):
enable_tag: bool
store: dict[Target, Any]
platform_name: str
parse_target_promot: Optional[str] = None
@abstractmethod
async def get_target_name(self, target: Target) -> Optional[str]:
@@ -73,6 +74,12 @@ class Platform(metaclass=RegistryABCMeta, base=True):
self.reverse_category[val] = key
self.store = dict()
class ParseTargetException(Exception):
pass
async def parse_target(self, target_string: str) -> Target:
return Target(target_string)
@abstractmethod
def get_tags(self, raw_post: RawPost) -> Optional[Collection[Tag]]:
"Return Tag list of given RawPost"