Implement force_replay_mode

This commit is contained in:
taizan-hokuto
2020-01-04 13:23:32 +09:00
parent 5d86fb4b71
commit 26fefddddf
3 changed files with 58 additions and 40 deletions

View File

@@ -28,6 +28,11 @@ class LiveChat:
---------
video_id : str
動画ID
seektime : int
(ライブチャット取得時は無視)
取得開始するアーカイブ済みチャットの経過時間(秒)
マイナス値を指定した場合は、配信開始前のチャットも取得する。
processor : ChatProcessor
チャットデータを加工するオブジェクト
@@ -51,6 +56,10 @@ class LiveChat:
Trueの場合、callbackの設定が必須
(設定していない場合IllegalFunctionCall例外を発生させる
force_replay : bool
Trueの場合、ライブチャットが取得できる場合であっても
強制的にアーカイブ済みチャットを取得する。
Attributes
---------
_executor : ThreadPoolExecutor
@@ -70,7 +79,8 @@ class LiveChat:
interruptable = True,
callback = None,
done_callback = None,
direct_mode = False
direct_mode = False,
force_replay = False
):
self.video_id = video_id
self.seektime = seektime
@@ -84,7 +94,8 @@ class LiveChat:
self._executor = ThreadPoolExecutor(max_workers=2)
self._direct_mode = direct_mode
self._is_alive = True
self._parser = Parser()
self._is_replay = force_replay
self._parser = Parser(is_replay = self._is_replay)
self._pauser = Queue()
self._pauser.put_nowait(None)
self._setup()
@@ -184,7 +195,7 @@ class LiveChat:
prohibit from blocking by putting None into _pauser.
'''
self._pauser.put_nowait(None)
if self._parser.mode == 'LIVE':
if not self._is_replay:
continuation = liveparam.getparam(self.video_id,3)
return continuation
@@ -202,9 +213,9 @@ class LiveChat:
)
contents = self._parser.get_contents(livechat_json)
if self._first_fetch:
if contents is None:
if contents is None or self._is_replay:
'''Try to fetch archive chat data.'''
self._parser.mode = 'REPLAY'
self._parser.is_replay = True
self._fetch_url = ("live_chat_replay/"
"get_live_chat_replay?continuation=")
continuation = arcparam.getparam(self.video_id, self.seektime)
@@ -235,7 +246,6 @@ class LiveChat:
else:
logger.error(f"[{self.video_id}]"
f"Exceeded retry count. status_code={status_code}")
#self.terminate()
return None
return livechat_json
@@ -266,8 +276,8 @@ class LiveChat:
raise IllegalFunctionCall(
"既にcallbackを登録済みのため、get()は実行できません。")
def get_mode(self):
return self._parser.mode
def is_replay(self):
return self._is_replay
def pause(self):
if self._callback is None:
@@ -298,7 +308,7 @@ class LiveChat:
self._is_alive = False
if self._direct_mode == False:
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
self._buffer.put({'chatdata':'','timeout':1})
self._buffer.put({'chatdata':'','timeout':0})
logger.info(f'[{self.video_id}]finished.')
@classmethod