async def test_loop(*mock): pattern = re.compile( r'^https://www.youtube.com/live_chat/get_live_chat\?continuation=.*$' ) _text = _open_file('tests/testdata/test_stream.json') mock[0].get(pattern, status=200, body=_text) chat = LiveChatAsync(video_id='__test_id__', processor=DummyProcessor()) chats = await chat.get() rawdata = chats[0]["chatdata"] # assert fetching livachat data assert list(rawdata[0]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[1]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[2]["addChatItemAction"] ["item"].keys())[0] == "liveChatPlaceholderItemRenderer" assert list( rawdata[3]["addLiveChatTickerItemAction"] ["item"].keys())[0] == "liveChatTickerPaidMessageItemRenderer" assert list(rawdata[4]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer" assert list(rawdata[5]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidStickerRenderer" assert list(rawdata[6]["addLiveChatTickerItemAction"] ["item"].keys())[0] == "liveChatTickerSponsorItemRenderer"
async def test_loop(*mock): pattern_live = re.compile( r'^https://www.youtube.com/live_chat/get_live_chat\?continuation=.*$' ) pattern_replay = re.compile( r'^https://www.youtube.com/live_chat_replay/get_live_chat_replay\?continuation=.*$' ) # valid live data, but force_replay = True _text_live = _open_file('tests/testdata/test_stream.json') # valid replay data _text_replay = _open_file('tests/testdata/chatreplay.json') mock[0].get(pattern_live, status=200, body=_text_live) mock[0].get(pattern_replay, status=200, body=_text_replay) # force replay chat = LiveChatAsync(video_id='__test_id__', processor=DummyProcessor(), force_replay=True) chats = await chat.get() rawdata = chats[0]["chatdata"] # assert fetching replaychat data assert list(rawdata[14]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer" # assert not mix livechat data assert list(rawdata[2]["addChatItemAction"] ["item"].keys())[0] != "liveChatPlaceholderItemRenderer"
async def background_task(self, message): try: channel = discord.utils.get( message.guild.channels, name="bot-for-questions") superchatchannel = discord.utils.get( message.guild.channels, name="bot-for-superchats") url = re.search( "(?P<url>https?://[^\s]+)", message.content).group("url") urlstr = url.split("=", 1) urlstr = urlstr[1].split("&", 1) await channel.send(f'Parsing chat for <{url}>') livechat = LiveChatAsync(urlstr[0], callback=( lambda chatdata: self.checkmessagesfunc(chatdata, message))) while livechat.is_alive(): await asyncio.sleep(1) #await channel.purge(limit=9000, bulk=False) await channel.send(f'Done parsing chat for <{url}> so channel cleared!') #await superchatchannel.purge(limit=9000, bulk=False) await superchatchannel.send(f'Done parsing chat for <{url}> so channel cleared!') except Exception as e: traceback.print_exc() await channel.send(e)
async def test_loop(): try: chat = LiveChatAsync(video_id='__test_id__') _ = await chat.get() assert chat.is_alive() chat.terminate() assert not chat.is_alive() except ResponseContextError: assert False
async def test_loop(): chat = LiveChatAsync(video_id='__test_id__', processor=DummyProcessor()) chats = await chat.get() rawdata = chats[0]["chatdata"] # assert fetching replaychat data assert list(rawdata[0]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[14]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer"
async def test_loop(): chat = LiveChatAsync(video_id='__test_id__', processor=DummyProcessor(), force_replay=True) chats = await chat.get() rawdata = chats[0]["chatdata"] # assert fetching replaychat data assert list(rawdata[14]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer" # assert not mix livechat data assert list(rawdata[2]["addChatItemAction"] ["item"].keys())[0] != "liveChatPlaceholderItemRenderer"
def test_Async(*mock): vid = '__test_id__' _text = _open_file('tests/testdata/paramgen_firstread.json') _text = json.loads(_text) mock[0].get(f"https://www.youtube.com/live_chat?v={vid}&is_popout=1", status=200, body=_text) try: chat = LiveChatAsync(video_id='__test_id__') assert chat.is_alive() chat.terminate() assert not chat.is_alive() except ResponseContextError: assert not chat.is_alive()
def test_MultiThread(mocker): _text = _open_file('tests/testdata/paramgen_firstread.json') _text = json.loads(_text) responseMock = mocker.Mock() responseMock.status_code = 200 responseMock.text = _text mocker.patch('requests.Session.get').return_value = responseMock try: chat = LiveChatAsync(video_id='__test_id__') assert chat.is_alive() chat.terminate() assert not chat.is_alive() except ResponseContextError: chat.terminate() assert not chat.is_alive()
async def test_loop(): for case in cases: stream = LiveChatAsync(video_id=case["video_id"], seektime=case["seektime"]) while stream.is_alive(): chat = await stream.get() agg1 = {} agg2 = {} for c in chat.items: if c.type in agg1: agg1[c.type] += 1 else: agg1[c.type] = 1 if c.author.type in agg2: agg2[c.author.type] += 1 else: agg2[c.author.type] = 1 break assert agg1 == case["result1"] assert agg2 == case["result2"]
async def test_loop(): chat = LiveChatAsync(video_id='__test_id__', processor=DummyProcessor()) chats = await chat.get() rawdata = chats[0]["chatdata"] assert list(rawdata[0]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[1]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[2]["addChatItemAction"] ["item"].keys())[0] == "liveChatPlaceholderItemRenderer" assert list( rawdata[3]["addLiveChatTickerItemAction"] ["item"].keys())[0] == "liveChatTickerPaidMessageItemRenderer" assert list(rawdata[4]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer" assert list(rawdata[5]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidStickerRenderer" assert list(rawdata[6]["addLiveChatTickerItemAction"] ["item"].keys())[0] == "liveChatTickerSponsorItemRenderer"
async def test_loop(*mock): pattern_live = re.compile( r'^https://www.youtube.com/live_chat/get_live_chat\?continuation=.*$' ) pattern_replay = re.compile( r'^https://www.youtube.com/live_chat_replay/get_live_chat_replay\?continuation=.*$' ) # empty livechat -> switch to fetch replaychat _text_live = _open_file('tests/testdata/finished_live.json') _text_replay = _open_file('tests/testdata/chatreplay.json') mock[0].get(pattern_live, status=200, body=_text_live) mock[0].get(pattern_replay, status=200, body=_text_replay) chat = LiveChatAsync(video_id='', processor=DummyProcessor()) chats = await chat.get() rawdata = chats[0]["chatdata"] # assert fetching replaychat data assert list(rawdata[0]["addChatItemAction"] ["item"].keys())[0] == "liveChatTextMessageRenderer" assert list(rawdata[14]["addChatItemAction"] ["item"].keys())[0] == "liveChatPaidMessageRenderer"