From 2cb455a36ff1436afdb1ebaa2c9e3af60c4dda08 Mon Sep 17 00:00:00 2001 From: mirasac <50930731+mirasac@users.noreply.github.com> Date: Tue, 21 Jun 2022 00:59:52 +0200 Subject: [PATCH] feat: add options to update single components These options are cumulative. Specifying no option defaults to the update of the entire web site. Tests are needed. --- pauperformance_bot/cli/group/academy.py | 67 ++++++++++++++++++++++++- pauperformance_bot/task/academy.py | 43 ++++++++++++++-- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/pauperformance_bot/cli/group/academy.py b/pauperformance_bot/cli/group/academy.py index 56f6bd23..8426f6db 100644 --- a/pauperformance_bot/cli/group/academy.py +++ b/pauperformance_bot/cli/group/academy.py @@ -1,29 +1,92 @@ #!/usr/bin/env python3 from pauperformance_bot.cli.builder.command import CLICommand from pauperformance_bot.cli.builder.group import CLIGroup +from pauperformance_bot.cli.builder.options import FlagCLIOption from pauperformance_bot.constant.cli import ( ACADEMY_CLI_GROUP, UPDATE2_ACADEMY_CMD, UPDATE_ACADEMY_CMD, ) from pauperformance_bot.task.academy import main as main_v1 +from pauperformance_bot.task.academy import ( + run_coroutine_in_event_loop, + async_import_players_videos_from_twitch, + async_import_players_videos_from_youtube, + async_import_decks_from_deckstats, + async_import_decks_from_discord +) from pauperformance_bot.task.academy2 import main as main_v2 from pauperformance_bot.util.log import get_application_logger logger = get_application_logger(ACADEMY_CLI_GROUP) +class TwitchCLIOption(FlagCLIOption): + def __init__(self): + super().__init__("twitch", "Import players' videos from Twitch.") + + +class YouTubeCLIOption(FlagCLIOption): + def __init__(self): + super().__init__("youtube", "Import players' videos from YouTube.") + + +class DeckstatsCLIOption(FlagCLIOption): + def __init__(self): + super().__init__("deckstats", "Import decks from DeckStats.") + + +class DiscordCLIOption(FlagCLIOption): + def __init__(self): + super().__init__("discord", "Import decks from Discord.") + + class UpdateCommand(CLICommand): def __init__(self): super().__init__( UPDATE_ACADEMY_CMD, "Update Academy web site (old version).", - [], + [ + TwitchCLIOption(), + YouTubeCLIOption(), + DeckstatsCLIOption(), + DiscordCLIOption() + ], ) def dispatch_cmd(self, *args, **kwargs): super().dispatch_cmd(*args, **kwargs) - main_v1() + default = True + if kwargs.pop(TwitchCLIOption().dest_var, False): + default = False + run_coroutine_in_event_loop( + async_import_players_videos_from_twitch, + send_notification=True, + update_dev=False + ) + if kwargs.pop(YouTubeCLIOption().dest_var, False): + default = False + run_coroutine_in_event_loop( + async_import_players_videos_from_youtube, + send_notification=True, + update_dev=False + ) + if kwargs.pop(DeckstatsCLIOption().dest_var, False): + default = False + run_coroutine_in_event_loop( + async_import_decks_from_deckstats, + send_notification=True, + update_dev=False + ) + if kwargs.pop(DiscordCLIOption().dest_var, False): + default = False + run_coroutine_in_event_loop( + async_import_decks_from_discord, + send_notification=True, + update_dev=False + ) + if default: + main_v1() class UpdateCommand2(CLICommand): diff --git a/pauperformance_bot/task/academy.py b/pauperformance_bot/task/academy.py index d4a627a2..9ef08ef2 100644 --- a/pauperformance_bot/task/academy.py +++ b/pauperformance_bot/task/academy.py @@ -11,12 +11,45 @@ from pauperformance_bot.service.pauperformance.storage.dropbox_ import DropboxService -async def async_academy_update(): +async def async_setup_pauperformance_service(): storage = DropboxService() archive = MTGGoldfishArchiveService(storage) discord = AsyncDiscordService() pauperformance = AsyncPauperformanceService(discord, storage, archive) await pauperformance.discord.wait_until_ready() + return pauperformance + + +async def async_import_players_videos_from_twitch(send_notification=True, update_dev=True): + pauperformance = await async_setup_pauperformance_service() + await pauperformance.import_players_videos_from_twitch(send_notification) + academy = AcademyService(pauperformance) + academy.update_all(update_dev) + + +async def async_import_players_videos_from_youtube(send_notification=True, update_dev=True): + pauperformance = await async_setup_pauperformance_service() + await pauperformance.import_players_videos_from_youtube(send_notification) + academy = AcademyService(pauperformance) + academy.update_all(update_dev) + + +async def async_import_decks_from_deckstats(send_notification=True, update_dev=True): + pauperformance = await async_setup_pauperformance_service() + await pauperformance.import_decks_from_deckstats(send_notification) + academy = AcademyService(pauperformance) + academy.update_all(update_dev) + + +async def async_import_decks_from_discord(send_notification=True, update_dev=True): + pauperformance = await async_setup_pauperformance_service() + await pauperformance.import_decks_from_discord(send_notification) + academy = AcademyService(pauperformance) + academy.update_all(update_dev) + + +async def async_academy_update(): + pauperformance = await async_setup_pauperformance_service() # import new content await pauperformance.import_players_videos_from_twitch(send_notification=True) @@ -29,13 +62,17 @@ async def async_academy_update(): academy.update_all(update_dev=False) # avoid unnecessary changes -def main(): +def run_coroutine_in_event_loop(coroutine, **kwargs): loop = asyncio.get_event_loop() try: - loop.run_until_complete(async_academy_update()) + loop.run_until_complete(coroutine(**kwargs)) finally: loop.close() +def main(): + run_coroutine_in_event_loop(async_academy_update) + + if __name__ == "__main__": main()