diff --git a/autostream/cli.py b/autostream/cli.py index 3cbe49f..cf1850f 100644 --- a/autostream/cli.py +++ b/autostream/cli.py @@ -5,6 +5,7 @@ import os import logging from autostream.controller.bili_controller import BiliController +from autostream.controller.config_controller import ConfigController def cli(): @@ -13,11 +14,20 @@ def cli(): subparsers = parser.add_subparsers(dest='subcommand', help='Subcommands') + # Check config + check_config_parser = subparsers.add_parser('check', help='Check the configuration') + + # Add config + add_config_parser = subparsers.add_parser('add', help='Add the configuration') + add_config_parser.add_argument('-s', '--bili_server_url', help='The bilibili server url') + add_config_parser.add_argument('-k', '--bili_key', help='The bilibili stream key') + add_config_parser.add_argument('-f', '--folder', help='The input folder') + + # Reset config + reset_config_parser = subparsers.add_parser('reset', help='Reset the configuration') + # Bilibili stream - bilibili_parser = subparsers.add_parser('bili', help='Stream on Bilibili') - bilibili_parser.add_argument('--server_url', required=True, help='(required) The server url') - bilibili_parser.add_argument('--key', required=True, help='(required) The stream key') - bilibili_parser.add_argument('--folder', required=True, help='(required) The input folder') + bilibili_parser = subparsers.add_parser('bili', help='Stream on the bilibili platform') args = parser.parse_args() @@ -27,9 +37,26 @@ def cli(): parser.print_help() sys.exit() + if args.subcommand == 'check': + ConfigController().check_config() + print(ConfigController().get_config()) + + if args.subcommand == 'add': + if args.bili_server_url: + ConfigController().update_specific_config('bili_server_url', args.bili_server_url) + if args.bili_key: + ConfigController().update_specific_config('bili_key', args.bili_key) + if args.folder: + ConfigController().update_specific_config('folder', args.folder) + + if args.subcommand == 'reset': + ConfigController().reset_config() + if args.subcommand == 'bili': - print(args) - BiliController(args.server_url, args.key, args.folder).stream() + if ConfigController().check_config(): + BiliController().stream() + else: + print("Please complete the configuration first!", flush=True) if __name__ == '__main__': cli() \ No newline at end of file diff --git a/autostream/controller/bili_controller.py b/autostream/controller/bili_controller.py index 5a241dd..89b92e0 100644 --- a/autostream/controller/bili_controller.py +++ b/autostream/controller/bili_controller.py @@ -2,17 +2,19 @@ import subprocess from autostream.execute.scan_and_execute import scan_folder_and_execute +from autostream.model.model import Model class BiliController: - def __init__(self, server_url, key, folder): - self.server_url = server_url - self.key = key - self.folder = folder - + def __init__(self): + self.model = Model() + self.server_url = self.model.get_config()['bili_server_url'] + self.key = self.model.get_config()['bili_key'] + self.folder = self.model.get_config()['folder'] + @property def stream_url(self): return f'{self.server_url}{self.key}' def stream(self): while True: - scan_folder_and_execute(self.folder, self.stream_url) + scan_folder_and_execute(self.folder, self.stream_url) \ No newline at end of file diff --git a/autostream/controller/config_controller.py b/autostream/controller/config_controller.py new file mode 100644 index 0000000..1269a5f --- /dev/null +++ b/autostream/controller/config_controller.py @@ -0,0 +1,29 @@ +# Copyright (c) 2025 autostream + +from autostream.model.model import Model + +class ConfigController: + def __init__(self): + self.model = Model() + + def check_config(self): + config_info = self.model.get_config() + if not config_info['bili_server_url'] or not config_info['bili_key'] or not config_info['folder']: + print("The configuration is not complete!", flush=True) + return False + return True + + def update_config(self, config_info): + self.model.update_multiple_config(config_info) + + def reset_config(self): + self.model.reset_config() + + def get_config(self): + return self.model.get_config() + + def get_specific_config(self, key): + return self.model.get_specific_config(key) + + def update_specific_config(self, key, value): + self.model.update_specific_config(key, value) diff --git a/autostream/model/__init__.py b/autostream/model/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/autostream/model/config.json b/autostream/model/config.json new file mode 100644 index 0000000..9ef9dd1 --- /dev/null +++ b/autostream/model/config.json @@ -0,0 +1,5 @@ +{ + "folder": "", + "bili_server_url": "", + "bili_key": "" +} \ No newline at end of file diff --git a/autostream/model/model.py b/autostream/model/model.py new file mode 100644 index 0000000..88a9325 --- /dev/null +++ b/autostream/model/model.py @@ -0,0 +1,46 @@ +# Copyright (c) 2025 autostream + +import json +import os + +class Model: + def __init__(self, path=None) -> None: + if path is None: + self.path = os.path.join(os.path.dirname(__file__), "config.json") + else: + self.path = path + self.default_config = { + "folder": "", + "bili_server_url": "", + "bili_key": "" + } + + def get_default_config(self): + return self.default_config + + def reset_config(self): + self.write(self.default_config) + + def update_specific_config(self, key, value): + config_info = self.get_config() + config_info[key] = value + self.write(config_info) + + def update_multiple_config(self, updates: dict): + config_info = self.get_config() + for key, value in updates.items(): + config_info[key] = value + self.write(config_info) + + def get_config(self): + if not os.path.exists(self.path): + self.reset_config() + return self.read() + + def read(self): + with open(self.path, "r") as f: + return json.load(f) + + def write(self, config): + with open(self.path, "w") as f: + json.dump(config, f, indent=4)