Пример #1
0
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()
Пример #2
0
class LiveChatProcessor:
    def __init__(self, videoid, msghandler, seektime=0):
        self.videoid = videoid
        self.msghandler = msghandler
        logging.info('videoid:' + videoid + ' seektime:' + str(seektime))
        self.livechat = LiveChatAsync(video_id=videoid,
                                      callback=self.chatProcessor,
                                      seektime=seektime)

    # callback function (automatically called)
    async def chatProcessor(self, chatdata):
        logging.debug(f'[{self.videoid}]chatProcessor')
        for c in chatdata.items:
            # logging.debug('chatdata.items.c\ndatetime:'+c.datetime)
            if c.author.isChatModerator or c.author.isChatOwner:
                logging.info('Chat:' + c.author.name + '\n' + c.message)
                self.sendChat(self.chatRendererToJson(c))
            await chatdata.tick_async()

    def chatRendererToJson(self, c):
        return {
            'type': c.type,
            'message': c.message,
            'timestamp': c.timestamp,
            'datetime': c.datetime,
            'author': {
                'name': c.author.name,
                'channelId': c.author.channelId,
                'imageUrl': c.author.imageUrl,
                'isChatOwner': c.author.isChatOwner,
                'isChatModerator': c.author.isChatModerator
            }
        }

    def sendChat(self, msg):
        self.msghandler.send_message(msg, self.videoid)

    def terminate(self):
        if not self.livechat is None:
            self.livechat.terminate()