-
Notifications
You must be signed in to change notification settings - Fork 70
/
initializer.py
401 lines (367 loc) · 15.4 KB
/
initializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import json
import os
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from werkzeug.security import generate_password_hash
import log
from app.conf import SystemConfig
from app.helper import DbHelper, PluginHelper
from app.plugins import PluginManager
from app.media import Category
from app.utils import ConfigLoadCache, CategoryLoadCache, ExceptionUtils, StringUtils
from app.utils.commons import INSTANCES
from app.utils.types import SystemConfigKey
from config import Config
from web.action import WebAction
_observer = Observer(timeout=10)
def check_config():
"""
检查配置文件,如有错误进行日志输出
"""
# 检查日志输出
if Config().get_config('app'):
logtype = Config().get_config('app').get('logtype')
if logtype:
log.info(f"日志输出类型为:{logtype}")
if logtype == "server":
logserver = Config().get_config('app').get('logserver')
if not logserver:
log.warn("【Config】日志中心地址未配置,无法正常输出日志")
else:
log.info("日志将上送到服务器:{logserver}")
elif logtype == "file":
logpath = Config().get_config('app').get('logpath')
if not logpath:
log.warn("【Config】日志文件路径未配置,无法正常输出日志")
else:
log.info(f"日志将写入文件:{logpath}")
# 检查WEB端口
web_port = Config().get_config('app').get('web_port')
if not web_port:
log.warn("【Config】WEB服务端口未设置,将使用默认3000端口")
# 检查登录用户和密码
login_user = Config().get_config('app').get('login_user')
login_password = Config().get_config('app').get('login_password')
if not login_user or not login_password:
log.warn("【Config】WEB管理用户或密码未设置,将使用默认用户:admin,密码:password")
else:
log.info(f"WEB管理页面用户:{str(login_user)}")
# 检查HTTPS
ssl_cert = Config().get_config('app').get('ssl_cert')
ssl_key = Config().get_config('app').get('ssl_key')
if not ssl_cert or not ssl_key:
log.info(f"未启用https,请使用 http://IP:{str(web_port)} 访问管理页面")
else:
if not os.path.exists(ssl_cert):
log.warn(f"【Config】ssl_cert文件不存在:{ssl_cert}")
if not os.path.exists(ssl_key):
log.warn(f"【Config】ssl_key文件不存在:{ssl_key}")
log.info(f"已启用https,请使用 https://IP:{str(web_port)} 访问管理页面")
else:
log.error("【Config】配置文件格式错误,找不到app配置项!")
def update_config():
"""
升级配置文件
"""
_config = Config().get_config()
_dbhelper = DbHelper()
overwrite_cofig = False
# 密码初始化
login_password = _config.get("app", {}).get("login_password") or "password"
if login_password and not login_password.startswith("[hash]"):
_config['app']['login_password'] = "[hash]%s" % generate_password_hash(
login_password)
overwrite_cofig = True
# API密钥初始化
if not _config.get("security", {}).get("api_key"):
_config['security']['api_key'] = StringUtils.generate_random_str(32)
overwrite_cofig = True
# 字幕兼容旧配置
try:
subtitle = Config().get_config('subtitle') or {}
if subtitle:
if subtitle.get("server") == "opensubtitles":
PluginManager().save_plugin_config(pid="OpenSubtitles",
conf={
"enable": subtitle.get("opensubtitles", {}).get("enable")
})
else:
chinesesubfinder = subtitle.get("chinesesubfinder", {})
PluginManager().save_plugin_config(pid="ChineseSubFinder", conf={
"host": chinesesubfinder.get("host"),
"api_key": chinesesubfinder.get("api_key"),
"local_path": chinesesubfinder.get("local_path"),
"remote_path": chinesesubfinder.get("remote_path")
})
# 删除旧配置
_config.pop("subtitle")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 自定义制作组/字幕组兼容旧配置
try:
custom_release_groups = (Config().get_config('laboratory') or {}).get('release_groups')
if custom_release_groups:
PluginManager().save_plugin_config(pid="CustomReleaseGroups", conf={
"release_groups": custom_release_groups
})
# 删除旧配置
_config["laboratory"].pop("release_groups")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 下载器兼容旧配置
try:
# pt
pt = Config().get_config('pt')
pt_client = pt.get("pt_client")
pt_monitor = pt.get("pt_monitor")
pt_monitor_only = pt.get("pt_monitor_only")
rmt_mode = pt.get("rmt_mode")
# downloaddir
download_dir_conf = []
downloaddir = Config().get_config('downloaddir')
if downloaddir:
for dl_dir in downloaddir:
download_dir_conf.append({
"save_path": dl_dir.get("save_path"),
"type": dl_dir.get("type"),
"category": dl_dir.get("category"),
"container_path": dl_dir.get("container_path"),
"label": dl_dir.get("label")
})
_config.pop("downloaddir")
overwrite_cofig = True
downloaddir = json.dumps(download_dir_conf)
# qbittorrent
qbittorrent = Config().get_config('qbittorrent')
if qbittorrent:
enabled = 1 if pt_client == "qbittorrent" else 0
transfer = 1 if pt_monitor else 0
only_nastool = 1 if pt_monitor_only else 0
config = json.dumps({
"host": qbittorrent.get("qbhost"),
"port": qbittorrent.get("qbport"),
"username": qbittorrent.get("qbusername"),
"password": qbittorrent.get("qbpassword")
})
_dbhelper.update_downloader(did=None,
name="Qbittorrent",
dtype="qbittorrent",
enabled=enabled,
transfer=transfer,
only_nastool=only_nastool,
rmt_mode=rmt_mode,
config=config,
download_dir=downloaddir)
_config.pop("qbittorrent")
overwrite_cofig = True
# transmission
transmission = Config().get_config('transmission')
if transmission:
enabled = 1 if pt_client == "transmission" else 0
transfer = 1 if pt_monitor else 0
only_nastool = 1 if pt_monitor_only else 0
config = json.dumps({
"host": transmission.get("trhost"),
"port": transmission.get("trport"),
"username": transmission.get("trusername"),
"password": transmission.get("trpassword")
})
_dbhelper.update_downloader(did=None,
name="Transmission",
dtype="transmission",
enabled=enabled,
transfer=transfer,
only_nastool=only_nastool,
rmt_mode=rmt_mode,
config=config,
download_dir=downloaddir)
_config.pop("transmission")
overwrite_cofig = True
# pt
if pt_client is not None:
pt.pop("pt_client")
if pt_monitor is not None:
pt.pop("pt_monitor")
if pt_monitor_only is not None:
pt.pop("pt_monitor_only")
if rmt_mode is not None:
pt.pop("rmt_mode")
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 站点数据刷新时间默认配置
try:
if "ptrefresh_date_cron" not in _config['pt']:
_config['pt']['ptrefresh_date_cron'] = '6'
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 豆瓣配置转为插件
try:
douban = Config().get_config('douban')
if douban:
_enable = True if douban.get("users") and douban.get("interval") and douban.get("types") else False
PluginManager().save_plugin_config(pid="DoubanSync", conf={
"onlyonce": False,
"enable": _enable,
"interval": douban.get("interval"),
"auto_search": douban.get("auto_search"),
"auto_rss": douban.get("auto_rss"),
"cookie": douban.get("cookie"),
"users": douban.get("users"),
"days": douban.get("days"),
"types": douban.get("types")
})
# 删除旧配置
_config.pop("douban")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 刮削配置改为存数据库
try:
scraper_conf = {}
# Nfo
scraper_nfo = Config().get_config("scraper_nfo")
if scraper_nfo:
scraper_conf["scraper_nfo"] = scraper_nfo
_config.pop("scraper_nfo")
overwrite_cofig = True
# 图片
scraper_pic = Config().get_config("scraper_pic")
if scraper_pic:
scraper_conf["scraper_pic"] = scraper_pic
_config.pop("scraper_pic")
overwrite_cofig = True
# 保存
if scraper_conf:
SystemConfig().set(SystemConfigKey.UserScraperConf,
scraper_conf)
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 内建索引器配置改为存数据库
try:
indexer_sites = Config().get_config("pt").get("indexer_sites")
if indexer_sites:
SystemConfig().set(SystemConfigKey.UserIndexerSites,
indexer_sites)
_config['pt'].pop("indexer_sites")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 站点签到转为插件
try:
ptsignin_cron = Config().get_config("pt").get("ptsignin_cron")
if ptsignin_cron:
# 转换周期
ptsignin_cron = str(ptsignin_cron).strip()
if ptsignin_cron.isdigit():
cron = f"0 */{ptsignin_cron} * * *"
elif ptsignin_cron.count(" ") == 4:
cron = ptsignin_cron
elif "-" in ptsignin_cron:
ptsignin_cron = ptsignin_cron.split("-")[0]
hour = int(ptsignin_cron.split(":")[0])
minute = int(ptsignin_cron.split(":")[1])
cron = f"{minute} {hour} * * *"
elif ptsignin_cron.count(":"):
hour = int(ptsignin_cron.split(":")[0])
minute = int(ptsignin_cron.split(":")[1])
cron = f"{minute} {hour} * * *"
else:
cron = "30 8 * * *"
# 安装插件
WebAction().install_plugin(data={"id": "AutoSignIn"}, reload=False)
# 保存配置
PluginManager().save_plugin_config(pid="AutoSignIn", conf={
"enabled": True,
"cron": cron,
"retry_keyword": '',
"sign_sites": [],
"special_sites": [],
"notify": True,
"onlyonce": False,
"queue_cnt": 10
})
_config['pt'].pop("ptsignin_cron")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 存量插件安装情况统计
try:
plugin_report_state = SystemConfig().get(SystemConfigKey.UserInstalledPluginsReport)
installed_plugins = SystemConfig().get(SystemConfigKey.UserInstalledPlugins)
if not plugin_report_state and installed_plugins:
ret = PluginHelper().report(installed_plugins)
if ret:
SystemConfig().set(SystemConfigKey.UserInstalledPluginsReport, '1')
except Exception as e:
ExceptionUtils.exception_traceback(e)
# TMDB代理服务开关迁移
try:
tmdb_proxy = Config().get_config('laboratory').get("tmdb_proxy")
if tmdb_proxy:
_config['app']['tmdb_domain'] = 'tmdb.nastool.cn'
_config['laboratory'].pop("tmdb_proxy")
overwrite_cofig = True
except Exception as e:
ExceptionUtils.exception_traceback(e)
# 重写配置文件
if overwrite_cofig:
Config().save_config(_config)
class ConfigMonitor(FileSystemEventHandler):
"""
配置文件变化响应
"""
def __init__(self):
FileSystemEventHandler.__init__(self)
def on_modified(self, event):
if event.is_directory:
return
src_path = event.src_path
file_name = os.path.basename(src_path)
file_head, file_ext = os.path.splitext(os.path.basename(file_name))
if file_ext != ".yaml":
return
# 配置文件10秒内只能加载一次
if file_name == "config.yaml" and not ConfigLoadCache.get(src_path):
ConfigLoadCache.set(src_path, True)
CategoryLoadCache.set("ConfigLoadBlock", True, ConfigLoadCache.ttl)
log.warn(f"【System】进程 {os.getpid()} 检测到系统配置文件已修改,正在重新加载...")
time.sleep(1)
# 重新加载配置
Config().init_config()
# 重载singleton服务
for instance in INSTANCES.values():
if hasattr(instance, "init_config"):
instance.init_config()
# 正在使用的二级分类策略文件3秒内只能加载一次,配置文件加载时,二级分类策略文件不加载
elif file_name == os.path.basename(Config().category_path) \
and not CategoryLoadCache.get(src_path) \
and not CategoryLoadCache.get("ConfigLoadBlock"):
CategoryLoadCache.set(src_path, True)
log.warn(f"【System】进程 {os.getpid()} 检测到二级分类策略 {file_head} 配置文件已修改,正在重新加载...")
time.sleep(1)
# 重新加载二级分类策略
Category().init_config()
def start_config_monitor():
"""
启动服务
"""
global _observer
# 配置文件监听
_observer.schedule(ConfigMonitor(), path=Config().get_config_path(), recursive=False)
_observer.daemon = True
_observer.start()
def stop_config_monitor():
"""
停止服务
"""
global _observer
try:
if _observer:
_observer.stop()
_observer.join()
except Exception as err:
print(str(err))