Skip to content

Commit

Permalink
ref(plugin): builtin friend 去除警告
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanling committed Jan 9, 2024
1 parent a3b660f commit 835d83c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 57 deletions.
99 changes: 54 additions & 45 deletions app/plugins/modules/builtinfriend/builtin_friend.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
from app.utils import SchedulerUtils

SCHEDULER_MAP = {}


class BuiltinFriend(_IPluginModule):
'''本插件约定站点配置文件如下:
"""本插件约定站点配置文件如下:
1. 站点配置文件名以 `{id}.json`
2. 站点图标文件地址在站点配置路径下的icon下
3. 站点图标文件名以 `{id}.png`
'''
"""
# 插件名称
module_name = "Builtin Friend"
module_name = "内建索引器助手"
# 插件描述
module_desc = "内建索引器的朋友"
module_desc = "预缓存站点图标,新建/编辑索引配置以及定时更新索引源"
# 插件图标
module_icon = "icon_builtin_friend.svg"
# 主题色
Expand Down Expand Up @@ -54,17 +56,18 @@ def get_fields():
return [
]


def load_site_config(*_):
'''重新加载配置文件
'''
"""
重新加载配置文件
"""
site_config = SitesConfig()
site_config.init_config()
site_config.get_site_config_info.cache_clear()

def init_config(self, config=None):
'''初始化
'''
"""
初始化
"""
self.info(f"初始化{self.module_name}插件")
self._icon_store = self.init_icon_path()

Expand Down Expand Up @@ -94,7 +97,7 @@ def init_config(self, config=None):
@classmethod
def init_icon_path(*arg, **kwargs):
site_config = SitesConfig()
icon_store_path = join(site_config._sites_config_path, 'icon')
icon_store_path = join(site_config.sites_config_path, 'icon')
os.makedirs(icon_store_path, exist_ok=True)
return icon_store_path

Expand All @@ -107,24 +110,25 @@ def update_by_source(self, code=None):
if not code:
code = self._source
spider = ConfigSpider.form_crawl(
[code], site_config._sites_config_path, thread_count=16
[code], site_config.sites_config_path, thread_count=16
)
spider.run()
if spider.count > 0:
self.load_site_config()

@classmethod
def icon(cls, config):
'''获取站点logo
'''
"""
获取站点logo
"""
configs = SitesConfig()
icon_store_path = join(configs._sites_config_path, 'icon')
icon_file = join(icon_store_path, config['id']+'.png')
icon_store_path = join(configs.sites_config_path, 'icon')
icon_file = join(icon_store_path, config['id'] + '.png')
# 优先使用本插件下载的图标, 因为可能是最新的
if os.path.exists(icon_file):
with open(icon_file, 'rb') as f:
content = b64encode(f.read()).decode()
return 'data:image/jpeg;base64,'+content
return 'data:image/jpeg;base64,' + content

# 使用配置里填写的地址,很大概率是无法正常显示的
# 因为能下载的都下载了
Expand All @@ -148,8 +152,9 @@ def get_custom_modal(self):

@staticmethod
def add_job(source, cron):
'''添加定时任务
'''
"""
添加定时任务
"""
config = {
'source': source,
'cron': cron,
Expand All @@ -171,7 +176,7 @@ def parse_code(code: str = None, **kwargs):
if code.startswith(('http://', 'https://')):
# 解析复杂情况,还是交给采集框架去做吧
spider = ConfigSpider.form_crawl(
[code], site_config._sites_config_path, thread_count=16
[code], site_config.sites_config_path, thread_count=16
)
spider.run()
if spider.count > 0:
Expand All @@ -181,27 +186,28 @@ def parse_code(code: str = None, **kwargs):

@staticmethod
def delete_config(id: str = None, **kwargs):
'''
"""
删除配置文件, 这里是直接删除文件
'''
"""
site_config = SitesConfig()
base_path = site_config._sites_config_path
path = join(base_path, id+'.json')
base_path = site_config.sites_config_path
path = join(base_path, id + '.json')
if exists(path):
os.remove(path)
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, {})
soure_address = config.get('soure_address')
if soure_address:
config = site_config.sites_config.get(id, {})
source_address = config.get('source_address')
if source_address:
spider = ConfigSpider.form_crawl(
[soure_address], site_config._sites_config_path, thread_count=1
[source_address], site_config.sites_config_path, thread_count=1
)
spider.run()
if spider.count > 0:
Expand All @@ -210,25 +216,27 @@ def update_site_config(id: str = None, **kwargs):

@staticmethod
def get_configs():
'''获取配置信息
'''
"""
获取配置信息
"""
return BuiltinFriend.render('site_list.html')

@staticmethod
def editor(id=None):
'''编辑文件
'''
"""
编辑文件
"""
from .editor import Editor
configs = SitesConfig()._sites_config
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'
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)

Expand All @@ -237,7 +245,7 @@ def edit_save(config):
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')
icon_path = join(icon_store_path, config.get('id') + '.png')
with open(icon_path, 'wb') as f:
f.write(response.content)

Expand All @@ -263,20 +271,21 @@ def render(template, **kwargs):
env.globals['p'] = BuiltinFriend
template = env.get_template(template)
plugin_setting = BuiltinFriend().get_config()
return template.render(sites_config=sites_config._sites_config,
return template.render(sites_config=sites_config.sites_config,
plugin_setting=plugin_setting,
**kwargs)

@staticmethod
def cron_test(code):
'''验证cron表达的合法性, 同时给出补全后的表达式
"""
验证cron表达的合法性, 同时给出补全后的表达式
todo 还可以给出表达式的描述 cron_descriptor测试结果不太理想
'''
"""
# pip3 install python-crontab cron_descriptor
from crontab import CronSlices
# 补齐表达式
# 补充表达式
fragment = code.split(' ')
fragment = fragment+["*"] * (5-len(fragment))
fragment = fragment + ["*"] * (5 - len(fragment))
expression = ' '.join(fragment)
return {
'state': CronSlices.is_valid(expression),
Expand All @@ -288,11 +297,11 @@ def stop_service(self):
退出插件
"""
try:
_scheduler = SCHEDULER_MAP.get('scheduler')
_scheduler = SCHEDULER_MAP.get('scheduler')
if _scheduler:
_scheduler.remove_all_jobs()
if _scheduler.running:
_scheduler.shutdown()
self._scheduler = None
self._scheduler = None
except Exception as e:
print(str(e))
4 changes: 2 additions & 2 deletions app/plugins/modules/builtinfriend/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ConfigSpider(feapder.AirSpider):
EXPORT_DATA_MAX_FAILED_TIMES=0,
EXPORT_DATA_MAX_RETRY_TIMES=0,
SPIDER_MAX_RETRY_TIMES=0,
KEEP_ALIVE = False
KEEP_ALIVE=False
)

@classmethod
Expand Down Expand Up @@ -51,7 +51,7 @@ def parse(self, request, response:feapder.Response):

try:
data = loads(body)
data['soure_address'] = response.url
data['source_address'] = response.url
if self.check_keys(data):
self.save(data)
icon = data.get('icon')
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/modules/builtinfriend/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self, id, configs):
self._can_edit =False
self.is_mew = False
self.config = configs.get(id) or self.defualt_config()
if not self.config.get('soure_address'):
if not self.config.get('source_address'):
# 不是拉取的数据都可以编辑
self._can_edit =True
self.site_id = id
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/modules/builtinfriend/templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="col-auto">
<button
class="col-auto btn btn-ghost-light btn-sm"
onclick="BuiltinFriend_parse_code('{{editor.config.soure_address}}')"
onclick="BuiltinFriend_parse_code('{{editor.config.source_address}}')"
data-bs-dismiss="modal"
>
更新
Expand Down
10 changes: 7 additions & 3 deletions app/plugins/modules/builtinfriend/templates/form.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<fieldset class="form-fieldset">
<div class="mb-3">
<input id="builtin-friend__code" class="form-control" type="text"
value="{{plugin_setting.source}}"
placeholder="输源地址或者暗号">
value="{% if plugin_setting.source %}{{ plugin_setting.source }}{% else %}https://raw.githubusercontent.com/hsuyelin/nas-tools-sites/master/user.sites.bin{% endif %}"
placeholder="索引源地址">
</div>
<div class="mb-3">
<input id="builtin-friend__cron" class="form-control" type="text"
value="{{plugin_setting.cron}}"
value="{% if plugin_setting.cron %}{{ plugin_setting.cron }}{% else %}0 4 * * *{% endif %}"
placeholder="cron表达式, 将定时进行拉取">
</div>
<div class="mb-3">
Expand All @@ -16,6 +16,10 @@
</fieldset>

<script>
// 由于被索引源和计划时间设置了默认值,因此设置初始状态为false
$("#builtin-friend__code_run").attr("disabled", false);
$("#builtin-friend__code_timer").attr("disabled", false);

$("#builtin-friend__code").on("input propertychange", function () {
let code = $('#builtin-friend__code').val();
if (code) {
Expand Down
8 changes: 4 additions & 4 deletions app/plugins/modules/builtinfriend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def wirte_configs(site_configs, data):
for url, config in data:
path = join(base_path, config['id']+'.json')
# 便于后面自动更新
config['soure_address'] = url
config['source_address'] = url
with open(path,'w') as f:
dump(config, f, ensure_ascii=True, indent=4)
count += 1
Expand All @@ -83,9 +83,9 @@ def download_icon(url, name):

def update_config(site_configs,id):
config = site_configs.get(id)
soure_address = site_configs.get(soure_address)
if config and soure_address:
ret = get_http_data(soure_address)
source_address = site_configs.get(source_address)
if config and source_address:
ret = get_http_data(source_address)
return wirte_configs(site_configs, ret)


Expand Down
24 changes: 23 additions & 1 deletion app/sites_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.utils.commons import singleton
from app.utils import StringUtils, RequestUtils


@singleton
class SitesConfig:
# 环境变量 站点配置文件夹路径
Expand Down Expand Up @@ -153,4 +154,25 @@ def download_file(url, filepath):
return True
except Exception as e:
log.error(f"【Sites Config】下载默认的站点配置文件失败: {e}")
return False
return False

@property
def sites_config(self):
"""
对外暴露的站点配置
"""
return self._sites_config

@property
def sites_config_bin_path(self):
"""
对外暴露的索引文件保存路径
"""
return self._sites_config_bin_path

@property
def sites_config_path(self):
"""
对外暴露的索引文件所在文件夹的路径
"""
return self._sites_config_path

0 comments on commit 835d83c

Please sign in to comment.