270 lines
10 KiB
Python
270 lines
10 KiB
Python
from typing import List, Tuple, Dict, Any
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
from app.core.config import settings
|
|
from app.log import logger
|
|
from app.modules.qbittorrent import Qbittorrent
|
|
from app.modules.transmission import Transmission
|
|
from app.plugins import _PluginBase
|
|
|
|
|
|
class SpeedLimiter(_PluginBase):
|
|
# 插件名称
|
|
plugin_name = "播放限速"
|
|
# 插件描述
|
|
plugin_desc = "外网播放媒体库视频时,自动对下载器进行限速。"
|
|
# 插件图标
|
|
plugin_icon = "SpeedLimiter.jpg"
|
|
# 主题色
|
|
plugin_color = "#183883"
|
|
# 插件版本
|
|
plugin_version = "1.0"
|
|
# 插件作者
|
|
plugin_author = "Shurelol"
|
|
# 作者主页
|
|
author_url = "https://github.com/Shurelol"
|
|
# 插件配置项ID前缀
|
|
plugin_config_prefix = "speedlimit_"
|
|
# 加载顺序
|
|
plugin_order = 11
|
|
# 可使用的用户级别
|
|
auth_level = 2
|
|
|
|
# 私有属性
|
|
_scheduler = None
|
|
_qb = None
|
|
_tr = None
|
|
_enabled: bool = False
|
|
_notify: bool = False
|
|
_interval: int = 60
|
|
_downloader: list = []
|
|
_play_up_speed: float = 0
|
|
_play_down_speed: float = 0
|
|
_noplay_up_speed: float = 0
|
|
_noplay_down_speed: float = 0
|
|
|
|
def init_plugin(self, config: dict = None):
|
|
# 读取配置
|
|
if config:
|
|
self._enabled = config.get("enabled")
|
|
self._notify = config.get("notify")
|
|
self._play_up_speed = float(config.get("play_up_speed")) if config.get("play_up_speed") else 0
|
|
self._play_down_speed = float(config.get("play_down_speed")) if config.get("play_down_speed") else 0
|
|
self._noplay_up_speed = float(config.get("noplay_up_speed")) if config.get("noplay_up_speed") else 0
|
|
self._noplay_down_speed = float(config.get("noplay_down_speed")) if config.get("noplay_down_speed") else 0
|
|
self._downloader = config.get("downloader") or []
|
|
if self._downloader:
|
|
if 'qbittorrent' in self._downloader:
|
|
self._qb = Qbittorrent()
|
|
if 'transmission' in self._downloader:
|
|
self._tr = Transmission()
|
|
|
|
# 移出现有任务
|
|
self.stop_service()
|
|
|
|
# 启动限速任务
|
|
if self._enabled:
|
|
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
|
|
self._scheduler.add_job(func=self.__check_playing_sessions,
|
|
trigger='interval',
|
|
seconds=self._interval,
|
|
name="播放限速检查")
|
|
self._scheduler.print_jobs()
|
|
self._scheduler.start()
|
|
logger.info("播放限速检查服务启动")
|
|
|
|
def get_state(self) -> bool:
|
|
return self._enabled
|
|
|
|
@staticmethod
|
|
def get_command() -> List[Dict[str, Any]]:
|
|
pass
|
|
|
|
def get_api(self) -> List[Dict[str, Any]]:
|
|
pass
|
|
|
|
def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
|
|
return [
|
|
{
|
|
'component': 'VForm',
|
|
'content': [
|
|
{
|
|
'component': 'VRow',
|
|
'content': [
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VSwitch',
|
|
'props': {
|
|
'model': 'enabled',
|
|
'label': '启用插件',
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VSwitch',
|
|
'props': {
|
|
'model': 'notify',
|
|
'label': '发送通知',
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VRow',
|
|
'content': [
|
|
{
|
|
'component': 'VCol',
|
|
'content': [
|
|
{
|
|
'component': 'VSelect',
|
|
'props': {
|
|
'chips': True,
|
|
'multiple': True,
|
|
'model': 'sign_sites',
|
|
'label': '下载器',
|
|
'items': [
|
|
{'title': 'Qbittorrent', 'value': 'qbittorrent'},
|
|
{'title': 'Transmission', 'value': 'transmission'},
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VRow',
|
|
'content': [
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VTextField',
|
|
'props': {
|
|
'model': 'play_up_speed',
|
|
'label': '播放限速(上传)',
|
|
'placeholder': 'KB/s'
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VTextField',
|
|
'props': {
|
|
'model': 'play_down_speed',
|
|
'label': '播放限速(下载)',
|
|
'placeholder': 'KB/s'
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VRow',
|
|
'content': [
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VTextField',
|
|
'props': {
|
|
'model': 'noplay_up_speed',
|
|
'label': '未播放限速(上传)',
|
|
'placeholder': 'KB/s'
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'component': 'VCol',
|
|
'props': {
|
|
'cols': 12,
|
|
'md': 6
|
|
},
|
|
'content': [
|
|
{
|
|
'component': 'VTextField',
|
|
'props': {
|
|
'model': 'noplay_down_speed',
|
|
'label': '未播放限速(下载)',
|
|
'placeholder': 'KB/s'
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
], {
|
|
"enabled": False,
|
|
"notify": True,
|
|
"downloader": [],
|
|
"play_up_speed": 0,
|
|
"play_down_speed": 0,
|
|
"noplay_up_speed": 0,
|
|
"noplay_down_speed": 0,
|
|
}
|
|
|
|
def get_page(self) -> List[dict]:
|
|
pass
|
|
|
|
def __check_playing_sessions(self):
|
|
"""
|
|
检查播放会话
|
|
"""
|
|
if not self._qb and not self._tr:
|
|
return
|
|
|
|
def __set_limiter(self):
|
|
"""
|
|
设置限速
|
|
"""
|
|
pass
|
|
|
|
def stop_service(self):
|
|
"""
|
|
退出插件
|
|
"""
|
|
try:
|
|
if self._scheduler:
|
|
self._scheduler.remove_all_jobs()
|
|
if self._scheduler.running:
|
|
self._scheduler.shutdown()
|
|
self._scheduler = None
|
|
except Exception as e:
|
|
print(str(e))
|