diff --git a/pytchat/cli/__init__.py b/pytchat/cli/__init__.py index 98c25ec..0d28485 100644 --- a/pytchat/cli/__init__.py +++ b/pytchat/cli/__init__.py @@ -94,10 +94,13 @@ class Runner: path = util.checkpath(separated_path + video_id + '.html') try: info = VideoInfo(video_id) - except Exception as e: + except (PatternUnmatchError, JSONDecodeError) as e: print("Cannot parse video information.:{} {}".format(video_id, type(e))) if Arguments().save_error_data: - util.save(str(e), "ERR", ".dat") + util.save(str(e.doc), "ERR", ".dat") + continue + except Exception as e: + print("Cannot parse video information.:{} {}".format(video_id, type(e))) continue print(f"\n" diff --git a/pytchat/exceptions.py b/pytchat/exceptions.py index 1bd918c..4212fdc 100644 --- a/pytchat/exceptions.py +++ b/pytchat/exceptions.py @@ -76,6 +76,6 @@ class PatternUnmatchError(VideoInfoParseError): ''' Thrown when failed to parse video info with unmatched pattern. ''' - def __init__(self, doc): + def __init__(self, doc=''): self.msg = "PatternUnmatchError" self.doc = doc diff --git a/pytchat/tool/videoinfo.py b/pytchat/tool/videoinfo.py index 19baa78..1744de7 100644 --- a/pytchat/tool/videoinfo.py +++ b/pytchat/tool/videoinfo.py @@ -8,8 +8,8 @@ from ..util.extract_video_id import extract_video_id headers = config.headers - pattern = re.compile(r"['\"]PLAYER_CONFIG['\"]:\s*({.*})") +pattern2 = re.compile(r"yt\.setConfig\((\{[\s\S]*?\})\);") item_channel_id = [ "videoDetails", @@ -31,6 +31,10 @@ item_response = [ "embedded_player_response" ] +item_response2 = [ + "PLAYER_VARS", + "embedded_player_response" +] item_author_image = [ "videoDetails", "embeddedPlayerOverlayVideoDetailsRenderer", @@ -83,6 +87,7 @@ class VideoInfo: def __init__(self, video_id): self.video_id = extract_video_id(video_id) self.client = httpx.Client(http2=True) + self.new_pattern_text = False err = None for _ in range(3): try: @@ -90,7 +95,6 @@ class VideoInfo: self._parse(text) break except (InvalidVideoIdException, UnknownConnectionError) as e: - print(str(e)) raise e except Exception as e: err = e @@ -118,12 +122,25 @@ class VideoInfo: def _parse(self, text): result = re.search(pattern, text) if result is None: - raise PatternUnmatchError(doc=text) + result = re.search(pattern2, text) + if result is None: + raise PatternUnmatchError(doc=text) + else: + self.new_pattern_text = True decoder = json.JSONDecoder() - res = decoder.raw_decode(result.group(1)[:-1])[0] - response = self._get_item(res, item_response) + if self.new_pattern_text: + res = decoder.raw_decode(result.group(1))[0] + else: + res = decoder.raw_decode(result.group(1)[:-1])[0] + if self.new_pattern_text: + response = self._get_item(res, item_response2) + else: + response = self._get_item(res, item_response) if response is None: - self._check_video_is_private(res.get("args")) + if self.new_pattern_text: + self._check_video_is_private(res.get("PLAYER_VARS")) + else: + self._check_video_is_private(res.get("args")) self._renderer = self._get_item(json.loads(response), item_renderer) if self._renderer is None: raise InvalidVideoIdException(