add 插件API
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, List, Dict
|
||||
|
||||
from app.chain import ChainBase
|
||||
from app.core.config import settings
|
||||
@@ -27,8 +27,6 @@ class _PluginBase(metaclass=ABCMeta):
|
||||
- update_config() 更新配置信息
|
||||
- init_plugin() 生效配置信息
|
||||
- get_data_path() 获取插件数据保存目录
|
||||
- get_command() 获取插件命令,使用消息机制通过远程控制
|
||||
|
||||
"""
|
||||
# 插件名称
|
||||
plugin_name: str = ""
|
||||
@@ -48,6 +46,20 @@ class _PluginBase(metaclass=ABCMeta):
|
||||
"""
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
@abstractmethod
|
||||
def get_command() -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取插件命令
|
||||
[{
|
||||
"cmd": "/xx",
|
||||
"event": EventType.xx,
|
||||
"desc": "xxxx",
|
||||
"data": {}
|
||||
}]
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def stop_service(self):
|
||||
"""
|
||||
|
@@ -2,12 +2,13 @@ import traceback
|
||||
from multiprocessing.dummy import Pool as ThreadPool
|
||||
from multiprocessing.pool import ThreadPool
|
||||
from threading import Event
|
||||
from typing import Any
|
||||
from typing import Any, List, Dict
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from ruamel.yaml import CommentedMap
|
||||
|
||||
from app import schemas
|
||||
from app.core.event import EventManager, eventmanager
|
||||
from app.core.config import settings
|
||||
from app.helper.browser import PlaywrightHelper
|
||||
@@ -18,6 +19,7 @@ from app.log import logger
|
||||
from app.plugins import _PluginBase
|
||||
from app.utils.http import RequestUtils
|
||||
from app.utils.site import SiteUtils
|
||||
from app.utils.string import StringUtils
|
||||
from app.utils.timer import TimerUtils
|
||||
from app.schemas.types import EventType
|
||||
|
||||
@@ -64,17 +66,17 @@ class AutoSignIn(_PluginBase):
|
||||
self._scheduler.start()
|
||||
|
||||
@staticmethod
|
||||
def get_command() -> dict:
|
||||
def get_command() -> List[Dict[str, Any]]:
|
||||
"""
|
||||
定义远程控制命令
|
||||
:return: 命令关键字、事件、描述、附带数据
|
||||
"""
|
||||
return {
|
||||
return [{
|
||||
"cmd": "/site_signin",
|
||||
"event": EventType.SiteSignin,
|
||||
"desc": "站点签到",
|
||||
"data": {}
|
||||
}
|
||||
}]
|
||||
|
||||
@eventmanager.register(EventType.SiteSignin)
|
||||
def sign_in(self, event: Event = None):
|
||||
@@ -110,6 +112,23 @@ class AutoSignIn(_PluginBase):
|
||||
logger.error("站点模块加载失败:%s" % str(e))
|
||||
return None
|
||||
|
||||
def signin_by_domain(self, url: str) -> schemas.Response:
|
||||
"""
|
||||
签到一个站点,可由API调用
|
||||
"""
|
||||
domain = StringUtils.get_url_domain(url)
|
||||
site_info = self.sites.get_indexer(domain)
|
||||
if site_info:
|
||||
return schemas.Response(
|
||||
success=True,
|
||||
message=f"站点【{url}】不存在"
|
||||
)
|
||||
else:
|
||||
return schemas.Response(
|
||||
success=True,
|
||||
message=self.signin_site(site_info)
|
||||
)
|
||||
|
||||
def signin_site(self, site_info: CommentedMap) -> str:
|
||||
"""
|
||||
签到一个站点
|
||||
|
@@ -1,12 +1,13 @@
|
||||
from datetime import datetime
|
||||
from multiprocessing.dummy import Pool as ThreadPool
|
||||
from threading import Lock
|
||||
from typing import Optional, Any, List
|
||||
from typing import Optional, Any, List, Dict
|
||||
|
||||
import requests
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from ruamel.yaml import CommentedMap
|
||||
|
||||
from app import schemas
|
||||
from app.core.config import settings
|
||||
from app.core.event import eventmanager
|
||||
from app.core.event import Event
|
||||
@@ -64,17 +65,17 @@ class SiteStatistic(_PluginBase):
|
||||
self._scheduler.start()
|
||||
|
||||
@staticmethod
|
||||
def get_command() -> dict:
|
||||
def get_command() -> List[Dict[str, Any]]:
|
||||
"""
|
||||
定义远程控制命令
|
||||
:return: 命令关键字、事件、描述、附带数据
|
||||
"""
|
||||
return {
|
||||
return [{
|
||||
"cmd": "/site_statistic",
|
||||
"event": EventType.SiteStatistic,
|
||||
"desc": "站点数据统计",
|
||||
"data": {}
|
||||
}
|
||||
}]
|
||||
|
||||
def stop_service(self):
|
||||
pass
|
||||
@@ -181,6 +182,28 @@ class SiteStatistic(_PluginBase):
|
||||
return site_schema(site_name, url, site_cookie, html_text, session=session, ua=ua, proxy=proxy)
|
||||
return None
|
||||
|
||||
def refresh_by_domain(self, domain: str) -> schemas.Response:
|
||||
"""
|
||||
刷新一个站点数据,可由API调用
|
||||
"""
|
||||
site_info = self.sites.get_indexer(domain)
|
||||
if site_info:
|
||||
site_data = self.__refresh_site_data(site_info)
|
||||
if site_data:
|
||||
return schemas.Response(
|
||||
success=True,
|
||||
message=f"站点 {domain} 刷新成功",
|
||||
data=site_data.to_dict()
|
||||
)
|
||||
return schemas.Response(
|
||||
success=False,
|
||||
message=f"站点 {domain} 刷新数据失败,未获取到数据"
|
||||
)
|
||||
return schemas.Response(
|
||||
success=False,
|
||||
message=f"站点 {domain} 不存在"
|
||||
)
|
||||
|
||||
def __refresh_site_data(self, site_info: CommentedMap) -> Optional[ISiteUserInfo]:
|
||||
"""
|
||||
更新单个site 数据信息
|
||||
|
Reference in New Issue
Block a user