diff --git a/pytchat/cli/__init__.py b/pytchat/cli/__init__.py index f8cffd3..16f8a76 100644 --- a/pytchat/cli/__init__.py +++ b/pytchat/cli/__init__.py @@ -10,6 +10,7 @@ from json.decoder import JSONDecodeError from pathlib import Path from httpcore import ReadTimeout as HCReadTimeout, NetworkError as HCNetworkError from .arguments import Arguments +from .echo import Echo from .progressbar import ProgressBar from .. exceptions import InvalidVideoIdException, NoContents, PatternUnmatchError, UnknownConnectionError from .. processors.html_archiver import HTMLArchiver @@ -41,11 +42,13 @@ def main(): help='Save error data when error occurs(".dat" file)') parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true', help='Show version') + parser.add_argument(f'--{Arguments.Name.ECHO}', action='store_true', + help='Show chats of specified video') Arguments(parser.parse_args().__dict__) if Arguments().print_version: - print(f'pytchat v{__version__} © 2019 taizan-hokuto') + print(f'pytchat v{__version__} © 2019,2020 taizan-hokuto') return # Extractor @@ -53,6 +56,20 @@ def main(): parser.print_help() return + # Echo + if Arguments().echo: + if len(Arguments().video_ids) > 1: + print("You can specify only one video ID.") + return + try: + SimpleEcho(Arguments().video_ids[0]).run() + except InvalidVideoIdException as e: + print("Invalid video id:", str(e)) + except Exception as e: + print(type(e), str(e)) + finally: + return + if not os.path.exists(Arguments().output): print("\nThe specified directory does not exist.:{}\n".format(Arguments().output)) return diff --git a/pytchat/cli/arguments.py b/pytchat/cli/arguments.py index 0e1baf4..be720c8 100644 --- a/pytchat/cli/arguments.py +++ b/pytchat/cli/arguments.py @@ -19,6 +19,7 @@ class Arguments(metaclass=Singleton): OUTPUT: str = 'output_dir' VIDEO_IDS: str = 'video_id' SAVE_ERROR_DATA: bool = 'save_error_data' + ECHO: bool = 'echo' def __init__(self, arguments: Optional[Dict[str, Union[str, bool, int]]] = None): @@ -36,8 +37,9 @@ class Arguments(metaclass=Singleton): self.output: str = arguments[Arguments.Name.OUTPUT] self.video_ids: List[int] = [] self.save_error_data: bool = arguments[Arguments.Name.SAVE_ERROR_DATA] - + self.echo: bool = arguments[Arguments.Name.ECHO] # Videos + if arguments[Arguments.Name.VIDEO_IDS]: self.video_ids = [video_id for video_id in arguments[Arguments.Name.VIDEO_IDS].split(',')] diff --git a/pytchat/cli/echo.py b/pytchat/cli/echo.py new file mode 100644 index 0000000..506a40f --- /dev/null +++ b/pytchat/cli/echo.py @@ -0,0 +1,17 @@ +import pytchat +from ..util.extract_video_id import extract_video_id + + +class Echo: + def __init__(self, video_id): + self.video_id = extract_video_id(video_id) + + def run(self): + livechat = pytchat.create(self.video_id) + while livechat.is_alive(): + chatdata = livechat.get() + for c in chatdata.sync_items(): + print(f"{c.datetime} [{c.author.name}] {c.message} {c.amountString}") + + +