diff --git a/pytchat/processors/speed_calculator.py b/pytchat/processors/speed_calculator.py index 361b32f..ff15517 100644 --- a/pytchat/processors/speed_calculator.py +++ b/pytchat/processors/speed_calculator.py @@ -21,7 +21,7 @@ class RingQueue: キュー内に余裕があるか。キュー内のアイテム個数が、キューの最大個数未満であればTrue。 """ - def __init__(self, capacity = 10): + def __init__(self, capacity): """ コンストラクタ @@ -82,29 +82,29 @@ class SpeedCalculator(ChatProcessor, RingQueue): チャットの勢いを計算するクラス Parameter ---------- + capacity : int 格納するチャットブロックの数 """ - def __init__(self, capacity, video_id): + def __init__(self, capacity = 10): super().__init__(capacity) - self.video_id=video_id self.speed = 0 def process(self, chat_components: list): + chatdata = [] if chat_components: for component in chat_components: - - chatdata = component.get('chatdata') - - if chatdata is None: - return self.speed - self.speed = self.calc(chatdata) - return self.speed + if component.get("chatdata"): + chatdata.extend(component.get("chatdata")) - def _value(self): - + self._put_chatdata(chatdata) + self.speed = self._calc_speed() + return self.speed + + + def _calc_speed(self): """ - ActionsQueue内のチャットデータリストから、 + RingQueue内のチャットデータリストから、 チャット速度を計算して返す Return @@ -123,24 +123,19 @@ class SpeedCalculator(ChatProcessor, RingQueue): except IndexError: return 0 - def _get_timestamp(self, action :dict): + def _put_chatdata(self,actions): """ - チャットデータのtimestampUsecを読み取る - liveChatTickerSponsorItemRenderer等のtickerデータは時刻格納位置が - 異なるため、時刻データなしとして扱う + チャットデータからタイムスタンプを読み取り、RingQueueに投入する。 + Parameter + --------- + actions : List[dict] + チャットデータ(addChatItemAction) のリスト """ - try: - item = action['addChatItemAction']['item'] - timestamp = int(item[list(item.keys())[0]]['timestampUsec']) - except (KeyError,TypeError): - return None - return timestamp - - def calc(self,actions): - - def empty_data(): + def _put_emptydata(): ''' - データがない場合にゼロのデータをリングキューに入れる + enqueue empty data when no chat data. + Return: int + speed value after enqueueing empty data ''' timestamp_now = calendar.timegm(datetime.datetime. now(pytz.utc).utctimetuple()) @@ -149,10 +144,21 @@ class SpeedCalculator(ChatProcessor, RingQueue): 'starttime':int(timestamp_now), 'endtime':int(timestamp_now) }) - return self._value() + + def _get_timestamp(action :dict): + """ + チャットデータのtimestampUsecを読み取る + """ + try: + item = action['addChatItemAction']['item'] + timestamp = int(item[list(item.keys())[0]]['timestampUsec']) + except (KeyError,TypeError): + return None + return timestamp if actions is None or len(actions)==0: - return empty_data + _put_emptydata() + return #actions内の時刻データを持つチャットデータの数(tickerは除く) counter=0 @@ -163,7 +169,7 @@ class SpeedCalculator(ChatProcessor, RingQueue): for action in actions: #チャットデータからtimestampUsecを読み取る - gettime = self._get_timestamp(action) + gettime = _get_timestamp(action) #時刻のないデータだった場合は次の行のデータで読み取り試行 if gettime is None: @@ -177,11 +183,12 @@ class SpeedCalculator(ChatProcessor, RingQueue): endtime = gettime #チャットの数をインクリメント - counter+=1 + counter += 1 #チャット速度用のデータをリングキューに送る if starttime is None or endtime is None: - return empty_data + _put_emptydata() + return self.put({ 'chat_count':counter, @@ -189,4 +196,3 @@ class SpeedCalculator(ChatProcessor, RingQueue): 'endtime':int(endtime/1000000) }) - return self._value() diff --git a/tests/test_speed_calculator.py b/tests/test_speed_calculator.py index 69a902c..b5964d6 100644 --- a/tests/test_speed_calculator.py +++ b/tests/test_speed_calculator.py @@ -12,14 +12,14 @@ from pytchat.processors.speed_calculator import SpeedCalculator parser = Parser() def test_speed_1(mocker): - '''test speed normal + '''test speed calculation with normal json. test json has 15 chatdata, duration is 30 seconds, so the speed of chatdata is 30 chats/minute. ''' - processor = SpeedCalculator(capacity=30,video_id="") + processor = SpeedCalculator(capacity=30) - _json = _open_file("tests/testdata/speed/speedtest1.json") + _json = _open_file("tests/testdata/speed/speedtest_normal.json") _, chatdata = parser.parse(json.loads(_json)) data = { @@ -31,13 +31,27 @@ def test_speed_1(mocker): assert 30 == ret def test_speed_2(mocker): - '''test speed with no valid chat data - + '''test speed calculation with no valid chat data. ''' + processor = SpeedCalculator(capacity=30) - processor = SpeedCalculator(capacity=30,video_id="") + _json = _open_file("tests/testdata/speed/speedtest_undefined.json") - _json = _open_file("tests/testdata/speed/speedtest_none.json") + _, chatdata = parser.parse(json.loads(_json)) + data = { + "video_id" : "", + "timeout" : 10, + "chatdata" : chatdata + } + ret = processor.process([data]) + assert 0 == ret + +def test_speed_3(mocker): + '''test speed calculation with empty data. + ''' + processor = SpeedCalculator(capacity=30) + + _json = _open_file("tests/testdata/speed/speedtest_empty.json") _, chatdata = parser.parse(json.loads(_json)) data = { @@ -52,3 +66,4 @@ def test_speed_2(mocker): def _open_file(path): with open(path,mode ='r',encoding = 'utf-8') as f: return f.read() +_put_chatdata \ No newline at end of file diff --git a/tests/testdata/speed/speedtest_empty.json b/tests/testdata/speed/speedtest_empty.json new file mode 100644 index 0000000..b116a66 --- /dev/null +++ b/tests/testdata/speed/speedtest_empty.json @@ -0,0 +1,24 @@ +{ + "timing": { + "info": { + "st": 164 + } + }, + "csn": "", + "response": { + "responseContext": { + }, + "continuationContents": { + "liveChatContinuation": { + "continuations": [{ + "timedContinuationData": { + "timeoutMs": 10000, + "continuation": "continuation" + } + }] + } + } + } +} + + diff --git a/tests/testdata/speed/speedtest1.json b/tests/testdata/speed/speedtest_normal.json similarity index 100% rename from tests/testdata/speed/speedtest1.json rename to tests/testdata/speed/speedtest_normal.json diff --git a/tests/testdata/speed/speedtest_none.json b/tests/testdata/speed/speedtest_undefined.json similarity index 100% rename from tests/testdata/speed/speedtest_none.json rename to tests/testdata/speed/speedtest_undefined.json