simplified some logic and improved error handling

This commit is contained in:
Alex Shnitman 2025-07-27 10:03:51 +03:00
parent 7f28f17d77
commit 8f36ca910a
1 changed files with 39 additions and 27 deletions

View File

@ -74,21 +74,22 @@ class Config:
if not self.URL_PREFIX.endswith('/'): if not self.URL_PREFIX.endswith('/'):
self.URL_PREFIX += '/' self.URL_PREFIX += '/'
success,_ = self.load_ytdl_options(True) success,_ = self.load_ytdl_options()
if not success: if not success:
sys.exit(1) sys.exit(1)
def load_ytdl_options(self, is_init=False) -> tuple[bool, str]: def load_ytdl_options(self) -> tuple[bool, str]:
msg = '' try:
if not self.load_ytdl_options_env(is_init): self.YTDL_OPTIONS = json.loads(os.environ.get('YTDL_OPTIONS', '{}'))
assert isinstance(self.YTDL_OPTIONS, dict)
except (json.decoder.JSONDecodeError, AssertionError):
msg = 'Environment variable YTDL_OPTIONS is invalid' msg = 'Environment variable YTDL_OPTIONS is invalid'
log.error(msg)
return (False, msg) return (False, msg)
if not self.YTDL_OPTIONS_FILE: if not self.YTDL_OPTIONS_FILE:
msg='YTDL_OPTIONS_FILE is not set' return (True, '')
if not is_init:
log.error(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'
@ -104,21 +105,7 @@ class Config:
return (False, msg) return (False, msg)
self.YTDL_OPTIONS.update(opts) self.YTDL_OPTIONS.update(opts)
return (True, msg) return (True, '')
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()
@ -161,15 +148,40 @@ app.on_startup.append(lambda app: dqueue.initialize())
class FileOpsFilter(DefaultFilter): class FileOpsFilter(DefaultFilter):
def __call__(self, change_type: int, path: str) -> bool: def __call__(self, change_type: int, path: str) -> bool:
return (os.path.samefile(path, config.YTDL_OPTIONS_FILE) and # Check if this path matches our YTDL_OPTIONS_FILE
change_type in (Change.modified,Change.added)) if path != config.YTDL_OPTIONS_FILE:
return False
# For existing files, use samefile comparison to handle symlinks correctly
if os.path.exists(config.YTDL_OPTIONS_FILE):
try:
if not os.path.samefile(path, config.YTDL_OPTIONS_FILE):
return False
except (OSError, IOError):
# If samefile fails, fall back to string comparison
if path != config.YTDL_OPTIONS_FILE:
return False
# Accept all change types for our file: modified, added, deleted
return change_type in (Change.modified, Change.added, Change.deleted)
def get_options_update_time(success=True, msg=''): def get_options_update_time(success=True, msg=''):
result = { result = {
'success': success, 'success': success,
'msg': msg, 'msg': msg,
'update_time': os.path.getmtime(config.YTDL_OPTIONS_FILE) 'update_time': None
} }
# Only try to get file modification time if YTDL_OPTIONS_FILE is set and file exists
if config.YTDL_OPTIONS_FILE and os.path.exists(config.YTDL_OPTIONS_FILE):
try:
result['update_time'] = os.path.getmtime(config.YTDL_OPTIONS_FILE)
except (OSError, IOError) as e:
log.warning(f"Could not get modification time for {config.YTDL_OPTIONS_FILE}: {e}")
result['update_time'] = None
return result return result
async def watch_files(): 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():