Add --echo option to cli

This commit is contained in:
taizan-hokouto
2020-10-29 01:40:43 +09:00
parent 15a1d5c210
commit 8f5c3f312a
3 changed files with 38 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ from json.decoder import JSONDecodeError
from pathlib import Path from pathlib import Path
from httpcore import ReadTimeout as HCReadTimeout, NetworkError as HCNetworkError from httpcore import ReadTimeout as HCReadTimeout, NetworkError as HCNetworkError
from .arguments import Arguments from .arguments import Arguments
from .echo import Echo
from .progressbar import ProgressBar from .progressbar import ProgressBar
from .. exceptions import InvalidVideoIdException, NoContents, PatternUnmatchError, UnknownConnectionError from .. exceptions import InvalidVideoIdException, NoContents, PatternUnmatchError, UnknownConnectionError
from .. processors.html_archiver import HTMLArchiver from .. processors.html_archiver import HTMLArchiver
@@ -41,11 +42,13 @@ def main():
help='Save error data when error occurs(".dat" file)') help='Save error data when error occurs(".dat" file)')
parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true', parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true',
help='Show version') help='Show version')
parser.add_argument(f'--{Arguments.Name.ECHO}', action='store_true',
help='Show chats of specified video')
Arguments(parser.parse_args().__dict__) Arguments(parser.parse_args().__dict__)
if Arguments().print_version: if Arguments().print_version:
print(f'pytchat v{__version__} © 2019 taizan-hokuto') print(f'pytchat v{__version__} © 2019,2020 taizan-hokuto')
return return
# Extractor # Extractor
@@ -53,6 +56,20 @@ def main():
parser.print_help() parser.print_help()
return 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): if not os.path.exists(Arguments().output):
print("\nThe specified directory does not exist.:{}\n".format(Arguments().output)) print("\nThe specified directory does not exist.:{}\n".format(Arguments().output))
return return

View File

@@ -19,6 +19,7 @@ class Arguments(metaclass=Singleton):
OUTPUT: str = 'output_dir' OUTPUT: str = 'output_dir'
VIDEO_IDS: str = 'video_id' VIDEO_IDS: str = 'video_id'
SAVE_ERROR_DATA: bool = 'save_error_data' SAVE_ERROR_DATA: bool = 'save_error_data'
ECHO: bool = 'echo'
def __init__(self, def __init__(self,
arguments: Optional[Dict[str, Union[str, bool, int]]] = None): 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.output: str = arguments[Arguments.Name.OUTPUT]
self.video_ids: List[int] = [] self.video_ids: List[int] = []
self.save_error_data: bool = arguments[Arguments.Name.SAVE_ERROR_DATA] self.save_error_data: bool = arguments[Arguments.Name.SAVE_ERROR_DATA]
self.echo: bool = arguments[Arguments.Name.ECHO]
# Videos # Videos
if arguments[Arguments.Name.VIDEO_IDS]: if arguments[Arguments.Name.VIDEO_IDS]:
self.video_ids = [video_id self.video_ids = [video_id
for video_id in arguments[Arguments.Name.VIDEO_IDS].split(',')] for video_id in arguments[Arguments.Name.VIDEO_IDS].split(',')]

17
pytchat/cli/echo.py Normal file
View File

@@ -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}")