|
| 1 | +import sys |
| 2 | + |
| 3 | +import rich |
| 4 | +import typer |
| 5 | +from yt_dlp.cookies import SUPPORTED_BROWSERS |
| 6 | + |
| 7 | +from ygka.adapters.openai_cookie_adapter import OpenAICookieAdapter |
| 8 | +from ygka.models import RevChatGPTChatbotConfigModel |
| 9 | +from ygka.models.ygka_config_model import YGKAConfigModel |
| 10 | +from ygka.utils import YGKAConfigManager |
| 11 | + |
| 12 | + |
| 13 | +def config_ygka(): |
| 14 | + rich.print(''' |
| 15 | +Hi! 🙌 I am [bold blue]YGKA[/bold blue]! |
| 16 | +[yellow][blue bold underline]Y[/blue bold underline]our |
| 17 | +[blue bold underline]G[/blue bold underline]enius, |
| 18 | +[blue bold underline]K[/blue bold underline]nowledgeable assistant |
| 19 | +[blue bold underline]A[/blue bold underline]n advanced ChatGPT client[/yellow] 🔥 |
| 20 | +I am here to assist you with configuring YGKA. 💪 |
| 21 | +
|
| 22 | +Please make sure that you have logged into chat.openai.com on your browser before we continue. 🗝️ |
| 23 | +
|
| 24 | +'''[1:]) |
| 25 | + typer.confirm('Are you ready to proceed? 🚀', abort=True) |
| 26 | + |
| 27 | + rich.print(f''' |
| 28 | +Which browser did you use to log in to chat.openai.com? |
| 29 | +
|
| 30 | +We support the following browsers: [{SUPPORTED_BROWSERS}]'''[1:]) |
| 31 | + browser_name = typer.prompt('Please enter your choice here: ') |
| 32 | + if browser_name not in SUPPORTED_BROWSERS: |
| 33 | + rich.print(f'Browser {browser_name} is not supported. Supported browsers are: {SUPPORTED_BROWSERS}') |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + adapter = OpenAICookieAdapter(browser_name) |
| 37 | + session_token = adapter.get_openai_session_token() |
| 38 | + if not session_token: |
| 39 | + rich.print('Failed to get session token. 😓 Can you check if you are logged in to https://chat.openai.com?') |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + config_manager = save_config(session_token) |
| 43 | + |
| 44 | + rich.print(f''' |
| 45 | +[green bold]Excellent![/green bold] You are now ready to use [bold blue]YGKA[/bold blue] 🚀 |
| 46 | +
|
| 47 | +Enjoy your AI powered terminal assistant! 🎉 |
| 48 | +
|
| 49 | +[dim]To check your settings file, it's at: {config_manager.config_path}[/dim] |
| 50 | +
|
| 51 | +'''[1:]) |
| 52 | + return config_manager |
| 53 | + |
| 54 | + |
| 55 | +def save_config(session_token: str): |
| 56 | + is_config_file_available = YGKAConfigManager.is_config_file_available(YGKAConfigManager.DEFAULT_CONFIG_PATH) |
| 57 | + if is_config_file_available: |
| 58 | + config_manager = YGKAConfigManager(load_config=True) |
| 59 | + is_chatgpt_config_available = config_manager.config_model.chatgpt_config is not None |
| 60 | + if is_chatgpt_config_available: |
| 61 | + assert config_manager.config_model.chatgpt_config # for type hinting |
| 62 | + config_manager.config_model.chatgpt_config.session_token = session_token |
| 63 | + else: |
| 64 | + config_manager.config_model.chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token) |
| 65 | + else: |
| 66 | + chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token) |
| 67 | + YGKA_config = YGKAConfigModel(chatgpt_config=chatgpt_config) |
| 68 | + config_manager = YGKAConfigManager(config_model=YGKA_config) |
| 69 | + |
| 70 | + config_manager.save_config() |
| 71 | + return config_manager |
0 commit comments