From eaa7bdc8b69a08f942b8e75745f9238b05bcf8ef Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Mon, 3 Feb 2020 22:01:54 +0900 Subject: [PATCH] Deligate processing extra chat data to Block class --- pytchat/tool/block.py | 22 +++++++++++++++++++++- pytchat/tool/dlworker.py | 24 +++++------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pytchat/tool/block.py b/pytchat/tool/block.py index 14b8ece..b4da7e6 100644 --- a/pytchat/tool/block.py +++ b/pytchat/tool/block.py @@ -1,3 +1,4 @@ +from . import parser class Block: """Block object represents virtual chunk of chatdata. @@ -33,4 +34,23 @@ class Block: self.last = last self.temp_last = 0 self.continuation = continuation - self.chat_data = chat_data \ No newline at end of file + self.chat_data = chat_data + + def start(self): + chats, cont = self._cut(self.chat_data, self.continuation, self.last) + self.chat_data = chats + return cont + + def fill(self, chats, cont, fetched_last): + chats, cont = self._cut(chats, cont, fetched_last) + self.chat_data.extend(chats) + return cont + + def _cut(self, chats, cont, fetched_last): + if fetched_last < self.temp_last or self.temp_last == -1: + return chats, cont + for i, line in enumerate(chats): + line_offset = parser.get_offset(line) + if line_offset >= self.temp_last: + self.last = line_offset + return chats[:i], None diff --git a/pytchat/tool/dlworker.py b/pytchat/tool/dlworker.py index 8e94ba0..dea0c17 100644 --- a/pytchat/tool/dlworker.py +++ b/pytchat/tool/dlworker.py @@ -15,27 +15,13 @@ class DownloadWorker: def __init__(self, fetch, block): self.block = block self.fetch = fetch - + async def run(self, session): """Remove extra chats just after ready_blocks(). """ - temp_last = self.block.temp_last - self.block.chat_data, continuation = self.cut( - self.block.chat_data, - self.block.continuation, - self.block.last, - temp_last ) + continuation = self.block.start() """download loop """ while continuation: - data, cont, fetched_last = await self.fetch(continuation, session) - data, continuation = self.cut(data, cont, fetched_last, temp_last) - self.block.chat_data.extend(data) + chats, new_cont, fetched_last = await self.fetch(continuation, session) + continuation = self.block.fill(chats, new_cont, fetched_last ) + - def cut(self, data, cont, fetched_last, temp_last): - """Remove extra chats.""" - if fetched_last < temp_last or temp_last == -1: - return data, cont - for i, line in enumerate(data): - line_offset = parser.get_offset(line) - if line_offset >= temp_last: - self.block.last = line_offset - return data[:i], None \ No newline at end of file