Exemple #1
0
async def main():
    livechat = LiveChatAsync(settings.VIDEO_ID, callback=func)
    while livechat.is_alive():
        await asyncio.sleep(1)
        #other background operation.

    # If you want to check the reason for the termination,
    # you can use `raise_for_status()` function.
    try:
        livechat.raise_for_status()
    except pytchat.ChatDataFinished:
        print("Chat data finished.")
    except Exception as e:
        print(type(e), str(e))
Exemple #2
0
async def main_async():
    global pytchatObjAsync
    pytchatObjAsync = LiveChatAsync(video_id=broadcastId,
                                    callback=pytchat_check)
    while pytchatObjAsync.is_alive():
        #await asyncio.sleep(3)
        await websocket_setup_listen()

    # If you want to check the reason for the termination,
    # you can use `raise_for_status()` function.
    try:
        pytchatObjAsync.raise_for_status()
    except pytchat.ChatDataFinished:
        print("Chat data finished.")
    except Exception as e:
        print(type(e), str(e))
Exemple #3
0
async def workload(vid_id):
    data = []
    vid_path = ROOT.joinpath(vid_id + ".json")

    if vid_path.exists():
        logger.critical("File already exists for stream {}", vid_id)
        return

    logger.debug("task {} started", vid_id)

    async def callback(chat_data: Chatdata):

        logger.debug("Processing Data")

        async for chat in chat_data.async_items():
            chat: Chat
            json_data: dict = json.loads(chat.json())
            logger.debug(f"S:[{json_data['author']['name']}][{json_data['timestamp']}][{json_data['message']}]")
            data.append(json_data)

    try:
        # live_chat = LiveChatAsync(vid_id, callback=callback)
        live_chat = LiveChatAsync(vid_id, callback=callback, force_replay=True, direct_mode=True)
    except Exception:
        raise
    else:
        while live_chat.is_alive():
            await asyncio.sleep(5)

        try:
            live_chat.raise_for_status()
        except ChatDataFinished:
            logger.info("Chat data finished.")
        except Exception:
            raise

    new_dict = {idx: chat_json for idx, chat_json in enumerate(data)}
    vid_path.write_text(json.dumps(new_dict, indent=2), encoding="utf8")

    logger.info("Written {} chats for {}", len(data), vid_id)
class YTchat:
    def __init__(self, ytid, chid, func_send, normal_msg=False,
                 save=False, live=True, chat_folder="chat"):
        # main
        self.livechat = LiveChatAsync(ytid, callback=self.post)
        if chid:
            self.id = str(chid) + "." + ytid
        else:
            self.id = ytid

        # discord channel and post function
        self.chid = str(chid)
        self.send = func_send

        # pytchat parameters
        self.ytid = ytid
        self.normal_msg = normal_msg
        self.live = live

        # save the chat
        self.save = save
        self.folder = chat_folder + "/"
        if save:
            os.makedirs(self.folder, exist_ok=True)

        if not self.is_alive():
            raise ValueError("Is not live")
        logger.info(self.id + " is added")

    def is_alive(self):
        return self.livechat.is_alive()

    async def close(self):
        logger.info(self.id + " to stopped")
        await self.send(f"{self.ytid} is stopped")
        self.livechat.terminate()

    def raise_for_status(self):
        return self.livechat.raise_for_status()

    async def post(self, chatdata):
        for c in chatdata.items:
            if self.save:
                with open(self.folder + self.id + ".data", "a") as f:
                    f.write(c.json() + "\n")

            if c.type != "textMessage" or self.normal_msg:
                logger.debug("post")
                await self.send(c)

            if self.live:
                await chatdata.tick_async()