pytchat

repository·master·Indexed 18 days ago

https://github.com/taizan-hokuto/pytchat

A Python library for fetching YouTube live chat data without using Selenium or BeautifulSoup. It supports asyncio, customizable data processors, and provides functionality to retrieve chat items in JSON format or via the CompatibleProcessor for YouTube API compatibility.

Tokens
775
Snippets
3
Records
6
Agent score
13%

What's inside pytchat

  1. Advanced usage: Async, Buffering, and YT API compatibility

    master

    Pytchat supports several advanced patterns:

    • Asyncio context: Use LiveChatAsync for asynchronous workflows.
    • Buffering: Use LiveChat to fetch chat with a buffer.
    • YouTube API compatibility: Use CompatibleProcessor to get chat data in a format compatible with the official YouTube API.
  2. Output chat data in JSON format

    master

    You can retrieve the entire batch of fetched chat items as a JSON string using chat.get().json(). Alternatively, you can iterate through individual chat items and call .json() on each item object.

    import pytchat
    import time
    
    chat = pytchat.create(video_id="uIx8l2xlYVY")
    while chat.is_alive():
        # Get all items in the current batch as a JSON string
        print(chat.get().json())
        
        # OR: Get individual items as JSON strings
        # for c in chat.get().items:
        #     print(c.json())
        
        time.sleep(5)
  3. Fetch YouTube live chat data

    master

    Use pytchat.create(video_id=...) to initialize a chat session. You can then iterate through chat items using chat.get().sync_items() while the session is active via chat.is_alive().

    import pytchat
    chat = pytchat.create(video_id="uIx8l2xlYVY")
    while chat.is_alive():
        for c in chat.get().sync_items():
            print(f"{c.datetime} [{c.author.name}]- {c.message}")
  4. Reference the Default Processor chat item structure

    master

    When using sync_items(), each chat item contains the following fields:

    NameTypeRemarks
    typestr"superChat", "textMessage", "superSticker", "newSponsor"
    idstr
    messagestrEmojis are represented by ":(shortcut text):"
    messageExstrList of message texts and emoji dicts (id, txt, url)
    timestampintUnix time in milliseconds
    datetimestre.g. "2019-10-10 12:34:56"
    elapsedTimestrElapsed time (e.g. "1:02:27"). *Replay Only
    amountValuefloate.g. 1,234.0
    amountStringstre.g. "$ 1,234"
    currencystrISO 4217 currency codes (e.g. "USD")
    bgColorintRGB Int
    authorobjectSee Author object structure below
  5. Reference the Author object structure

    master

    The author field in a chat item is an object containing the following fields:

    NameTypeRemarks
    namestr
    channelIdstrChatter's channel ID
    channelUrlstr
    imageUrlstr
    badgeUrlstr
    isVerifiedbool
    isChatOwnerbool
    isChatSponsorbool
    isChatModeratorbool