Implement callback parameter

This commit is contained in:
taizan-hokuto
2020-01-26 13:49:42 +09:00
parent 540f16c1a0
commit 03b901d59c

View File

@@ -25,11 +25,12 @@ class Block:
self.chat_data = chat_data self.chat_data = chat_data
class Downloader: class Downloader:
def __init__(self, video_id,duration,div): def __init__(self, video_id, duration, div, callback=None):
self.video_id = video_id self.video_id = video_id
self.duration = duration self.duration = duration
self.div = div self.div = div
self.blocks = [] self.blocks = []
self.callback = callback
def ready_blocks(self): def ready_blocks(self):
@@ -58,26 +59,30 @@ class Downloader:
async def _get_blocks(duration,div): async def _get_blocks(duration,div):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
futures = [_create_block(session, pos, seektime) futures = [_create_block(session, pos, seektime)
for pos, seektime in enumerate(_divide(-1, duration , div))] for pos, seektime in enumerate(_divide(-1, duration, div))]
return await asyncio.gather(*futures,return_exceptions=True) return await asyncio.gather(*futures,return_exceptions=True)
async def _create_block(session, pos, seektime): async def _create_block(session, pos, seektime):
continuation = arcparam.getparam(self.video_id, seektime = seektime) continuation = arcparam.getparam(
self.video_id, seektime = seektime)
url = f"{REPLAY_URL}{quote(continuation)}&pbj=1" url = f"{REPLAY_URL}{quote(continuation)}&pbj=1"
async with session.get(url, headers = headers) as resp: async with session.get(url, headers = headers) as resp:
text = await resp.text() text = await resp.text()
next_continuation, actions = parser.parse(json.loads(text)) next_continuation, actions = parser.parse(json.loads(text))
first = parser.get_offset(actions[0])
last = parser.get_offset(actions[-1])
if self.callback:
self.callback(actions,last-first)
return Block( return Block(
pos = pos, pos = pos,
continuation = next_continuation, continuation = next_continuation,
chat_data = actions, chat_data = actions,
first = parser.get_offset(actions[0]), first = first,
last = parser.get_offset(actions[-1]) last = last
) )
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
self.blocks = loop.run_until_complete(_get_blocks(self.duration, self.div)) self.blocks = loop.run_until_complete(
_get_blocks(self.duration, self.div))
return self return self
def remove_duplicate_head(self): def remove_duplicate_head(self):
@@ -104,7 +109,6 @@ class Downloader:
self.blocks = ret self.blocks = ret
return self return self
def set_temporary_last(self): def set_temporary_last(self):
for i in range(len(self.blocks)-1): for i in range(len(self.blocks)-1):
self.blocks[i].temp_last = self.blocks[i+1].first self.blocks[i].temp_last = self.blocks[i+1].first
@@ -172,6 +176,9 @@ class Downloader:
if actions: if actions:
block.chat_data.extend(actions) block.chat_data.extend(actions)
last = get_last_offset(actions) last = get_last_offset(actions)
first = parser.get_offset(actions[0])
if self.callback:
self.callback(actions,last-first)
if block.temp_last != -1: if block.temp_last != -1:
if last > block.temp_last: if last > block.temp_last:
block.last = last block.last = last