Metadata-Version: 2.4
Name: dyproto
Version: 0.1.1
Summary: Douyu Live Stream Protocol - minimal library for encoding/decoding danmu messages.
Author-email: Douyu Danmu Crawler Project <contact@example.com>
License: MIT
Project-URL: Homepage, https://github.com/Joxos/dyproto
Project-URL: Repository, https://github.com/Joxos/dyproto
Keywords: douyu,danmu,chat,websocket,protocol
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: construct>=2.10.70
Provides-Extra: discovery
Requires-Dist: httpx>=0.28.1; extra == "discovery"
Requires-Dist: beautifulsoup4>=4.14.3; extra == "discovery"
Requires-Dist: tenacity>=9.1.2; extra == "discovery"

# dyproto

Douyu Live Stream Protocol - minimal library for encoding/decoding danmu messages.

## Installation

```bash
# Core (encoding/decoding only)
pip install dyproto

# With room discovery
pip install dyproto[discovery]
```

## Quick Start

```python
from dyproto import pack, unpack, MessageBuffer

# Encode a message to bytes
data = pack({"type": "chatmsg", "content": "Hello"})

# Decode bytes to message
msg = unpack(data)  # {"type": "chatmsg", "content": "Hello"}

# For streaming, use MessageBuffer
buffer = MessageBuffer()
buffer.add_data(raw_bytes)
for msg_dict in buffer.get_messages():
    process(msg_dict)
```

## API

### Core Functions

| Function | Description |
|----------|-------------|
| `pack(msg_dict)` | Encode dict to bytes (serialize + encode) |
| `unpack(data)` | Decode bytes to dict (decode + deserialize) |
| `encode_message(msg_str)` | Encode string to bytes |
| `decode_message(data)` | Decode bytes to string |
| `serialize_message(msg_dict)` | Serialize dict to Douyu KV format |
| `deserialize_message(msg_str)` | Deserialize Douyu KV format to dict |

### Buffer

| Class | Description |
|-------|-------------|
| `MessageBuffer` | UTF-8 safe buffer for streaming packet reassembly |

### Types

| Type | Description |
|------|-------------|
| `MessageType` | Enum of recognized Douyu message types |
| `PacketHeader` | Dataclass for packet header fields |

### Constants

| Constant | Description |
|----------|-------------|
| `DOUYU_WS_URL` | Default WebSocket URL |
| `CLIENT_MSG_TYPE` | Client message type (689) |
| `SERVER_MSG_TYPE` | Server message type (690) |

## Room Discovery (optional)

```python
from dyproto.discovery import resolve_room_id, get_danmu_server

# Resolve vanity URL to numeric ID
room_id = resolve_room_id("longzhu")  # -> 6657

# Get danmu WebSocket servers
urls, room_id = get_danmu_server(6657)
# -> (['wss://danmuproxy.douyu.com:8506/', ...], 6657)
```

Requires `dyproto[discovery]`.

## License

MIT
