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
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 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(): 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"]