From 235d6b7212ed07f2483b4467d727e576f49c07c4 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Fri, 4 Sep 2020 01:46:10 +0900 Subject: [PATCH 1/3] Fix extract video info --- pytchat/tool/videoinfo.py | 3 ++- tests/test_videoinfo.py | 15 +++++++++++++-- tests/testdata/videoinfo/extradata_page.txt | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/testdata/videoinfo/extradata_page.txt diff --git a/pytchat/tool/videoinfo.py b/pytchat/tool/videoinfo.py index e2de5f5..a6fb117 100644 --- a/pytchat/tool/videoinfo.py +++ b/pytchat/tool/videoinfo.py @@ -93,7 +93,8 @@ class VideoInfo: result = re.search(pattern, text) if result is None: raise PatternUnmatchError(text) - res = json.loads(result.group(1)[:-1]) + decoder = json.JSONDecoder() + res = decoder.raw_decode(result.group(1)[:-1])[0] response = self._get_item(res, item_response) if response is None: self._check_video_is_private(res.get("args")) diff --git a/tests/test_videoinfo.py b/tests/test_videoinfo.py index af1ba84..c2a8803 100644 --- a/tests/test_videoinfo.py +++ b/tests/test_videoinfo.py @@ -1,7 +1,6 @@ from json.decoder import JSONDecodeError from pytchat.tool.videoinfo import VideoInfo from pytchat.exceptions import InvalidVideoIdException, PatternUnmatchError -from pytchat import util def _open_file(path): @@ -32,7 +31,7 @@ def test_archived_page(mocker): def test_live_page(mocker): _set_test_data('tests/testdata/videoinfo/live_page.txt', mocker) info = VideoInfo('__test_id__') - '''live page :duration = 0''' + '''live page: duration==0''' assert info.get_duration() == 0 assert info.video_id == '__test_id__' assert info.get_channel_name() == 'BGM channel' @@ -88,3 +87,15 @@ def test_pattern_unmatch(mocker): assert False except PatternUnmatchError: assert True + + +def test_extradata_handling(mocker): + '''Test case the extracted data are JSON lines.''' + _set_test_data( + 'tests/testdata/videoinfo/extradata_page.txt', mocker) + try: + _ = VideoInfo('__test_id__') + assert True + except JSONDecodeError as e: + print(e.doc) + assert False diff --git a/tests/testdata/videoinfo/extradata_page.txt b/tests/testdata/videoinfo/extradata_page.txt new file mode 100644 index 0000000..a8f41ff --- /dev/null +++ b/tests/testdata/videoinfo/extradata_page.txt @@ -0,0 +1,15 @@ + + + + + GitHub Arctic Code Vault - YouTube + + + + + + +
+ \ No newline at end of file From 3bdc465740df0b14ab73b9927f1ddbb606643861 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Fri, 4 Sep 2020 01:52:53 +0900 Subject: [PATCH 2/3] Devide exception handling --- pytchat/cli/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pytchat/cli/__init__.py b/pytchat/cli/__init__.py index a7f045f..94601a3 100644 --- a/pytchat/cli/__init__.py +++ b/pytchat/cli/__init__.py @@ -76,9 +76,10 @@ def main(): print("\nThe extraction process has been completed.\n") except InvalidVideoIdException: print("Invalid Video ID or URL:", video_id) - except (TypeError, NoContents) as e: - + except TypeError as e: print(e.with_traceback()) + except NoContents as e: + print(e) except FileNotFoundError: print("The specified directory does not exist.:{}".format(Arguments().output)) except JSONDecodeError as e: From bc8655ed623e427cce72ac36cd0e2e76c1083dc1 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Fri, 4 Sep 2020 01:53:14 +0900 Subject: [PATCH 3/3] Increment version --- pytchat/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytchat/__init__.py b/pytchat/__init__.py index 6651659..61d1189 100644 --- a/pytchat/__init__.py +++ b/pytchat/__init__.py @@ -2,7 +2,7 @@ pytchat is a lightweight python library to browse youtube livechat without Selenium or BeautifulSoup. """ __copyright__ = 'Copyright (C) 2019 taizan-hokuto' -__version__ = '0.1.6' +__version__ = '0.1.7' __license__ = 'MIT' __author__ = 'taizan-hokuto' __author_email__ = '55448286+taizan-hokuto@users.noreply.github.com'