add RSS订阅插件

This commit is contained in:
jxxghp
2023-08-17 16:24:24 +08:00
parent 406bff1a3b
commit 0903730ab6
6 changed files with 771 additions and 31 deletions

View File

@ -3,6 +3,7 @@ from pathlib import Path
from threading import Lock
from typing import Optional, Any, List, Dict, Tuple
import pytz
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
@ -55,10 +56,13 @@ class DoubanSync(_PluginBase):
# 配置属性
_enabled: bool = False
_onlyonce: bool = False
_cron: str = ""
_notify: bool = False
_days: int = 7
_users: str = ""
_clear: bool = False
_clearflag: bool = False
def init_plugin(self, config: dict = None):
self.rsshelper = RssHelper()
@ -76,8 +80,10 @@ class DoubanSync(_PluginBase):
self._notify = config.get("notify")
self._days = config.get("days")
self._users = config.get("users")
self._onlyonce = config.get("onlyonce")
self._clear = config.get("clear")
if self._enabled:
if self._enabled or self._onlyonce:
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
if self._cron:
@ -92,6 +98,23 @@ class DoubanSync(_PluginBase):
else:
self._scheduler.add_job(self.sync, "interval", minutes=30, name="豆瓣想看")
if self._onlyonce:
logger.info(f"豆瓣想看服务启动,立即运行一次")
self._scheduler.add_job(func=self.sync, trigger='date',
run_date=datetime.datetime.now(
tz=pytz.timezone(settings.TZ)) + datetime.timedelta(seconds=3)
)
if self._onlyonce or self._clear:
# 关闭一次性开关
self._onlyonce = False
# 记录缓存清理标志
self._clearflag = self._clear
# 关闭清理缓存
self._clear = False
# 保存配置
self.__update_config()
# 启动任务
if self._scheduler.get_jobs():
self._scheduler.print_jobs()
@ -140,7 +163,7 @@ class DoubanSync(_PluginBase):
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
'md': 4
},
'content': [
{
@ -156,7 +179,7 @@ class DoubanSync(_PluginBase):
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
'md': 4
},
'content': [
{
@ -167,6 +190,22 @@ class DoubanSync(_PluginBase):
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'onlyonce',
'label': '立即运行一次',
}
}
]
}
]
},
@ -225,15 +264,38 @@ class DoubanSync(_PluginBase):
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 6
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'clear',
'label': '清理历史记录',
}
}
]
}
]
}
]
}
], {
"enabled": False,
"notify": True,
"onlyonce": False,
"cron": "*/30 * * * *",
"days": 7,
"users": "",
"clear": False
}
def get_page(self) -> List[dict]:
@ -339,6 +401,20 @@ class DoubanSync(_PluginBase):
}
]
def __update_config(self):
"""
更新配置
"""
self.update_config({
"enabled": self._enabled,
"notify": self._notify,
"onlyonce": self._onlyonce,
"cron": self._cron,
"days": self._days,
"users": self._users,
"clear": self._clear
})
def stop_service(self):
"""
退出插件
@ -359,7 +435,10 @@ class DoubanSync(_PluginBase):
if not self._users:
return
# 读取历史记录
history: List[dict] = self.get_data('history') or []
if self._clearflag:
history = []
else:
history: List[dict] = self.get_data('history') or []
for user_id in self._users.split(","):
# 同步每个用户的豆瓣数据
if not user_id:
@ -460,6 +539,8 @@ class DoubanSync(_PluginBase):
logger.info(f"用户 {user_id} 豆瓣想看同步完成")
# 保存历史记录
self.save_data('history', history)
# 缓存只清理一次
self._clearflag = False
@eventmanager.register(EventType.DoubanSync)
def remote_sync(self, event: Event):