great change

This commit is contained in:
felinae98
2021-04-26 22:35:05 +08:00
parent 63fa370823
commit 81e402c783
9 changed files with 84 additions and 34 deletions
@@ -2,6 +2,7 @@ from .bilibili import Bilibili
from .rss import Rss
from .weibo import Weibo
from .wechat import Wechat
from .utils import check_sub_target
from .utils import check_sub_target, fetch_and_send
from .platform import PlatformNoTarget
from .utils import platform_manager
@@ -19,6 +19,10 @@ class Bilibili(Platform):
}
platform_name = 'bilibili'
enable_tag = False
enabled = True
is_common = True
schedule_interval = 10
name = 'B站'
@staticmethod
async def get_account_name(target: Target) -> Optional[str]:
@@ -9,21 +9,43 @@ from ..config import Config
from ..plugin_config import plugin_config
from ..post import Post
from ..types import Category, RawPost, Tag, Target, User
from ..utils import Singleton
class CategoryNotSupport(Exception):
"raise in get_category, when post category is not supported"
pass
class PlatformProto(metaclass=Singleton):
class RegistryMeta(type):
def __new__(cls, name, bases, namespace, **kwargs):
if name not in ['PlatformProto', 'Platform', 'PlatformNoTarget'] and \
'platform_name' not in namespace:
raise TypeError('Platform has no `platform_name`')
return super().__new__(cls, name, bases, namespace, **kwargs)
def __init__(cls, name, bases, namespace, **kwargs):
if not hasattr(cls, 'registory'):
# this is the base class
cls.registory = []
elif name not in ['Platform', 'PlatformNoTarget']:
# this is the subclass
cls.registory.append(cls)
super().__init__(name, bases, namespace, **kwargs)
class PlatformProto(metaclass=RegistryMeta):
categories: dict[Category, str]
reverse_category: dict[str, Category]
has_target: bool
platform_name: str
name: str
enable_tag: bool
cache: dict[Any, Post]
enabled: bool
is_common: bool
schedule_interval: int
async def fetch_new_post(self, target: Target, users: list[User]) -> list[tuple[User, list[Post]]]:
...
@@ -14,6 +14,10 @@ class Rss(Platform):
categories = {}
enable_tag = False
platform_name = 'rss'
name = "Rss"
enabled = True
is_common = True
schedule_interval = 30
@staticmethod
async def get_account_name(target: Target) -> Optional[str]:
@@ -2,10 +2,6 @@ import nonebot
from nonebot import logger
from collections import defaultdict
from typing import Type
from .weibo import Weibo
from .bilibili import Bilibili
from .rss import Rss
from .wechat import Wechat
from .platform import PlatformProto
from ..config import Config
from ..post import Post
@@ -15,10 +11,8 @@ async def check_sub_target(target_type, target):
return await platform_manager[target_type].get_account_name(target)
platform_manager: dict[str, PlatformProto] = {
'bilibili': Bilibili(),
'weibo': Weibo(),
'rss': Rss(),
# 'wechat': Wechat(),
obj.platform_name: obj() for obj in \
filter(lambda platform: platform.enabled, PlatformProto.registory)
}
async def fetch_and_send(target_type: str):
@@ -17,6 +17,9 @@ class Wechat(Platform):
categories = {}
enable_tag = False
platform_name = 'wechat'
enabled = False
is_common = False
name = '微信公众号'
@classmethod
def _get_query_url(cls, target: Target):
@@ -21,6 +21,10 @@ class Weibo(Platform):
}
enable_tag = False
platform_name = 'weibo'
name = '新浪微博'
enabled = True
is_common = True
schedule_interval = 10
def __init__(self):
self.top : dict[Target, RawPost] = dict()