Fix comments
This commit is contained in:
@@ -3,7 +3,6 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import signal
|
import signal
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
@@ -123,7 +122,7 @@ class LiveChatAsync:
|
|||||||
|
|
||||||
async def _startlisten(self):
|
async def _startlisten(self):
|
||||||
"""最初のcontinuationパラメータを取得し、
|
"""最初のcontinuationパラメータを取得し、
|
||||||
_listenループを開始する
|
_listenループのタスクを作成し開始する
|
||||||
"""
|
"""
|
||||||
initial_continuation = await self._get_initial_continuation()
|
initial_continuation = await self._get_initial_continuation()
|
||||||
if initial_continuation is None:
|
if initial_continuation is None:
|
||||||
@@ -286,13 +285,4 @@ class LiveChatAsync:
|
|||||||
logger.debug(f"残っているタスクを終了しています")
|
logger.debug(f"残っているタスクを終了しています")
|
||||||
await asyncio.gather(*tasks,return_exceptions=True)
|
await asyncio.gather(*tasks,return_exceptions=True)
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.stop()
|
loop.stop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -21,8 +21,10 @@ logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
|
|||||||
MAX_RETRY = 10
|
MAX_RETRY = 10
|
||||||
headers = config.headers
|
headers = config.headers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ReplayChatAsync:
|
class ReplayChatAsync:
|
||||||
''' aiohttpを利用してYouTubeのライブ配信のチャットデータを取得する
|
'''asyncio(aiohttp)を利用してYouTubeのチャットデータを取得する。
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
---------
|
---------
|
||||||
@@ -49,6 +51,9 @@ class ReplayChatAsync:
|
|||||||
done_callback : func
|
done_callback : func
|
||||||
listener終了時に呼び出すコールバック。
|
listener終了時に呼び出すコールバック。
|
||||||
|
|
||||||
|
exception_handler : func
|
||||||
|
例外を処理する関数
|
||||||
|
|
||||||
direct_mode : bool
|
direct_mode : bool
|
||||||
Trueの場合、bufferを使わずにcallbackを呼ぶ。
|
Trueの場合、bufferを使わずにcallbackを呼ぶ。
|
||||||
Trueの場合、callbackの設定が必須
|
Trueの場合、callbackの設定が必須
|
||||||
@@ -56,11 +61,8 @@ class ReplayChatAsync:
|
|||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
---------
|
---------
|
||||||
_executor : ThreadPoolExecutor
|
|
||||||
チャットデータ取得ループ(_listen)用のスレッド
|
|
||||||
|
|
||||||
_is_alive : bool
|
_is_alive : bool
|
||||||
チャット取得を終了したか
|
チャット取得を停止するためのフラグ
|
||||||
'''
|
'''
|
||||||
|
|
||||||
_setup_finished = False
|
_setup_finished = False
|
||||||
@@ -68,7 +70,7 @@ class ReplayChatAsync:
|
|||||||
def __init__(self, video_id,
|
def __init__(self, video_id,
|
||||||
seektime = 0,
|
seektime = 0,
|
||||||
processor = DefaultProcessor(),
|
processor = DefaultProcessor(),
|
||||||
buffer = Buffer(maxsize = 20),
|
buffer = None,
|
||||||
interruptable = True,
|
interruptable = True,
|
||||||
callback = None,
|
callback = None,
|
||||||
done_callback = None,
|
done_callback = None,
|
||||||
@@ -154,8 +156,8 @@ class ReplayChatAsync:
|
|||||||
|
|
||||||
async def _listen(self, continuation):
|
async def _listen(self, continuation):
|
||||||
''' continuationに紐付いたチャットデータを取得し
|
''' continuationに紐付いたチャットデータを取得し
|
||||||
にチャットデータを格納、
|
Bufferにチャットデータを格納、
|
||||||
次のcontinuaitonを取得してループする
|
次のcontinuaitonを取得してループする。
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
---------
|
---------
|
||||||
@@ -189,11 +191,10 @@ class ReplayChatAsync:
|
|||||||
else:
|
else:
|
||||||
await self._buffer.put(chat_component)
|
await self._buffer.put(chat_component)
|
||||||
diff_time = timeout - (time.time()-time_mark)
|
diff_time = timeout - (time.time()-time_mark)
|
||||||
if diff_time < 0 : diff_time=0
|
|
||||||
await asyncio.sleep(diff_time)
|
await asyncio.sleep(diff_time)
|
||||||
continuation = metadata.get('continuation')
|
continuation = metadata.get('continuation')
|
||||||
except ChatParseException as e:
|
except ChatParseException as e:
|
||||||
logger.error(f"{str(e)}(動画ID:\"{self.video_id}\")")
|
logger.info(f"{str(e)}(video_id:\"{self.video_id}\")")
|
||||||
return
|
return
|
||||||
except (TypeError , json.JSONDecodeError) :
|
except (TypeError , json.JSONDecodeError) :
|
||||||
logger.error(f"{traceback.format_exc(limit = -1)}")
|
logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class LiveChat:
|
|||||||
チャットデータ取得ループ(_listen)用のスレッド
|
チャットデータ取得ループ(_listen)用のスレッド
|
||||||
|
|
||||||
_is_alive : bool
|
_is_alive : bool
|
||||||
チャット取得を終了したか
|
チャット取得を停止するためのフラグ
|
||||||
'''
|
'''
|
||||||
|
|
||||||
_setup_finished = False
|
_setup_finished = False
|
||||||
@@ -142,7 +142,7 @@ class LiveChat:
|
|||||||
|
|
||||||
def _listen(self, continuation):
|
def _listen(self, continuation):
|
||||||
''' continuationに紐付いたチャットデータを取得し
|
''' continuationに紐付いたチャットデータを取得し
|
||||||
にチャットデータを格納、
|
BUfferにチャットデータを格納、
|
||||||
次のcontinuaitonを取得してループする
|
次のcontinuaitonを取得してループする
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
@@ -157,7 +157,6 @@ class LiveChat:
|
|||||||
self._get_livechat_json(continuation, session, headers)
|
self._get_livechat_json(continuation, session, headers)
|
||||||
)
|
)
|
||||||
metadata, chatdata = self._parser.parse( livechat_json )
|
metadata, chatdata = self._parser.parse( livechat_json )
|
||||||
#チャットデータを含むコンポーネントを組み立ててbufferに投入する
|
|
||||||
timeout = metadata['timeoutMs']/1000
|
timeout = metadata['timeoutMs']/1000
|
||||||
chat_component = {
|
chat_component = {
|
||||||
"video_id" : self.video_id,
|
"video_id" : self.video_id,
|
||||||
@@ -171,16 +170,12 @@ class LiveChat:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._buffer.put(chat_component)
|
self._buffer.put(chat_component)
|
||||||
#次のchatを取得するまでsleepする
|
|
||||||
diff_time = timeout - (time.time()-time_mark)
|
diff_time = timeout - (time.time()-time_mark)
|
||||||
if diff_time < 0 : diff_time=0
|
if diff_time < 0 : diff_time=0
|
||||||
time.sleep(diff_time)
|
time.sleep(diff_time)
|
||||||
#次のチャットデータのcontinuationパラメータを取り出す。
|
|
||||||
continuation = metadata.get('continuation')
|
continuation = metadata.get('continuation')
|
||||||
|
|
||||||
#whileループ先頭に戻る
|
|
||||||
except ChatParseException as e:
|
except ChatParseException as e:
|
||||||
logger.error(f"{str(e)}(動画ID:\"{self.video_id}\")")
|
logger.info(f"{str(e)}(video_id:\"{self.video_id}\")")
|
||||||
return
|
return
|
||||||
except (TypeError , json.JSONDecodeError) :
|
except (TypeError , json.JSONDecodeError) :
|
||||||
logger.error(f"{traceback.format_exc(limit = -1)}")
|
logger.error(f"{traceback.format_exc(limit = -1)}")
|
||||||
|
|||||||
Reference in New Issue
Block a user