change the reload options file logic
This commit is contained in:
parent
91aaa9f425
commit
7f28f17d77
35
app/main.py
35
app/main.py
|
|
@ -74,24 +74,21 @@ class Config:
|
||||||
if not self.URL_PREFIX.endswith('/'):
|
if not self.URL_PREFIX.endswith('/'):
|
||||||
self.URL_PREFIX += '/'
|
self.URL_PREFIX += '/'
|
||||||
|
|
||||||
try:
|
success,_ = self.load_ytdl_options(True)
|
||||||
self.YTDL_OPTIONS = json.loads(self.YTDL_OPTIONS)
|
|
||||||
assert isinstance(self.YTDL_OPTIONS, dict)
|
|
||||||
except (json.decoder.JSONDecodeError, AssertionError):
|
|
||||||
log.error('YTDL_OPTIONS is invalid')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
success,_ = self.load_ytdl_options_file(True)
|
|
||||||
if not success:
|
if not success:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def load_ytdl_options_file(self, allow_empty_path=False) -> tuple[bool, str]:
|
def load_ytdl_options(self, is_init=False) -> tuple[bool, str]:
|
||||||
msg = ''
|
msg = ''
|
||||||
|
if not self.load_ytdl_options_env(is_init):
|
||||||
|
msg = 'Environment variable YTDL_OPTIONS is invalid'
|
||||||
|
return (False, msg)
|
||||||
|
|
||||||
if not self.YTDL_OPTIONS_FILE:
|
if not self.YTDL_OPTIONS_FILE:
|
||||||
msg='YTDL_OPTIONS_FILE is not set'
|
msg='YTDL_OPTIONS_FILE is not set'
|
||||||
if not allow_empty_path:
|
if not is_init:
|
||||||
log.error(msg)
|
log.error(msg)
|
||||||
return (allow_empty_path, msg)
|
return (is_init, msg)
|
||||||
log.info(f'Loading yt-dlp custom options from "{self.YTDL_OPTIONS_FILE}"')
|
log.info(f'Loading yt-dlp custom options from "{self.YTDL_OPTIONS_FILE}"')
|
||||||
if not os.path.exists(self.YTDL_OPTIONS_FILE):
|
if not os.path.exists(self.YTDL_OPTIONS_FILE):
|
||||||
msg = f'File "{self.YTDL_OPTIONS_FILE}" not found'
|
msg = f'File "{self.YTDL_OPTIONS_FILE}" not found'
|
||||||
|
|
@ -108,6 +105,20 @@ class Config:
|
||||||
|
|
||||||
self.YTDL_OPTIONS.update(opts)
|
self.YTDL_OPTIONS.update(opts)
|
||||||
return (True, msg)
|
return (True, msg)
|
||||||
|
|
||||||
|
def load_ytdl_options_env(self, is_init=False) -> tuple[bool, str]:
|
||||||
|
k = 'YTDL_OPTIONS'
|
||||||
|
if not is_init:
|
||||||
|
|
||||||
|
setattr(self, k, os.environ.get(k, '{}'))
|
||||||
|
print(os.environ.get(k, '{}'))
|
||||||
|
try:
|
||||||
|
self.YTDL_OPTIONS = json.loads(self.YTDL_OPTIONS)
|
||||||
|
assert isinstance(self.YTDL_OPTIONS, dict)
|
||||||
|
except (json.decoder.JSONDecodeError, AssertionError):
|
||||||
|
log.error('YTDL_OPTIONS is invalid')
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
config = Config()
|
config = Config()
|
||||||
|
|
||||||
|
|
@ -163,7 +174,7 @@ async def watch_files():
|
||||||
path_to_watch = Path(config.YTDL_OPTIONS_FILE).resolve()
|
path_to_watch = Path(config.YTDL_OPTIONS_FILE).resolve()
|
||||||
async def _watch_files():
|
async def _watch_files():
|
||||||
async for changes in awatch(path_to_watch, watch_filter=FileOpsFilter()):
|
async for changes in awatch(path_to_watch, watch_filter=FileOpsFilter()):
|
||||||
success, msg = config.load_ytdl_options_file()
|
success, msg = config.load_ytdl_options()
|
||||||
result = get_options_update_time(success, msg)
|
result = get_options_update_time(success, msg)
|
||||||
await sio.emit('ytdl_options_changed', serializer.encode(result))
|
await sio.emit('ytdl_options_changed', serializer.encode(result))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue