diff --git a/pytchat/__init__.py b/pytchat/__init__.py index 2595d0c..d3550b4 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, 2020 taizan-hokuto' -__version__ = '0.4.5' +__version__ = '0.4.6' __license__ = 'MIT' __author__ = 'taizan-hokuto' __author_email__ = '55448286+taizan-hokuto@users.noreply.github.com' diff --git a/pytchat/processors/default/processor.py b/pytchat/processors/default/processor.py index d8bbac5..e28da0e 100644 --- a/pytchat/processors/default/processor.py +++ b/pytchat/processors/default/processor.py @@ -112,7 +112,7 @@ class Chatdata: await asyncio.sleep(1 - stop_interval) def json(self) -> str: - return json.dumps([vars(a) for a in self.items], ensure_ascii=False, cls=CustomEncoder) + return ''.join(("[", ','.join((a.json() for a in self.items)), "]")) class DefaultProcessor(ChatProcessor): diff --git a/tests/test_default_processor.py b/tests/test_default_processor.py index c0fc5b9..819a6da 100644 --- a/tests/test_default_processor.py +++ b/tests/test_default_processor.py @@ -222,6 +222,60 @@ def test_sponsor_legacy(mocker): assert ret.author.isChatModerator is False +def test_jsonify_list(mocker): + '''lagacy sponsor(membership)''' + processor = DefaultProcessor() + parser = Parser(is_replay=False) + _json = _open_file("tests/testdata/default/superchat.json") + + _, chatdata = parser.parse(parser.get_contents(json.loads(_json))) + data = { + "video_id": "", + "timeout": 7, + "chatdata": chatdata + } + + ret = processor.process([data]).json() + tobe = _open_file("tests/testdata/default/jsonified_list.json") + assert ret == tobe + + +def test_jsonify_item(mocker): + '''lagacy sponsor(membership)''' + processor = DefaultProcessor() + parser = Parser(is_replay=False) + _json = _open_file("tests/testdata/default/superchat.json") + + _, chatdata = parser.parse(parser.get_contents(json.loads(_json))) + data = { + "video_id": "", + "timeout": 7, + "chatdata": chatdata + } + + ret = processor.process([data]).items[0].json() + tobe = _open_file("tests/testdata/default/jsonified_item.json") + assert ret == tobe + + +def test_jsonify_item(mocker): + '''lagacy sponsor(membership)''' + processor = DefaultProcessor() + parser = Parser(is_replay=False) + _json = _open_file("tests/testdata/default/superchat.json") + + _, chatdata = parser.parse(parser.get_contents(json.loads(_json))) + data = { + "video_id": "", + "timeout": 7, + "chatdata": chatdata + } + + ret = processor.process([data]).items[0].json() + tobe = _open_file("tests/testdata/default/jsonified_item.json") + assert ret == tobe + + def _open_file(path): with open(path, mode='r', encoding='utf-8') as f: return f.read() diff --git a/tests/testdata/default/jsonified_item.json b/tests/testdata/default/jsonified_item.json new file mode 100644 index 0000000..e1e0d31 --- /dev/null +++ b/tests/testdata/default/jsonified_item.json @@ -0,0 +1 @@ +{"author": {"badgeUrl": "", "type": "", "isVerified": false, "isChatOwner": false, "isChatSponsor": false, "isChatModerator": false, "channelId": "author_channel_id", "channelUrl": "http://www.youtube.com/channel/author_channel_id", "name": "author_name", "imageUrl": "https://yt3.ggpht.com/------------/AAAAAAAAAAA/AAAAAAAAAAA/xxxxxxxxxxxx/s64-x-x-xx-xx-xx-c0xffffff/photo.jpg"}, "type": "superChat", "id": "dummy_id", "timestamp": 1570678496000, "elapsedTime": "", "datetime": "2019-10-10 12:34:56", "message": "dummy_message", "messageEx": ["dummy_message"], "amountValue": 800.0, "amountString": "¥800", "currency": "JPY", "bgColor": 4280150454, "colors": {"headerBackgroundColor": 4278239141, "headerTextColor": 4278190080, "bodyBackgroundColor": 4280150454, "bodyTextColor": 4278190080, "timestampColor": 2147483648, "authorNameTextColor": 2315255808}} \ No newline at end of file diff --git a/tests/testdata/default/jsonified_list.json b/tests/testdata/default/jsonified_list.json new file mode 100644 index 0000000..3d6fe70 --- /dev/null +++ b/tests/testdata/default/jsonified_list.json @@ -0,0 +1 @@ +[{"author": {"badgeUrl": "", "type": "", "isVerified": false, "isChatOwner": false, "isChatSponsor": false, "isChatModerator": false, "channelId": "author_channel_id", "channelUrl": "http://www.youtube.com/channel/author_channel_id", "name": "author_name", "imageUrl": "https://yt3.ggpht.com/------------/AAAAAAAAAAA/AAAAAAAAAAA/xxxxxxxxxxxx/s64-x-x-xx-xx-xx-c0xffffff/photo.jpg"}, "type": "superChat", "id": "dummy_id", "timestamp": 1570678496000, "elapsedTime": "", "datetime": "2019-10-10 12:34:56", "message": "dummy_message", "messageEx": ["dummy_message"], "amountValue": 800.0, "amountString": "¥800", "currency": "JPY", "bgColor": 4280150454, "colors": {"headerBackgroundColor": 4278239141, "headerTextColor": 4278190080, "bodyBackgroundColor": 4280150454, "bodyTextColor": 4278190080, "timestampColor": 2147483648, "authorNameTextColor": 2315255808}}] \ No newline at end of file