From 40262de6c9691e0a42370aa949fc64d9e54f13d0 Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Mon, 11 Nov 2019 21:26:03 +0900 Subject: [PATCH] Move parser to common directory --- pytchat/core_async/livechat.py | 6 ++-- pytchat/core_async/parser.py | 40 ------------------------ pytchat/core_multithread/livechat.py | 2 +- pytchat/core_multithread/replaychat.py | 7 ----- pytchat/{core_multithread => }/parser.py | 6 ++-- 5 files changed, 7 insertions(+), 54 deletions(-) delete mode 100644 pytchat/core_async/parser.py rename pytchat/{core_multithread => }/parser.py (95%) diff --git a/pytchat/core_async/livechat.py b/pytchat/core_async/livechat.py index 9388442..aa92adc 100644 --- a/pytchat/core_async/livechat.py +++ b/pytchat/core_async/livechat.py @@ -10,7 +10,7 @@ import urllib.parse from aiohttp.client_exceptions import ClientConnectorError from concurrent.futures import CancelledError from .buffer import Buffer -from .parser import Parser +from ..parser import Parser from .. import config from .. import mylogger from ..exceptions import ChatParseException,IllegalFunctionCall @@ -81,7 +81,7 @@ class LiveChatAsync: self._exception_handler = exception_handler self._direct_mode = direct_mode self._is_alive = True - + self._parser = Parser() self._setup() if not LiveChatAsync._setup_finished: @@ -164,7 +164,7 @@ class LiveChatAsync: livechat_json = (await self._get_livechat_json(continuation, session, headers) ) - metadata, chatdata = Parser.parse( livechat_json ) + metadata, chatdata = self._parser.parse( livechat_json ) timeout = metadata['timeoutMs']/1000 chat_component = { "video_id" : self.video_id, diff --git a/pytchat/core_async/parser.py b/pytchat/core_async/parser.py deleted file mode 100644 index 16a7412..0000000 --- a/pytchat/core_async/parser.py +++ /dev/null @@ -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 diff --git a/pytchat/core_multithread/livechat.py b/pytchat/core_multithread/livechat.py index 8357191..95e9514 100644 --- a/pytchat/core_multithread/livechat.py +++ b/pytchat/core_multithread/livechat.py @@ -8,7 +8,7 @@ import traceback import urllib.parse from concurrent.futures import CancelledError, ThreadPoolExecutor from .buffer import Buffer -from .parser import Parser +from ..parser import Parser from .. import config from .. import mylogger from ..exceptions import ChatParseException,IllegalFunctionCall diff --git a/pytchat/core_multithread/replaychat.py b/pytchat/core_multithread/replaychat.py index 4c518c5..3738844 100644 --- a/pytchat/core_multithread/replaychat.py +++ b/pytchat/core_multithread/replaychat.py @@ -182,14 +182,10 @@ class ReplayChat: ) else: self._buffer.put(chat_component) - #次のchatを取得するまでsleepする diff_time = timeout - (time.time()-time_mark) if diff_time < 0 : diff_time=0 time.sleep(diff_time) - #次のチャットデータのcontinuationパラメータを取り出す。 continuation = metadata.get('continuation') - - #whileループ先頭に戻る except ChatParseException as e: logger.error(f"{str(e)}(動画ID:\"{self.video_id}\")") return @@ -253,13 +249,10 @@ class ReplayChat: "既にcallbackを登録済みのため、get()は実行できません。") def pause(self): - '''チャット取得を一時停止する。''' if not self._pauser.empty(): self._pauser.get() - def resume(self): - '''チャット取得を再開する。''' if self._pauser.empty(): self._pauser.put_nowait(None) diff --git a/pytchat/core_multithread/parser.py b/pytchat/parser.py similarity index 95% rename from pytchat/core_multithread/parser.py rename to pytchat/parser.py index c4dada8..a31ec40 100644 --- a/pytchat/core_multithread/parser.py +++ b/pytchat/parser.py @@ -1,7 +1,7 @@ import json -from .. import config -from .. import mylogger -from .. exceptions import ( +from . import config +from . import mylogger +from . exceptions import ( ResponseContextError, NoContentsException, NoContinuationsException )