Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does the bounce code work?


Jun 02, 2021 Article blog


Table of contents


danmu

 How does the bounce code work?1  How does the bounce code work?2 English Version

Danmu is an open source live platform pop-up interface that you can easily operate with no foundation.

With less than thirty lines of code, you can use Python for further development based on the bullet screen.

Support for fish fighting, pandas, battle flags, national, Bilibili multi-platform impeachment.

Support for all versions of Python, no platform dependency, convenient for all types of developers, enthusiasts to use.

Once on, the host on-line automatic connection, off-line automatically reconnect.

Deeply optimized, almost no one screen is missed, and abstract architecture is used for easy modification and development.

Documents

You can get help using it here.

Installation

Danmu can be installed via this command:

pip install danmu

Simple uses

With the following code, the bullet screen can be initially processed via Python.

import time, sys

from danmu import DanMuClient

def pp(msg): print(msg.encode(sys.stdin.encoding, 'ignore'). decode(sys.stdin.encoding))

dmc = DanMuClient('http://www.douyu.com/lslalala') if not dmc.isValid(): print('Url not valid')

@dmc.danmu def danmu_fn(msg): pp('[%s] %s' % (msg['NickName'], msg['Content']))

@dmc.gift def gift_fn(msg): pp('[%s] sent a gift!' % msg['NickName'])

@dmc.other def other_fn(msg): pp('Other message received')

dmc.start(blockThread = True)

Screenshot

 How does the bounce code work?3

Advanced uses

Set the default message processing method

Messages are registered in three types: danmu, gift, and other

Corresponds to: ordinary impeachment, gift message, other messages

If a type is not registered, the default method is used, as follows:

from danmu import DanMuClient

dmc = DanMuClient('http://www.douyu.com/lslalala')

@dmc.default def default_fn(msg): pp('[%s] %s' % (msg['NickName'], msg['Content']))

Cancel a method that has already been registered

You can cancel a certain type of registration by doing the following.

from danmu import DanMuClient

dmc = DanMuClient('http://www.douyu.com/lslalala') dmc.default(None) dmc.gift(None)

FAQ

Q: What are the message formats you get?

A: The message is a dictionary and must have three keys: NickName, Content, MsgType, corresponding to the user name, message content, and message type.

Comments

If you have any questions or suggestions, you can discuss them with me in this Issue

https://github.com/littlecodersh/danmu/#danmu