Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Fetching Bilibili Live Chat Logs and Public User Profiles

Notes 1

Bilibili provides dedicated HTTP interfaces for extracting broadcast chat history and public account metadata. The primary endpoints operate via standard GET requests and return structured JSON payloads.

Live Room Chat History Interface

This endpoint retrieves recent comments and system messages from an active stream.

Request Configuraton

  • Base URL: https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory
  • Method: GET
  • Query Parameters:
    • roomid: Integer identifier for the live room.
    • room_type: Integer type (typically 0 for standard rooms).

Response Payload Structure A successful request returns a JSON object where the data.room array contains individual message objects. Key attributes include the message content, sender identifier, username, and timestamp.

{
  "status_code": 0,
  "status_msg": "success",
  "payload": {
    "moderator_messages": [],
    "chat_log": [
      {
        "message_content": "Locating the track named Green Melody",
        "message_type": 0,
        "sender_id": 366087710,
        "display_name": "ben_net",
        "display_color": null,
        "timestamp_str": "2023-09-16 23:23:15",
        "is_admin": false,
        "vip_status": 0,
        "guard_tier": 0,
        "fan_medal": [13],
        "user_tier": [0, 0, 9868950, ">50000"],
        "server_rnd": "891587222",
        "bubble_config": 0,
        "verification_token": {
          "issued_at": 1694877795,
          "hash": "3D11739F"
        },
        "voice_payload": {
          "audio_uri": "",
          "format_type": "",
          "transcript": "",
          "duration_sec": 0,
          "asset_id": ""
        },
        "emoji_data": {
          "asset_id": 0,
          "unique_slug": "",
          "label": "",
          "permission_flag": 0,
          "render_url": "",
          "is_overlay": false,
          "display_mode": 0,
          "is_animated": false,
          "dimensions": {"h": 0, "w": 0}
        },
        "msg_uuid": "74a6a08b91717847d8325eea06505c815",
        "wealth_tier": 0
      }
    ]
  }
}

Public User Profile Interface

Account metadata, including registration details, verification status, and subscription information, is accessible via the space API. Note that modern requests require WBI signature validation.

Request Configuraton

  • Base URL: https://api.bilibili.com/x/space/wbi/acc/info
  • Method: GET
  • Query Parametres:
    • mid: Numeric user identifier.
    • w_rid: WBI signature string.
    • wts: Timestamp used for signature generation.
    • platform: Client type (usually web).

Response Payload Structure The response wraps profile details inside a data object. It contains identifiers, media assets, level progression, and VIP configurations.

{
  "response_code": 0,
  "response_message": "0",
  "cache_ttl": 1,
  "profile_data": {
    "user_id": 35112364,
    "username": "Xi_Jian_Chan",
    "gender": "private",
    "avatar_uri": "https://i1.hdslb.com/bfs/face/ac256f82809b9a06a88b8bde9bbf54137932df58.jpg",
    "nft_avatar_flag": 0,
    "bio_text": "Daily Bilibili browsing",
    "reputation_score": 10000,
    "account_level": 6,
    "registration_timestamp": 0,
    "morality_points": 0,
    "ban_status": 0,
    "coin_balance": 0,
    "badge_visibility": {
      "display_enabled": false,
      "currently_worn": false,
      "medal_details": null
    },
    "official_certification": {
      "role_type": 0,
      "cert_title": "",
      "description": "",
      "cert_category": -1
    },
    "premium_status": {
      "tier": 1,
      "active": false,
      "expiry_timestamp": 1544630400000,
      "payment_method": 0,
      "theme_variant": 0,
      "label_config": {
        "badge_uri_static": "https://i0.hdslb.com/bfs/vip/d7b702ef65a976b20ed854cbd04cb9e27341bb79.png"
      },
      "avatar_border": 0,
      "tv_premium_active": false
    },
    "decoration": {
      "frame_id": 0,
      "frame_name": "",
      "frame_uri": "",
      "expiry": 0,
      "enhanced_uri": "",
      "frame_enhanced_uri": ""
    },
    "nameplate": {
      "plate_id": 0,
      "plate_name": "",
      "plate_uri": "",
      "plate_uri_small": "",
      "plate_tier": "",
      "unlock_condition": ""
    },
    "follow_status": false,
    "banner_image": "http://i1.hdslb.com/bfs/space/cb1c3ef50e22b6096fde67febe863494caefebad.png",
    "birth_date": "11-03",
    "educational_background": null,
    "professional_info": {
      "industry": "",
      "division": "",
      "position": "",
      "public_visible": 0
    },
    "account_series": {
      "upgrade_progress": 3,
      "upgrade_prompt_active": false
    },
    "senior_member_flag": 0,
    "risk_assessment": false,
    "energy_badge": {
      "display_config": {
        "visible": false,
        "state_flag": -1
      }
    },
    "contract_agreement": {
      "visible": false,
      "follow_visible": false
    }
  }
}

Integration Example

The following Python implementation demonstrates how to query both endpoints, normalize the variable names, and extract the relevant data fields.

import requests
from typing import Dict, List

def fetch_live_chat(room_identifier: int, room_category: int = 0) -> List[Dict]:
    endpoint = "https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory"
    query_payload = {"roomid": room_identifier, "room_type": room_category}

    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
    response = requests.get(endpoint, params=query_payload, headers=headers)
    response.raise_for_status()

    data_block = response.json().get("data", {})
    raw_messages = data_block.get("room", [])

    normalized_log = []
    for entry in raw_messages:
        normalized_log.append({
            "content": entry.get("text"),
            "author_uid": entry.get("uid"),
            "author_name": entry.get("nickname"),
            "post_time": entry.get("timeline"),
            "sender_level": entry.get("user_level", [0, 0, 0, ""])[3],
            "is_guard": bool(entry.get("guard_level", 0))
        })
    return normalized_log

def retrieve_user_profile(user_mid: int, wbi_signature: str, timestamp: int) -> Dict:
    endpoint = "https://api.bilibili.com/x/space/wbi/acc/info"
    query_payload = {
        "mid": user_mid,
        "platform": "web",
        "web_location": 1550101,
        "w_rid": wbi_signature,
        "wts": timestamp
    }

    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
    response = requests.get(endpoint, params=query_payload, headers=headers)
    response.raise_for_status()

    profile_data = response.json().get("data", {})
    return {
        "uid": profile_data.get("mid"),
        "handle": profile_data.get("name"),
        "avatar_link": profile_data.get("face"),
        "biography": profile_data.get("sign"),
        "tier": profile_data.get("level"),
        "vip_expires_at": profile_data.get("vip", {}).get("due_date"),
        "follower_badge_active": profile_data.get("fans_medal", {}).get("wear", False)
    }

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.