Add test
This commit is contained in:
@@ -13,12 +13,23 @@ headers = config.headers
|
||||
REPLAY_URL = "https://www.youtube.com/live_chat_replay/" \
|
||||
"get_live_chat_replay?continuation="
|
||||
|
||||
def _divide(start, end, count):
|
||||
min_interval = 120
|
||||
if (not isinstance(start,int) or
|
||||
not isinstance(end,int) or
|
||||
not isinstance(count,int)):
|
||||
raise ValueError("start/end/count must be int")
|
||||
def _split(start, end, count, min_interval = 120):
|
||||
"""
|
||||
Split section from `start` to `end` into `count` pieces,
|
||||
and returns the beginning of each piece.
|
||||
The `count` is adjusted so that the length of each piece
|
||||
is no smaller than `min_interval`.
|
||||
|
||||
Returns:
|
||||
--------
|
||||
List of the beginning position of each piece.
|
||||
"""
|
||||
|
||||
if not (isinstance(start,int) or isinstance(start,float)) or \
|
||||
not (isinstance(end,int) or isinstance(end,float)):
|
||||
raise ValueError("start/end must be int or float")
|
||||
if not isinstance(count,int):
|
||||
raise ValueError("count must be int")
|
||||
if start>end:
|
||||
raise ValueError("end must be equal to or greater than start.")
|
||||
if count<1:
|
||||
@@ -39,7 +50,7 @@ def ready_blocks(video_id, duration, div, callback):
|
||||
async def _get_blocks( video_id, duration, div, callback):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
futures = [_create_block(session, video_id, pos, seektime, callback)
|
||||
for pos, seektime in enumerate(_divide(-1, duration, div))]
|
||||
for pos, seektime in enumerate(_split(-1, duration, div))]
|
||||
return await asyncio.gather(*futures,return_exceptions=True)
|
||||
|
||||
async def _create_block(session, video_id, pos, seektime, callback):
|
||||
|
||||
@@ -3,27 +3,27 @@ class Block:
|
||||
|
||||
Parameter:
|
||||
---------
|
||||
pos : int
|
||||
pos : int :
|
||||
index of this block on block list.
|
||||
|
||||
first : int
|
||||
first : int :
|
||||
videoOffsetTimeMs of chat_data[0]
|
||||
|
||||
last : int
|
||||
videoOffsetTimeMs of the last chat_data current read.
|
||||
last : int :
|
||||
videoOffsetTimeMs of the last chat_data.
|
||||
(chat_data[-1])
|
||||
|
||||
this value increases as fetching chatdata progresses.
|
||||
|
||||
temp_last : int
|
||||
temporary videoOffsetTimeMs of last chat data,
|
||||
temp_last : int :
|
||||
target videoOffsetTimeMs of last chat data for download,
|
||||
equals to first videoOffsetTimeMs of next block.
|
||||
when download worker reaches this offset, the download will stop.
|
||||
when download worker reaches this offset, stop downloading.
|
||||
|
||||
continuation : str
|
||||
continuation : str :
|
||||
continuation param of last chat data.
|
||||
|
||||
chat_data : List
|
||||
chat_data : List
|
||||
"""
|
||||
def __init__(self, pos=0, first=0, last=0,
|
||||
continuation='', chat_data=[]):
|
||||
|
||||
@@ -2,15 +2,15 @@ from . import parser
|
||||
|
||||
class DownloadWorker:
|
||||
"""
|
||||
DownloadWorker : associates a download session with a block.
|
||||
DownloadWorker associates a download session with a block.
|
||||
|
||||
Parameter
|
||||
----------
|
||||
fetch : func
|
||||
fetch :
|
||||
download function of asyncdl
|
||||
|
||||
block : Block
|
||||
chunk of chat_data
|
||||
block :
|
||||
Block object that includes chat_data
|
||||
"""
|
||||
def __init__(self, fetch, block):
|
||||
self.block = block
|
||||
|
||||
@@ -55,8 +55,9 @@ class Downloader:
|
||||
return (
|
||||
self.ready_blocks()
|
||||
.remove_duplicate_head()
|
||||
.set_temporary_last()
|
||||
# .set_temporary_last()
|
||||
.remove_overwrap()
|
||||
.set_temporary_last()
|
||||
.download_blocks()
|
||||
.remove_duplicate_tail()
|
||||
.combine()
|
||||
|
||||
@@ -52,7 +52,6 @@ def duplicate_head(blocks):
|
||||
and
|
||||
type_0 == type_1
|
||||
)
|
||||
|
||||
ret = [blocks[i] for i in range(len(blocks)-1)
|
||||
if (len(blocks[i].chat_data)>0 and
|
||||
not is_duplicate_head(i) )]
|
||||
@@ -75,9 +74,9 @@ def duplicate_tail(blocks):
|
||||
type_0 == type_1
|
||||
)
|
||||
|
||||
ret = [blocks[i] for i in range(0,len(blocks)-1)
|
||||
ret = [blocks[i] for i in range(0,len(blocks))
|
||||
if i == 0 or not is_duplicate_tail(i) ]
|
||||
ret.append(blocks[-1])
|
||||
#ret.append(blocks[-1])
|
||||
return ret
|
||||
|
||||
def overwrap(blocks):
|
||||
@@ -103,3 +102,9 @@ def overwrap(blocks):
|
||||
b = a+1
|
||||
ret.append(blocks[-1])
|
||||
return ret
|
||||
|
||||
def _dump(blocks):
|
||||
print(__name__)
|
||||
print(f"---------- first last temp_last {'':>3}---")
|
||||
for i,block in enumerate(blocks):
|
||||
print(f"block[{i:3}] {block.first:>10} {block.last:>10} {block.temp_last:>10}")
|
||||
Reference in New Issue
Block a user