diff --git a/pytchat/cli/__init__.py b/pytchat/cli/__init__.py index edcb818..213f8e7 100644 --- a/pytchat/cli/__init__.py +++ b/pytchat/cli/__init__.py @@ -2,6 +2,7 @@ import argparse import os import signal +import time from json.decoder import JSONDecodeError from pathlib import Path from .arguments import Arguments @@ -55,7 +56,21 @@ def main(): path = Path(Arguments().output + video_id + '.html') else: raise FileNotFoundError - info = VideoInfo(video_id) + err = None + for _ in range(3): # retry 3 times + try: + info = VideoInfo(video_id) + break + except (PatternUnmatchError, JSONDecodeError, InvalidVideoIdException) as e: + err = e + time.sleep(2) + continue + else: + print("Cannot parse video information.:{}".format(video_id)) + if Arguments().save_error_data: + util.save(err.doc, "ERR", ".dat") + continue + if len(Arguments().video_ids) > 1: print(f"\n{'-' * 10} video:{counter + 1} of {len(Arguments().video_ids)} {'-' * 10}") print(f"\n" @@ -86,8 +101,6 @@ def main(): print() if pbar.is_cancelled(): print("\nThe extraction process has been discontinued.\n") - - except InvalidVideoIdException: print("Invalid Video ID or URL:", video_id) except NoContents as e: @@ -96,14 +109,9 @@ def main(): print("The specified directory does not exist.:{}".format(Arguments().output)) except JSONDecodeError as e: print(e.msg) - print("Cannot parse video information.:{}".format(video_id)) + print("JSONDecodeError.:{}".format(video_id)) if Arguments().save_error_data: util.save(e.doc, "ERR_JSON_DECODE", ".dat") - except PatternUnmatchError as e: - print(e.msg) - print("Cannot parse video information.:{}".format(video_id)) - if Arguments().save_error_data: - util.save(e.doc, "ERR_PATTERN_UNMATCH", ".dat") return diff --git a/pytchat/exceptions.py b/pytchat/exceptions.py index af6d4af..9e498d1 100644 --- a/pytchat/exceptions.py +++ b/pytchat/exceptions.py @@ -38,7 +38,10 @@ class InvalidVideoIdException(Exception): ''' Thrown when the video_id is not exist (VideoInfo). ''' - pass + def __init__(self, doc): + self.msg = "InvalidVideoIdException" + self.doc = doc + class UnknownConnectionError(Exception): @@ -47,7 +50,7 @@ class UnknownConnectionError(Exception): class RetryExceedMaxCount(Exception): ''' - thrown when the number of retries exceeds the maximum value. + Thrown when the number of retries exceeds the maximum value. ''' pass @@ -66,13 +69,13 @@ class FailedExtractContinuation(ChatDataFinished): class VideoInfoParseError(Exception): ''' - thrown when failed to parse video info + Base exception when parsing video info. ''' class PatternUnmatchError(VideoInfoParseError): ''' - thrown when failed to parse video info with unmatched pattern + Thrown when failed to parse video info with unmatched pattern. ''' def __init__(self, doc): self.msg = "PatternUnmatchError"