Implement force_replay_mode
This commit is contained in:
@@ -30,6 +30,11 @@ class LiveChatAsync:
|
|||||||
video_id : str
|
video_id : str
|
||||||
動画ID
|
動画ID
|
||||||
|
|
||||||
|
seektime : int
|
||||||
|
(ライブチャット取得時は無視)
|
||||||
|
取得開始するアーカイブ済みチャットの経過時間(秒)
|
||||||
|
マイナス値を指定した場合は、配信開始前のチャットも取得する。
|
||||||
|
|
||||||
processor : ChatProcessor
|
processor : ChatProcessor
|
||||||
チャットデータを加工するオブジェクト
|
チャットデータを加工するオブジェクト
|
||||||
|
|
||||||
@@ -53,7 +58,11 @@ class LiveChatAsync:
|
|||||||
direct_mode : bool
|
direct_mode : bool
|
||||||
Trueの場合、bufferを使わずにcallbackを呼ぶ。
|
Trueの場合、bufferを使わずにcallbackを呼ぶ。
|
||||||
Trueの場合、callbackの設定が必須
|
Trueの場合、callbackの設定が必須
|
||||||
(設定していない場合IllegalFunctionCall例外を発生させる)
|
(設定していない場合IllegalFunctionCall例外を発生させる)
|
||||||
|
|
||||||
|
force_replay : bool
|
||||||
|
Trueの場合、ライブチャットが取得できる場合であっても
|
||||||
|
強制的にアーカイブ済みチャットを取得する。
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
---------
|
---------
|
||||||
@@ -71,7 +80,9 @@ class LiveChatAsync:
|
|||||||
callback = None,
|
callback = None,
|
||||||
done_callback = None,
|
done_callback = None,
|
||||||
exception_handler = None,
|
exception_handler = None,
|
||||||
direct_mode = False):
|
direct_mode = False,
|
||||||
|
force_replay = False
|
||||||
|
):
|
||||||
self.video_id = video_id
|
self.video_id = video_id
|
||||||
self.seektime = seektime
|
self.seektime = seektime
|
||||||
if isinstance(processor, tuple):
|
if isinstance(processor, tuple):
|
||||||
@@ -84,7 +95,8 @@ class LiveChatAsync:
|
|||||||
self._exception_handler = exception_handler
|
self._exception_handler = exception_handler
|
||||||
self._direct_mode = direct_mode
|
self._direct_mode = direct_mode
|
||||||
self._is_alive = True
|
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 = Queue()
|
||||||
self._pauser.put_nowait(None)
|
self._pauser.put_nowait(None)
|
||||||
self._setup()
|
self._setup()
|
||||||
@@ -187,7 +199,7 @@ class LiveChatAsync:
|
|||||||
prohibit from blocking by putting None into _pauser.
|
prohibit from blocking by putting None into _pauser.
|
||||||
'''
|
'''
|
||||||
self._pauser.put_nowait(None)
|
self._pauser.put_nowait(None)
|
||||||
if self._parser.mode == 'LIVE':
|
if not self._is_replay:
|
||||||
continuation = liveparam.getparam(self.video_id,3)
|
continuation = liveparam.getparam(self.video_id,3)
|
||||||
return continuation
|
return continuation
|
||||||
|
|
||||||
@@ -205,9 +217,9 @@ class LiveChatAsync:
|
|||||||
)
|
)
|
||||||
contents = self._parser.get_contents(livechat_json)
|
contents = self._parser.get_contents(livechat_json)
|
||||||
if self._first_fetch:
|
if self._first_fetch:
|
||||||
if contents is None:
|
if contents is None or self._is_replay:
|
||||||
'''Try to fetch archive chat data.'''
|
'''Try to fetch archive chat data.'''
|
||||||
self._parser.mode = 'REPLAY'
|
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)
|
||||||
@@ -268,8 +280,8 @@ class LiveChatAsync:
|
|||||||
raise IllegalFunctionCall(
|
raise IllegalFunctionCall(
|
||||||
"既にcallbackを登録済みのため、get()は実行できません。")
|
"既にcallbackを登録済みのため、get()は実行できません。")
|
||||||
|
|
||||||
def get_mode(self):
|
def is_replay(self):
|
||||||
return self._parser.mode
|
return self._is_replay
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
if self._callback is None:
|
if self._callback is None:
|
||||||
@@ -300,7 +312,7 @@ class LiveChatAsync:
|
|||||||
self._is_alive = False
|
self._is_alive = False
|
||||||
if self._direct_mode == False:
|
if self._direct_mode == False:
|
||||||
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
||||||
self._buffer.put_nowait({'chatdata':'','timeout':1})
|
self._buffer.put_nowait({'chatdata':'','timeout':0})
|
||||||
logger.info(f'[{self.video_id}]finished.')
|
logger.info(f'[{self.video_id}]finished.')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ class LiveChat:
|
|||||||
---------
|
---------
|
||||||
video_id : str
|
video_id : str
|
||||||
動画ID
|
動画ID
|
||||||
|
|
||||||
|
seektime : int
|
||||||
|
(ライブチャット取得時は無視)
|
||||||
|
取得開始するアーカイブ済みチャットの経過時間(秒)
|
||||||
|
マイナス値を指定した場合は、配信開始前のチャットも取得する。
|
||||||
|
|
||||||
processor : ChatProcessor
|
processor : ChatProcessor
|
||||||
チャットデータを加工するオブジェクト
|
チャットデータを加工するオブジェクト
|
||||||
@@ -51,6 +56,10 @@ class LiveChat:
|
|||||||
Trueの場合、callbackの設定が必須
|
Trueの場合、callbackの設定が必須
|
||||||
(設定していない場合IllegalFunctionCall例外を発生させる)
|
(設定していない場合IllegalFunctionCall例外を発生させる)
|
||||||
|
|
||||||
|
force_replay : bool
|
||||||
|
Trueの場合、ライブチャットが取得できる場合であっても
|
||||||
|
強制的にアーカイブ済みチャットを取得する。
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
---------
|
---------
|
||||||
_executor : ThreadPoolExecutor
|
_executor : ThreadPoolExecutor
|
||||||
@@ -70,7 +79,8 @@ class LiveChat:
|
|||||||
interruptable = True,
|
interruptable = True,
|
||||||
callback = None,
|
callback = None,
|
||||||
done_callback = None,
|
done_callback = None,
|
||||||
direct_mode = False
|
direct_mode = False,
|
||||||
|
force_replay = False
|
||||||
):
|
):
|
||||||
self.video_id = video_id
|
self.video_id = video_id
|
||||||
self.seektime = seektime
|
self.seektime = seektime
|
||||||
@@ -84,7 +94,8 @@ class LiveChat:
|
|||||||
self._executor = ThreadPoolExecutor(max_workers=2)
|
self._executor = ThreadPoolExecutor(max_workers=2)
|
||||||
self._direct_mode = direct_mode
|
self._direct_mode = direct_mode
|
||||||
self._is_alive = True
|
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 = Queue()
|
||||||
self._pauser.put_nowait(None)
|
self._pauser.put_nowait(None)
|
||||||
self._setup()
|
self._setup()
|
||||||
@@ -184,7 +195,7 @@ class LiveChat:
|
|||||||
prohibit from blocking by putting None into _pauser.
|
prohibit from blocking by putting None into _pauser.
|
||||||
'''
|
'''
|
||||||
self._pauser.put_nowait(None)
|
self._pauser.put_nowait(None)
|
||||||
if self._parser.mode == 'LIVE':
|
if not self._is_replay:
|
||||||
continuation = liveparam.getparam(self.video_id,3)
|
continuation = liveparam.getparam(self.video_id,3)
|
||||||
return continuation
|
return continuation
|
||||||
|
|
||||||
@@ -202,9 +213,9 @@ class LiveChat:
|
|||||||
)
|
)
|
||||||
contents = self._parser.get_contents(livechat_json)
|
contents = self._parser.get_contents(livechat_json)
|
||||||
if self._first_fetch:
|
if self._first_fetch:
|
||||||
if contents is None:
|
if contents is None or self._is_replay:
|
||||||
'''Try to fetch archive chat data.'''
|
'''Try to fetch archive chat data.'''
|
||||||
self._parser.mode = 'REPLAY'
|
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)
|
||||||
@@ -235,7 +246,6 @@ class LiveChat:
|
|||||||
else:
|
else:
|
||||||
logger.error(f"[{self.video_id}]"
|
logger.error(f"[{self.video_id}]"
|
||||||
f"Exceeded retry count. status_code={status_code}")
|
f"Exceeded retry count. status_code={status_code}")
|
||||||
#self.terminate()
|
|
||||||
return None
|
return None
|
||||||
return livechat_json
|
return livechat_json
|
||||||
|
|
||||||
@@ -266,8 +276,8 @@ class LiveChat:
|
|||||||
raise IllegalFunctionCall(
|
raise IllegalFunctionCall(
|
||||||
"既にcallbackを登録済みのため、get()は実行できません。")
|
"既にcallbackを登録済みのため、get()は実行できません。")
|
||||||
|
|
||||||
def get_mode(self):
|
def is_replay(self):
|
||||||
return self._parser.mode
|
return self._is_replay
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
if self._callback is None:
|
if self._callback is None:
|
||||||
@@ -298,7 +308,7 @@ class LiveChat:
|
|||||||
self._is_alive = False
|
self._is_alive = False
|
||||||
if self._direct_mode == False:
|
if self._direct_mode == False:
|
||||||
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
|
||||||
self._buffer.put({'chatdata':'','timeout':1})
|
self._buffer.put({'chatdata':'','timeout':0})
|
||||||
logger.info(f'[{self.video_id}]finished.')
|
logger.info(f'[{self.video_id}]finished.')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
pytchat.parser.live
|
pytchat.parser.live
|
||||||
~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~
|
||||||
This module is parser of live chat JSON.
|
Parser of live chat JSON.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
@@ -15,11 +15,12 @@ from .. exceptions import (
|
|||||||
|
|
||||||
logger = config.logger(__name__)
|
logger = config.logger(__name__)
|
||||||
|
|
||||||
from .. import util
|
|
||||||
class Parser:
|
class Parser:
|
||||||
|
|
||||||
def __init__(self):
|
__slots__ = ['is_replay']
|
||||||
self.mode = 'LIVE'
|
|
||||||
|
def __init__(self, is_replay):
|
||||||
|
self.is_replay = is_replay
|
||||||
|
|
||||||
def get_contents(self, jsn):
|
def get_contents(self, jsn):
|
||||||
if jsn is None:
|
if jsn is None:
|
||||||
@@ -31,29 +32,23 @@ class Parser:
|
|||||||
|
|
||||||
def parse(self, contents):
|
def parse(self, contents):
|
||||||
"""
|
"""
|
||||||
このparse関数はLiveChat._listen() 関数から定期的に呼び出される。
|
|
||||||
引数contentsはYoutubeから取得したチャットデータの生JSONであり、
|
|
||||||
与えられたJSONをチャットデータとメタデータに分割して返す。
|
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
----------
|
----------
|
||||||
+ contents : dict
|
+ contents : dict
|
||||||
+ Youtubeから取得したチャットデータのJSONオブジェクト。
|
+ JSON of chat data from YouTube.
|
||||||
(pythonの辞書形式に変換済みの状態で渡される)
|
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
tuple:
|
tuple:
|
||||||
+ metadata : dict チャットデータに付随するメタデータ
|
+ metadata : dict
|
||||||
+ timeout
|
+ timeout
|
||||||
+ video_id
|
+ video_id
|
||||||
+ continuation
|
+ continuation
|
||||||
+ chatdata : list[dict]
|
+ chatdata : List[dict]
|
||||||
チャットデータ本体のリスト。
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if contents is None:
|
if contents is None:
|
||||||
'''配信が終了した場合、もしくはチャットデータが取得できない場合'''
|
'''Broadcasting end or cannot fetch chat stream'''
|
||||||
raise NoContentsException('Chat data stream is empty.')
|
raise NoContentsException('Chat data stream is empty.')
|
||||||
|
|
||||||
cont = contents['liveChatContinuation']['continuations'][0]
|
cont = contents['liveChatContinuation']['continuations'][0]
|
||||||
@@ -76,15 +71,16 @@ class Parser:
|
|||||||
return self._create_data(metadata, contents)
|
return self._create_data(metadata, contents)
|
||||||
|
|
||||||
def _create_data(self, metadata, contents):
|
def _create_data(self, metadata, contents):
|
||||||
chatdata = contents['liveChatContinuation'].get('actions')
|
actions = contents['liveChatContinuation'].get('actions')
|
||||||
if self.mode == 'LIVE':
|
if self.is_replay:
|
||||||
metadata.setdefault('timeoutMs', 10000)
|
interval = self._get_interval(actions)
|
||||||
else:
|
|
||||||
interval = self._get_interval(chatdata)
|
|
||||||
metadata.setdefault("timeoutMs",interval)
|
metadata.setdefault("timeoutMs",interval)
|
||||||
"""アーカイブ済みチャットはライブチャットと構造が異なっているため、以下の行により
|
"""Archived chat has different structures than live chat,
|
||||||
ライブチャットと同じ形式にそろえる"""
|
so make it the same format."""
|
||||||
chatdata = [action["replayChatItemAction"]["actions"][0] for action in chatdata]
|
chatdata = [action["replayChatItemAction"]["actions"][0] for action in actions]
|
||||||
|
else:
|
||||||
|
metadata.setdefault('timeoutMs', 10000)
|
||||||
|
chatdata = actions
|
||||||
return metadata, chatdata
|
return metadata, chatdata
|
||||||
|
|
||||||
def _get_interval(self, actions: list):
|
def _get_interval(self, actions: list):
|
||||||
|
|||||||
Reference in New Issue
Block a user