From 404623546e6b17fd3d968ed4704deb60a810643b Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Fri, 10 Jan 2020 01:10:12 +0900 Subject: [PATCH 1/3] Exclude test requirements --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9728c33..84683f3 100644 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ setup( long_description_content_type='text/markdown', license=license, install_requires=_requirements(), - tests_require=_test_requirements(), + #tests_require=_test_requirements(), description="a python library for fetching youtube live chat.", classifiers=[ 'Natural Language :: Japanese', From ff1ee70d7e5051a41af21f5bb23249526a0d8ba8 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Sat, 11 Jan 2020 04:22:48 +0900 Subject: [PATCH 2/3] Implement 'topchat_only' parameter : make it possible to select whether to get only top chat. --- pytchat/core_async/livechat.py | 13 ++++++++++--- pytchat/core_multithread/livechat.py | 10 +++++++--- pytchat/paramgen/arcparam.py | 10 ++++++---- pytchat/paramgen/liveparam.py | 10 ++++++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pytchat/core_async/livechat.py b/pytchat/core_async/livechat.py index 27341ee..efde22d 100644 --- a/pytchat/core_async/livechat.py +++ b/pytchat/core_async/livechat.py @@ -63,6 +63,9 @@ class LiveChatAsync: force_replay : bool Trueの場合、ライブチャットが取得できる場合であっても 強制的にアーカイブ済みチャットを取得する。 + + topchat_only : bool + Trueの場合、上位チャットのみ取得する。 Attributes --------- @@ -81,7 +84,8 @@ class LiveChatAsync: done_callback = None, exception_handler = None, direct_mode = False, - force_replay = False + force_replay = False, + topchat_only = False ): self.video_id = video_id self.seektime = seektime @@ -102,6 +106,7 @@ class LiveChatAsync: self._setup() self._first_fetch = True self._fetch_url = "live_chat/get_live_chat?continuation=" + self._topchat_only = topchat_only if not LiveChatAsync._setup_finished: LiveChatAsync._setup_finished = True if exception_handler == None: @@ -200,7 +205,8 @@ class LiveChatAsync: ''' self._pauser.put_nowait(None) if not self._is_replay: - continuation = liveparam.getparam(self.video_id,3) + continuation = liveparam.getparam( + self.video_id, 3, self._topchat_only) return continuation async def _get_contents(self, continuation, session, headers): @@ -222,7 +228,8 @@ class LiveChatAsync: self._parser.is_replay = True self._fetch_url = ("live_chat_replay/" "get_live_chat_replay?continuation=") - continuation = arcparam.getparam(self.video_id, self.seektime) + continuation = arcparam.getparam( + self.video_id, self.seektime, self._topchat_only) livechat_json = (await self._get_livechat_json( continuation, session, headers)) contents = self._parser.get_contents(livechat_json) diff --git a/pytchat/core_multithread/livechat.py b/pytchat/core_multithread/livechat.py index 766cfcc..4cccb1b 100644 --- a/pytchat/core_multithread/livechat.py +++ b/pytchat/core_multithread/livechat.py @@ -60,6 +60,9 @@ class LiveChat: Trueの場合、ライブチャットが取得できる場合であっても 強制的にアーカイブ済みチャットを取得する。 + topchat_only : bool + Trueの場合、上位チャットのみ取得する。 + Attributes --------- _executor : ThreadPoolExecutor @@ -80,7 +83,8 @@ class LiveChat: callback = None, done_callback = None, direct_mode = False, - force_replay = False + force_replay = False, + topchat_only = False ): self.video_id = video_id self.seektime = seektime @@ -100,8 +104,8 @@ class LiveChat: self._pauser.put_nowait(None) self._setup() self._first_fetch = True - self._fetch_url = "live_chat/get_live_chat?continuation=" - + self._fetch_url = "live_chat/get_live_chat?continuation=", + self._topchat_only = topchat_only if not LiveChat._setup_finished: LiveChat._setup_finished = True if interruptable: diff --git a/pytchat/paramgen/arcparam.py b/pytchat/paramgen/arcparam.py index 1d80ae1..2ac64e0 100644 --- a/pytchat/paramgen/arcparam.py +++ b/pytchat/paramgen/arcparam.py @@ -52,8 +52,8 @@ def _nval(val): buf += val.to_bytes(1,'big') return buf -def _build(video_id, seektime, topchatonly = False): - switch_01 = b'\x04' if topchatonly else b'\x01' +def _build(video_id, seektime, topchat_only): + switch_01 = b'\x04' if topchat_only else b'\x01' if seektime < 0: times =_nval(0) switch = b'\x04' @@ -102,12 +102,14 @@ def _build(video_id, seektime, topchatonly = False): ).decode() ) -def getparam(video_id, seektime = 0): +def getparam(video_id, seektime = 0, topchat_only = False): ''' Parameter --------- seektime : int unit:seconds start position of fetching chat data. + topchat_only : bool + if True, fetch only 'top chat' ''' - return _build(video_id, seektime) + return _build(video_id, seektime, topchat_only) diff --git a/pytchat/paramgen/liveparam.py b/pytchat/paramgen/liveparam.py index f3bb8b7..9965cfa 100644 --- a/pytchat/paramgen/liveparam.py +++ b/pytchat/paramgen/liveparam.py @@ -66,9 +66,9 @@ def _nval(val): buf += val.to_bytes(1,'big') return buf -def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchatonly = False): +def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only): #_short_type2 - switch_01 = b'\x04' if topchatonly else b'\x01' + switch_01 = b'\x04' if topchat_only else b'\x01' parity = _tzparity(video_id, _ts1^_ts2^_ts3^_ts4^_ts5) header_magic= b'\xD2\x87\xCC\xC8\x03' @@ -155,12 +155,14 @@ def _times(past_sec): return list(map(lambda x:int(x*1000000),[_ts1,_ts2,_ts3,_ts4,_ts5])) -def getparam(video_id,past_sec = 0): +def getparam(video_id, past_sec = 0, topchat_only = False): ''' Parameter --------- past_sec : int seconds to load past chat data + topchat_only : bool + if True, fetch only 'top chat' ''' - return _build(video_id,*_times(past_sec)) + return _build(video_id,*_times(past_sec),topchat_only) From 6c8d390fc7dd855008e298e2ed0b02b8b91b881f Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Sat, 11 Jan 2020 04:41:39 +0900 Subject: [PATCH 3/3] Modify test --- tests/test_liveparam.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_liveparam.py b/tests/test_liveparam.py index 320a15f..0260fa2 100644 --- a/tests/test_liveparam.py +++ b/tests/test_liveparam.py @@ -4,6 +4,6 @@ from pytchat.paramgen import liveparam def test_liveparam_0(mocker): _ts1= 1546268400 param = liveparam._build("01234567890", - *([_ts1*1000000 for i in range(5)])) + *([_ts1*1000000 for i in range(5)]), topchat_only=False) test_param="0ofMyAPiARp8Q2c4S0RRb0xNREV5TXpRMU5qYzRPVEFhUTZxNXdiMEJQUW83YUhSMGNITTZMeTkzZDNjdWVXOTFkSFZpWlM1amIyMHZiR2wyWlY5amFHRjBQM1k5TURFeU16UTFOamM0T1RBbWFYTmZjRzl3YjNWMFBURWdBZyUzRCUzRCiAuNbVqsrfAjAAOABAAkorCAEQABgAIAAqDnN0YXRpY2NoZWNrc3VtOgBAAEoCCAFQgLjW1arK3wJYA1CAuNbVqsrfAliAuNbVqsrfAmgBggEECAEQAIgBAKABgLjW1arK3wI%3D" assert test_param == param \ No newline at end of file