Merge tag 'termination' into develop

This commit is contained in:
taizan-hokuto
2020-05-05 21:18:46 +09:00
3 changed files with 166 additions and 173 deletions

View File

@@ -72,8 +72,6 @@ class LiveChat:
'''
_setup_finished = False
#チャット監視中のListenerのリスト
_listeners = []
def __init__(self, video_id,
seektime=0,
@@ -103,19 +101,13 @@ class LiveChat:
self._parser = Parser(is_replay=self._is_replay)
self._pauser = Queue()
self._pauser.put_nowait(None)
self._setup()
self._first_fetch = True
self._fetch_url = "live_chat/get_live_chat?continuation="
self._topchat_only = topchat_only
self._logger = logger
LiveChat._logger = logger
if not LiveChat._setup_finished:
LiveChat._setup_finished = True
if interruptable:
signal.signal(signal.SIGINT, (lambda a, b:
(LiveChat.shutdown(None,signal.SIGINT,b))
))
LiveChat._listeners.append(self)
signal.signal(signal.SIGINT, lambda a, b: self.terminate())
self._setup()
def _setup(self):
# direct modeがTrueでcallback未設定の場合例外発生。
@@ -174,7 +166,8 @@ class LiveChat:
}
time_mark = time.time()
if self._direct_mode:
processed_chat = self.processor.process([chat_component])
processed_chat = self.processor.process(
[chat_component])
if isinstance(processed_chat, tuple):
self._callback(*processed_chat)
else:
@@ -319,14 +312,7 @@ class LiveChat:
'''
Listenerを終了する。
'''
if self.is_alive():
self._is_alive = False
if self._direct_mode == False:
#bufferにダミーオブジェクトを入れてis_alive()を判定させる
self._buffer.put({'chatdata':'','timeout':0})
self._logger.info(f'[{self.video_id}]finished.')
@classmethod
def shutdown(cls, event, sig = None, handler=None):
cls._logger.debug("shutdown...")
for t in LiveChat._listeners:
t._is_alive = False
self._buffer.put({})
self._logger.info(f'[{self.video_id}]終了しました')

View File

@@ -12,6 +12,7 @@ Author: taizan-hokuto (2019) @taizan205
ver 0.0.1 2019.10.05
'''
def _gen_vid(video_id):
"""generate video_id parameter.
Parameter
@@ -40,9 +41,11 @@ def _gen_vid(video_id):
b64enc(reduce(lambda x, y: x+y, item)).decode()
).encode()
def _nval(val):
"""convert value to byte array"""
if val<0: raise ValueError
if val < 0:
raise ValueError
buf = b''
while val >> 7:
m = val & 0xFF | 0x80
@@ -51,6 +54,7 @@ def _nval(val):
buf += val.to_bytes(1, 'big')
return buf
def _build(video_id, seektime, topchat_only):
switch_01 = b'\x04' if topchat_only else b'\x01'
if seektime < 0:
@@ -75,7 +79,8 @@ def _build(video_id, seektime, topchat_only):
sep_3 = b'\x00\x58\x03\x60'
sep_4 = b'\x68' + parity + b'\x72\x04\x08'
sep_5 = b'\x10' + parity + b'\x78\x00'
body = [
body = b''.join([
sep_0,
_nval(len(vid)),
vid,
@@ -90,9 +95,7 @@ def _build(video_id, seektime, topchat_only):
sep_4,
switch_01,
sep_5
]
body = reduce(lambda x, y: x+y, body)
])
return urllib.parse.quote(
b64enc(header_magic +
@@ -101,6 +104,7 @@ def _build(video_id, seektime, topchat_only):
).decode()
)
def getparam(video_id, seektime=0, topchat_only=False):
'''
Parameter

View File

@@ -11,6 +11,8 @@ Author: taizan-hokuto (2019) @taizan205
ver 0.0.1 2019.10.05
'''
def _gen_vid(video_id):
"""generate video_id parameter.
Parameter
@@ -44,6 +46,7 @@ def _gen_vid(video_id):
b64enc(reduce(lambda x, y: x+y, item)).decode()
).encode()
def _tzparity(video_id, times):
t = 0
for i, s in enumerate(video_id):
@@ -55,9 +58,11 @@ def _tzparity(video_id,times):
return ((times ^ t) % 2).to_bytes(1, 'big')
def _nval(val):
"""convert value to byte array"""
if val<0: raise ValueError
if val < 0:
raise ValueError
buf = b''
while val >> 7:
m = val & 0xFF | 0x80
@@ -66,6 +71,7 @@ def _nval(val):
buf += val.to_bytes(1, 'big')
return buf
def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
# _short_type2
switch_01 = b'\x04' if topchat_only else b'\x01'
@@ -99,7 +105,7 @@ def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
sep_9 = b'\x88\x01\x00\xA0\x01'
timestamp5 = _nval(_ts5)
body = [
body = b''.join([
sep_0,
_nval(len(vid)),
vid,
@@ -121,15 +127,13 @@ def _build(video_id, _ts1, _ts2, _ts3, _ts4, _ts5, topchat_only):
ts_3_end,
timestamp4,
sep_6,
switch_01,#
switch_01,
sep_7,
switch_01,#
switch_01,
sep_8,
sep_9,
timestamp5
]
body = reduce(lambda x, y: x+y, body)
])
return urllib.parse.quote(
b64enc(header_magic +
@@ -161,4 +165,3 @@ def getparam(video_id, past_sec = 0, topchat_only = False):
if True, fetch only 'top chat'
'''
return _build(video_id, *_times(past_sec), topchat_only)