Skip to content

Commit

Permalink
ref(plugin): 内置索引插件支持在线更新站点索引配置
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanling committed Jan 10, 2024
1 parent 835d83c commit 30c6def
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 740 deletions.
124 changes: 14 additions & 110 deletions app/plugins/modules/builtinfriend/builtin_friend.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import requests
from app.plugins.modules._base import _IPluginModule
from app.sites_config import SitesConfig
import os
from json import loads, dump
from os.path import join, exists
from os.path import join
from jinja2 import Environment, FileSystemLoader
from .download import ConfigSpider
from apscheduler.schedulers.background import BackgroundScheduler
from urllib.parse import urljoin
from flask.globals import current_app
Expand All @@ -18,16 +15,11 @@


class BuiltinFriend(_IPluginModule):
"""本插件约定站点配置文件如下:

1. 站点配置文件名以 `{id}.json`
2. 站点图标文件地址在站点配置路径下的icon下
3. 站点图标文件名以 `{id}.png`
"""
# 插件名称
module_name = "内建索引器助手"
# 插件描述
module_desc = "预缓存站点图标,新建/编辑索引配置以及定时更新索引源"
module_desc = "预缓存站点图标,更新索引源"
# 插件图标
module_icon = "icon_builtin_friend.svg"
# 主题色
Expand All @@ -49,8 +41,6 @@ class BuiltinFriend(_IPluginModule):
_cron = None
_source = None

_icon_store: str = None

@staticmethod
def get_fields():
return [
Expand All @@ -69,7 +59,6 @@ def init_config(self, config=None):
初始化
"""
self.info(f"初始化{self.module_name}插件")
self._icon_store = self.init_icon_path()

# 读取配置
if config:
Expand All @@ -80,8 +69,7 @@ def init_config(self, config=None):
self.stop_service()
# 启动服务
if self.get_state():
self._scheduler = BackgroundScheduler(
timezone=Config().get_timezone())
self._scheduler = BackgroundScheduler(timezone=Config().get_timezone())
# 任务进行缓存
SCHEDULER_MAP['scheduler'] = self._scheduler
# 启动定时
Expand All @@ -91,29 +79,16 @@ def init_config(self, config=None):
cron=str(self._cron))
# 启动任务
if self._scheduler.get_jobs():
self._scheduler.print_jobs()
self._scheduler.start()

@classmethod
def init_icon_path(*arg, **kwargs):
site_config = SitesConfig()
icon_store_path = join(site_config.sites_config_path, 'icon')
os.makedirs(icon_store_path, exist_ok=True)
return icon_store_path

def get_state(self):
config = self.get_config()
return config and config.get('cron') and config.get('source')

def update_by_source(self, code=None):
site_config = SitesConfig()
if not code:
code = self._source
spider = ConfigSpider.form_crawl(
[code], site_config.sites_config_path, thread_count=16
)
spider.run()
if spider.count > 0:
update_result = site_config.download_file(self._source, site_config.sites_config_bin_path)
if update_result:
self.load_site_config()

@classmethod
Expand Down Expand Up @@ -159,60 +134,22 @@ def add_job(source, cron):
'source': source,
'cron': cron,
}

instance = BuiltinFriend()
instance.update_config(config,
plugin_id=BuiltinFriend.__name__
)
instance.update_config(config, plugin_id=BuiltinFriend.__name__)
# 重新加载任务
instance.init_config(config)
return

@staticmethod
def parse_code(code: str = None, **kwargs):
"""解析配置源
"""
site_config = SitesConfig()
# 只支持网站类的,后续可以考虑文本加密类的
if code.startswith(('http://', 'https://')):
# 解析复杂情况,还是交给采集框架去做吧
spider = ConfigSpider.form_crawl(
[code], site_config.sites_config_path, thread_count=16
)
spider.run()
if spider.count > 0:
BuiltinFriend.load_site_config()
return {"code": 0, "data": spider.count}
return {"code": -1, "msg": '不支持解析此数据'}
return {"code": -1, "msg": "添加定时任务成功"}

@staticmethod
def delete_config(id: str = None, **kwargs):
"""
删除配置文件, 这里是直接删除文件
"""
def update_site_config(source):
site_config = SitesConfig()
base_path = site_config.sites_config_path
path = join(base_path, id + '.json')
if exists(path):
os.remove(path)
update_result = site_config.download_file(source, site_config.sites_config_bin_path)
if update_result:
BuiltinFriend.load_site_config()
return {"code": 0}

@staticmethod
def update_site_config(id: str = None, **kwargs):
"""
更新
"""
site_config = SitesConfig()
config = site_config.sites_config.get(id, {})
source_address = config.get('source_address')
if source_address:
spider = ConfigSpider.form_crawl(
[source_address], site_config.sites_config_path, thread_count=1
)
spider.run()
if spider.count > 0:
BuiltinFriend.load_site_config()
return {"code": 0}
return {"code": 0, "msg": "更新成功"}
else:
return {"code": -1, "msg": "更新失败"}

@staticmethod
def get_configs():
Expand All @@ -221,45 +158,12 @@ def get_configs():
"""
return BuiltinFriend.render('site_list.html')

@staticmethod
def editor(id=None):
"""
编辑文件
"""
from .editor import Editor
configs = SitesConfig().sites_config
editor = Editor(id, configs)
return BuiltinFriend.render(template='edit.html', editor=editor)

@staticmethod
def edit_save(config):
config = loads(config)
sites = SitesConfig()
path = sites.sites_config_path
filename = config.get('id') + '.json'
with open(join(path, filename), 'w', encoding="utf-8") as f:
dump(config, f, ensure_ascii=False, indent=4)

# 图标下载
icon = config.get('icon')
if icon:
icon_store_path = BuiltinFriend.init_icon_path()
response = requests.get(icon, timeout=15)
icon_path = join(icon_store_path, config.get('id') + '.png')
with open(icon_path, 'wb') as f:
f.write(response.content)

# 重新加载配置文件
BuiltinFriend.load_site_config()
return True

@staticmethod
def render(template, **kwargs):
sites_config = SitesConfig()
template_folder = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'templates')

BuiltinFriend.init_icon_path()
# 获取当前 Flask APP 实例
app: Flask = current_app._get_current_object()
loader = FileSystemLoader(template_folder)
Expand Down
30 changes: 0 additions & 30 deletions app/plugins/modules/builtinfriend/dome.json

This file was deleted.

120 changes: 0 additions & 120 deletions app/plugins/modules/builtinfriend/download.py

This file was deleted.

Loading

0 comments on commit 30c6def

Please sign in to comment.