Last active
August 31, 2023 06:34
-
-
Save doxy-ai/f567236fd6320e721cbd127b11ca7cb0 to your computer and use it in GitHub Desktop.
Youtube Zatsudachi Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requirements: pip install git+https://github.com/KaitoCross/pytchat.git@developing | |
# Importing required packages | |
from api import PluginBase, Message, Color | |
from flask import escape | |
from pytchat import LiveChatAsync, ChatDataFinished | |
import asyncio | |
class Plugin(PluginBase): | |
""" | |
Plugin class for interacting with YouTube livestream chat. | |
""" | |
# Set plugin name and target stream | |
name = "Youtube Chat Plugin" | |
# Set image URL for the plugin | |
pluginImageURL = r"https://upload.wikimedia.org/wikipedia/commons/0/09/YouTube_full-color_icon_%282017%29.svg" | |
# CONNECTION NOTE: Paste the id of your livestream here! | |
targetStream = "LPUGzHtVvWY" | |
async def go(self): | |
""" | |
Entry point method for the plugin. | |
Starts an asynchronous connection to the YouTube livestream chat and processes messages. | |
""" | |
# Create a LiveChatAsync object with the targetStream ID and specify the callback function | |
# interruptable=False ensures the program doesn't exit when the chat data is finished | |
livechat = LiveChatAsync(self.targetStream, callback=self.on_messages, interruptable=False) | |
# Wait for a short period of time before checking if the live chat is still active | |
while livechat.is_alive(): | |
await asyncio.sleep(.1) | |
async def on_messages(self, chatdata): | |
""" | |
Callback method for handling chat messages. | |
Iterates through chat messages and calls the `on_message` method for each message. | |
Args: | |
chatdata (ChatDataFinished): Object containing chat messages and associated data. | |
""" | |
# Iterate through each chat message in the chatdata and call the on_message function for each message | |
for chat in chatdata.items: | |
self.on_message(chat) | |
await chatdata.tick_async() | |
def on_message(self, chat): | |
""" | |
Method for processing each chat message. | |
Args: | |
chat (Chat): Object representing a single chat message. | |
""" | |
# Initialize an empty string for the message content | |
msg = "" | |
for node in chat.messageEx: | |
# If the node is a string, append it to the message content | |
if isinstance(node, str): | |
msg += node | |
# If the node is not a string, register the emote and append its text to the message content | |
else: | |
self.register_emote(node["txt"], node["url"]) | |
msg += node["txt"] | |
# Create a Message object with the constructed content, sender information, color, and plugin image URL | |
self.recieve_message(Message(content=escape(msg), senderColor=Color("white"), sender=chat.author.name, pluginImageUrl=self.pluginImageURL)) | |
print(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment