blivedm

repository·dev·Indexed 23 days ago

https://github.com/xfgryujk/blivedm

A Python library for fetching Bilibili live stream danmaku (bullet chats) via the WebSocket protocol. It supports both the standard web interface and the official Bilibili Live Open Platform, providing a BaseHandler class for processing messages such as danmaku, gifts, and super chats.

Tokens
1K
Snippets
2
Records
8
Agent score
80%

What's inside blivedm

  1. Use blivedm to fetch Bilibili live danmaku

    dev

    The blivedm library allows you to retrieve Bilibili live stream danmaku (bullet chats) using the WebSocket protocol. It supports two different interface methods:

    1. Web Interface: Uses the standard web-based WebSocket protocol.
    2. Bilibili Live Open Platform: Uses the official Bilibili Live Open Platform interface.

    For implementation details regarding the protocol, refer to the Bilibili Open Platform documentation.

    Usage examples are provided in the following files within the repository:

    • Web interface example: sample.py
    • Open Platform interface example: open_live_sample.py
  2. Install blivedm via uv

    dev

    To install blivedm, use uv to pull directly from the GitHub repository. Note that this package is not published to PyPI; installing from PyPI will result in an incorrect version.

    uv add git+https://github.com/xfgryujk/blivedm.git --branch master
    uv add git+https://github.com/xfgryujk/blivedm.git --branch master
  3. Implement a custom message handler using BaseHandler

    dev

    To process live stream messages (danmaku, gifts, etc.), inherit from BaseHandler and override the specific _on_xxx methods corresponding to the message types you want to handle. BaseHandler automatically handles message dispatching and converts raw command dictionaries into structured model objects (from web_models or open_models).

    Supported internal methods to override include:

    • _on_danmaku(client, message): For standard danmaku.
    • _on_gift(client, message): For gift messages.
    • _on_buy_guard(client, message): For 'Guard' (subscription) messages.
    • _on_super_chat(client, message): For super chats (highlighted messages).
    • _on_interact_word_v2(client, message): For interaction messages like entering a room or following a streamer.
    • _on_heartbeat(client, message): For heartbeat signals.

    For Open Platform messages, use the corresponding _on_open_live_xxx methods.

  4. Create a linear retry policy with make_linear_retry_policy

    dev

    Use make_linear_retry_policy(start_interval: float, interval_step: float, max_interval: float) to create a retry policy function where the wait interval increases linearly with each retry attempt.

    • start_interval: The initial wait time for the first retry.
    • interval_step: The amount to increase the interval by for each subsequent retry.
    • max_interval: The maximum allowed wait time.

    The returned function calculates the interval as min(start_interval + (retry_count - 1) * interval_step, max_interval).

  5. Create a constant retry policy with make_constant_retry_policy

    dev
    Use make_constant_retry_policy(interval: float) to create a retry policy function that always returns the same wait interval between retries. The returned function accepts _retry_count and _total_retry_count as arguments and returns the fixed interval.
  6. Reference: Supported message command types and handlers

    dev
    The following table maps the command strings received from the WebSocket to the BaseHandler methods you can override. Note that commands may include parameters separated by :, which are stripped before routing.
  7. HandlerInterface interface definition

    dev

    The HandlerInterface defines the required structure for any live stream message processor. It requires the implementation of handle and provides an optional hook on_client_stopped.

    class HandlerInterface:
        def handle(self, client: ws_base.WebSocketClientBase, command: dict):
            raise NotImplementedError
    
        def on_client_stopped(self, client: ws_base.WebSocketClientBase, exception: Optional[Exception]):
            """Called when the client stops. Can be used to close or restart."""
            pass
  8. BaseHandler class

    dev
    A concrete implementation of HandlerInterface that provides automatic message routing and type conversion. It maps incoming command strings (e.g., DANMU_MSG, SEND_GIFT) to specific protected methods (e.g., _on_danmaku, _on_gift) using an internal callback dictionary. This allows developers to focus on business logic by overriding specific _on_xxx methods rather than parsing raw JSON.