jellyfin/src/config.py

45 lines
1.1 KiB
Python
Raw Normal View History

2023-04-05 16:31:14 +00:00
"""handle config file"""
import json
import os
2023-07-26 04:19:10 +00:00
from typing import Literal
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
from src.static_types import ConfigType
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def get_config() -> ConfigType:
2023-04-05 16:31:14 +00:00
"""get connection config"""
2023-07-26 04:19:10 +00:00
config_content: ConfigType | Literal[False] = (
get_config_file() or get_config_env()
)
if not config_content:
raise ValueError("No config.json or environment variable found")
return config_content
def get_config_file() -> ConfigType | Literal[False]:
"""read config file if available"""
if os.path.exists("config.json"):
with open("config.json", "r", encoding="utf-8") as f:
config_content: ConfigType = json.loads(f.read())
2023-07-26 04:19:10 +00:00
return config_content
2023-07-26 04:19:10 +00:00
return False
def get_config_env() -> ConfigType | Literal[False]:
"""read config from environment"""
if "TA_URL" in os.environ:
config_content: ConfigType = {
"ta_video_path": "/youtube",
"ta_url": os.environ["TA_URL"],
"ta_token": os.environ["TA_TOKEN"],
"jf_url": os.environ["JF_URL"],
"jf_token": os.environ["JF_TOKEN"],
}
return config_content
2023-07-26 04:19:10 +00:00
return False