Implement 'topchat_only' parameter

: make it possible to select whether to get only top chat.
This commit is contained in:
taizan-hokuto
2020-01-11 04:22:48 +09:00
parent 404623546e
commit ff1ee70d7e
4 changed files with 29 additions and 14 deletions

View File

@@ -64,6 +64,9 @@ class LiveChatAsync:
Trueの場合、ライブチャットが取得できる場合であっても Trueの場合、ライブチャットが取得できる場合であっても
強制的にアーカイブ済みチャットを取得する。 強制的にアーカイブ済みチャットを取得する。
topchat_only : bool
Trueの場合、上位チャットのみ取得する。
Attributes Attributes
--------- ---------
_is_alive : bool _is_alive : bool
@@ -81,7 +84,8 @@ class LiveChatAsync:
done_callback = None, done_callback = None,
exception_handler = None, exception_handler = None,
direct_mode = False, direct_mode = False,
force_replay = False force_replay = False,
topchat_only = False
): ):
self.video_id = video_id self.video_id = video_id
self.seektime = seektime self.seektime = seektime
@@ -102,6 +106,7 @@ class LiveChatAsync:
self._setup() self._setup()
self._first_fetch = True 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 LiveChatAsync._setup_finished: if not LiveChatAsync._setup_finished:
LiveChatAsync._setup_finished = True LiveChatAsync._setup_finished = True
if exception_handler == None: if exception_handler == None:
@@ -200,7 +205,8 @@ class LiveChatAsync:
''' '''
self._pauser.put_nowait(None) self._pauser.put_nowait(None)
if not self._is_replay: if not self._is_replay:
continuation = liveparam.getparam(self.video_id,3) continuation = liveparam.getparam(
self.video_id, 3, self._topchat_only)
return continuation return continuation
async def _get_contents(self, continuation, session, headers): async def _get_contents(self, continuation, session, headers):
@@ -222,7 +228,8 @@ class LiveChatAsync:
self._parser.is_replay = True self._parser.is_replay = True
self._fetch_url = ("live_chat_replay/" self._fetch_url = ("live_chat_replay/"
"get_live_chat_replay?continuation=") "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( livechat_json = (await self._get_livechat_json(
continuation, session, headers)) continuation, session, headers))
contents = self._parser.get_contents(livechat_json) contents = self._parser.get_contents(livechat_json)

View File

@@ -60,6 +60,9 @@ class LiveChat:
Trueの場合、ライブチャットが取得できる場合であっても Trueの場合、ライブチャットが取得できる場合であっても
強制的にアーカイブ済みチャットを取得する。 強制的にアーカイブ済みチャットを取得する。
topchat_only : bool
Trueの場合、上位チャットのみ取得する。
Attributes Attributes
--------- ---------
_executor : ThreadPoolExecutor _executor : ThreadPoolExecutor
@@ -80,7 +83,8 @@ class LiveChat:
callback = None, callback = None,
done_callback = None, done_callback = None,
direct_mode = False, direct_mode = False,
force_replay = False force_replay = False,
topchat_only = False
): ):
self.video_id = video_id self.video_id = video_id
self.seektime = seektime self.seektime = seektime
@@ -100,8 +104,8 @@ class LiveChat:
self._pauser.put_nowait(None) self._pauser.put_nowait(None)
self._setup() self._setup()
self._first_fetch = True 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: if not LiveChat._setup_finished:
LiveChat._setup_finished = True LiveChat._setup_finished = True
if interruptable: if interruptable:

View File

@@ -52,8 +52,8 @@ def _nval(val):
buf += val.to_bytes(1,'big') buf += val.to_bytes(1,'big')
return buf return buf
def _build(video_id, seektime, topchatonly = False): def _build(video_id, seektime, topchat_only):
switch_01 = b'\x04' if topchatonly else b'\x01' switch_01 = b'\x04' if topchat_only else b'\x01'
if seektime < 0: if seektime < 0:
times =_nval(0) times =_nval(0)
switch = b'\x04' switch = b'\x04'
@@ -102,12 +102,14 @@ def _build(video_id, seektime, topchatonly = False):
).decode() ).decode()
) )
def getparam(video_id, seektime = 0): def getparam(video_id, seektime = 0, topchat_only = False):
''' '''
Parameter Parameter
--------- ---------
seektime : int seektime : int
unit:seconds unit:seconds
start position of fetching chat data. 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)

View File

@@ -66,9 +66,9 @@ def _nval(val):
buf += val.to_bytes(1,'big') buf += val.to_bytes(1,'big')
return buf 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 #_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) parity = _tzparity(video_id, _ts1^_ts2^_ts3^_ts4^_ts5)
header_magic= b'\xD2\x87\xCC\xC8\x03' 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])) 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 Parameter
--------- ---------
past_sec : int past_sec : int
seconds to load past chat data 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)