Move parser to common directory

This commit is contained in:
taizan-hokuto
2019-11-11 21:26:03 +09:00
parent ba4e75063a
commit 40262de6c9
5 changed files with 7 additions and 54 deletions

View File

@@ -10,7 +10,7 @@ import urllib.parse
from aiohttp.client_exceptions import ClientConnectorError from aiohttp.client_exceptions import ClientConnectorError
from concurrent.futures import CancelledError from concurrent.futures import CancelledError
from .buffer import Buffer from .buffer import Buffer
from .parser import Parser from ..parser import Parser
from .. import config from .. import config
from .. import mylogger from .. import mylogger
from ..exceptions import ChatParseException,IllegalFunctionCall from ..exceptions import ChatParseException,IllegalFunctionCall
@@ -81,7 +81,7 @@ 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._setup() self._setup()
if not LiveChatAsync._setup_finished: if not LiveChatAsync._setup_finished:
@@ -164,7 +164,7 @@ class LiveChatAsync:
livechat_json = (await livechat_json = (await
self._get_livechat_json(continuation, session, headers) self._get_livechat_json(continuation, session, headers)
) )
metadata, chatdata = Parser.parse( livechat_json ) metadata, chatdata = self._parser.parse( livechat_json )
timeout = metadata['timeoutMs']/1000 timeout = metadata['timeoutMs']/1000
chat_component = { chat_component = {
"video_id" : self.video_id, "video_id" : self.video_id,

View File

@@ -1,40 +0,0 @@
import json
from .. import config
from .. import mylogger
from .. exceptions import (
ResponseContextError,
NoContentsException,
NoContinuationsException )
logger = mylogger.get_logger(__name__,mode=config.LOGGER_MODE)
class Parser:
@classmethod
def parse(cls, jsn):
if jsn is None:
return {'timeoutMs':0,'continuation':None},[]
if jsn['response']['responseContext'].get('errors'):
raise ResponseContextError('動画に接続できません。'
'動画IDが間違っているか、動画が削除非公開の可能性があります。')
contents=jsn['response'].get('continuationContents')
#配信が終了した場合、もしくはチャットデータが取得できない場合
if contents is None:
raise NoContentsException('チャットデータを取得できませんでした。')
cont = contents['liveChatContinuation']['continuations'][0]
if cont is None:
raise NoContinuationsException('Continuationがありません。')
metadata = (cont.get('invalidationContinuationData') or
cont.get('timedContinuationData') or
cont.get('reloadContinuationData')
)
if metadata is None:
unknown = list(cont.keys())[0]
if unknown:
logger.error(f"Received unknown continuation type:{unknown}")
metadata = cont.get(unknown)
metadata.setdefault('timeoutMs', 10000)
chatdata = contents['liveChatContinuation'].get('actions')
return metadata, chatdata

View File

@@ -8,7 +8,7 @@ import traceback
import urllib.parse import urllib.parse
from concurrent.futures import CancelledError, ThreadPoolExecutor from concurrent.futures import CancelledError, ThreadPoolExecutor
from .buffer import Buffer from .buffer import Buffer
from .parser import Parser from ..parser import Parser
from .. import config from .. import config
from .. import mylogger from .. import mylogger
from ..exceptions import ChatParseException,IllegalFunctionCall from ..exceptions import ChatParseException,IllegalFunctionCall

View File

@@ -182,14 +182,10 @@ class ReplayChat:
) )
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.error(f"{str(e)}動画ID:\"{self.video_id}\"")
return return
@@ -253,13 +249,10 @@ class ReplayChat:
"既にcallbackを登録済みのため、get()は実行できません。") "既にcallbackを登録済みのため、get()は実行できません。")
def pause(self): def pause(self):
'''チャット取得を一時停止する。'''
if not self._pauser.empty(): if not self._pauser.empty():
self._pauser.get() self._pauser.get()
def resume(self): def resume(self):
'''チャット取得を再開する。'''
if self._pauser.empty(): if self._pauser.empty():
self._pauser.put_nowait(None) self._pauser.put_nowait(None)

View File

@@ -1,7 +1,7 @@
import json import json
from .. import config from . import config
from .. import mylogger from . import mylogger
from .. exceptions import ( from . exceptions import (
ResponseContextError, ResponseContextError,
NoContentsException, NoContentsException,
NoContinuationsException ) NoContinuationsException )