Move extract method into class
This commit is contained in:
@@ -9,19 +9,26 @@ logger = config.logger(__name__)
|
|||||||
headers=config.headers
|
headers=config.headers
|
||||||
|
|
||||||
class Extractor:
|
class Extractor:
|
||||||
def __init__(self, video_id, duration, div, callback):
|
def __init__(self, video_id, div, callback = None, processor = None):
|
||||||
if not isinstance(div ,int) or div < 1:
|
if not isinstance(div ,int) or div < 1:
|
||||||
raise ValueError('div must be positive integer.')
|
raise ValueError('div must be positive integer.')
|
||||||
elif div > 10:
|
elif div > 10:
|
||||||
div = 10
|
div = 10
|
||||||
if not isinstance(duration ,int) or duration < 1:
|
|
||||||
raise ValueError('duration must be positive integer.')
|
|
||||||
self.video_id = video_id
|
self.video_id = video_id
|
||||||
self.duration = duration
|
|
||||||
self.div = div
|
self.div = div
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
|
self.processor = processor
|
||||||
|
self.duration = self._get_duration_of_video(video_id)
|
||||||
self.blocks = []
|
self.blocks = []
|
||||||
|
|
||||||
|
def _get_duration_of_video(self, video_id):
|
||||||
|
duration = 0
|
||||||
|
try:
|
||||||
|
duration = VideoInfo(video_id).get("duration")
|
||||||
|
except InvalidVideoIdException:
|
||||||
|
raise
|
||||||
|
return duration
|
||||||
|
|
||||||
def _ready_blocks(self):
|
def _ready_blocks(self):
|
||||||
blocks = asyncdl.ready_blocks(
|
blocks = asyncdl.ready_blocks(
|
||||||
self.video_id, self.duration, self.div, self.callback)
|
self.video_id, self.duration, self.div, self.callback)
|
||||||
@@ -57,7 +64,7 @@ class Extractor:
|
|||||||
ret.extend(block.chat_data)
|
ret.extend(block.chat_data)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def extract(self):
|
def _execute_extract_operations(self):
|
||||||
return (
|
return (
|
||||||
self._ready_blocks()
|
self._ready_blocks()
|
||||||
._remove_duplicate_head()
|
._remove_duplicate_head()
|
||||||
@@ -68,22 +75,17 @@ class Extractor:
|
|||||||
._combine()
|
._combine()
|
||||||
)
|
)
|
||||||
|
|
||||||
def extract(video_id, div = 1, callback = None, processor = None):
|
def extract(self):
|
||||||
duration = 0
|
if self.duration == 0:
|
||||||
try:
|
print("video is not archived.")
|
||||||
duration = VideoInfo(video_id).get("duration")
|
return []
|
||||||
except InvalidVideoIdException:
|
data = self._execute_extract_operations()
|
||||||
raise
|
if self.processor is None:
|
||||||
if duration == 0:
|
return data
|
||||||
print("video is live.")
|
return self.processor.process(
|
||||||
return []
|
[{'video_id':None,'timeout':1,'chatdata' : (action
|
||||||
data = Extractor(video_id, duration, div, callback).extract()
|
["replayChatItemAction"]["actions"][0] for action in data)}]
|
||||||
if processor is None:
|
|
||||||
return data
|
|
||||||
return processor.process(
|
|
||||||
[{'video_id':None,'timeout':1,'chatdata' : (action
|
|
||||||
["replayChatItemAction"]["actions"][0] for action in data)}]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def cancel():
|
def cancel(self):
|
||||||
asyncdl.cancel()
|
asyncdl.cancel()
|
||||||
@@ -9,6 +9,7 @@ from pytchat.tool.extract import parser
|
|||||||
from pytchat.tool.extract.block import Block
|
from pytchat.tool.extract.block import Block
|
||||||
from pytchat.tool.extract.patch import Patch, fill, split, set_patch
|
from pytchat.tool.extract.patch import Patch, fill, split, set_patch
|
||||||
from pytchat.tool.extract.duplcheck import _dump
|
from pytchat.tool.extract.duplcheck import _dump
|
||||||
|
|
||||||
def _open_file(path):
|
def _open_file(path):
|
||||||
with open(path,mode ='r',encoding = 'utf-8') as f:
|
with open(path,mode ='r',encoding = 'utf-8') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from pytchat.exceptions import (
|
|||||||
NoLivechatRendererException,NoYtinitialdataException,
|
NoLivechatRendererException,NoYtinitialdataException,
|
||||||
ResponseContextError, NoContentsException)
|
ResponseContextError, NoContentsException)
|
||||||
|
|
||||||
from pytchat.processors.speed_calculator import SpeedCalculator
|
from pytchat.processors.speed.calculator import SpeedCalculator
|
||||||
|
|
||||||
parser = Parser(is_replay =False)
|
parser = Parser(is_replay =False)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user