The project address

Github.com/jonssonyan/…

The development tools

  1. Python 3.7.9
  2. Pycharm 2019.3.5

code

import threading
import time
import tkinter.simpledialog
from tkinter import END, simpledialog, messagebox

import requests


class Danmu() :
    def __init__(self, room_id) :
        # barrage url
        self.url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'
        # request header
        self.headers = {
            'Host': 'api.live.bilibili.com'.'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; x64; The rv: 78.0) Gecko / 20100101 Firefox 78.0 / ',}# define the parameters passed by POST
        self.data = {
            'roomid': room_id,
            'csrf_token': ' '.'csrf': ' '.'visit_id': ' ',}# log write object
        self.log_file_write = open('danmu.log', mode='a', encoding='utf-8')
        # read log
        log_file_read = open('danmu.log', mode='r', encoding='utf-8')
        self.log = log_file_read.readlines()

    def get_danmu(self) :
        Pause 0.5 to prevent high CPU usage
        time.sleep(1)
        # Get live broadcast barrage
        html = requests.post(url=self.url, headers=self.headers, data=self.data).json()
        Parse the bullet-screen list
        for content in html['data'] ['room'] :# get a nickname
            nickname = content['nickname']
            # Get comments
            text = content['text']
            # Get time to speak
            timeline = content['timeline']
            # Record your speech
            msg = timeline + ' ' + nickname + ':' + text
            # Check whether the corresponding message exists in the log. If it is the same as the last one, print it and save it
            if msg + '\n' not in self.log:
                # print message
                listb.insert(END, msg)
                listb.see(END)
                # save log
                self.log_file_write.write(msg + '\n')
                # Add to log list
                self.log.append(msg + '\n')
            Clear the variable cache
            nickname = ' '
            text = ' '
            timeline = ' '
            msg = ' '


def bilibili(delay, room_id) :
    Create bDanmu instance
    bDanmu = Danmu(room_id)
    while True:
        Pause to prevent excessive CPU usage
        time.sleep(delay)
        # Get barrage
        bDanmu.get_danmu()


def author() :
    # Pop-up dialog box
    messagebox.showinfo(title='about', message='Author: Zhuang Jonson\ N Date: February 4, 2021 \n wechat official Number: Science cat')


# tkinter GUI
window = tkinter.Tk()
window.title('BiliBli Barrage viewer tool ')
window.minsize(300.500)
window.geometry('400x600+250+100')

# menu bar
menubar = tkinter.Menu(window)
# Open in the menu bar
menubar.add_command(label='about', command=author)
After creating the menubar, configure the menubar menubar to be displayed
window.config(menu=menubar)

# the scroll bar
sc = tkinter.Scrollbar(window)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
# Listbox control
listb = tkinter.Listbox(window, yscrollcommand=sc.set)
# Place the widget in the main window
listb.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
# Scroll bar moves, list moves
sc.config(command=listb.yview)

# Get string (title, hint, initial value)
room_id = simpledialog.askstring(title='Please enter room number', prompt='Please enter room number:'
                                 , initialvalue='21089733')
if room_id is not None:
    Create a thread to fetch bullets
    try:
        t = threading.Thread(target=bilibili, args=(0.5.str(room_id),))
        t.setDaemon(True)
        t.start()
    except:
        print("Error: Startup failed! Please check the correct room number.")
Enter the loop display
window.mainloop()
Copy the code

compile

Use the third-party package pyInstaller

The command

pyinstaller -F -w bilibli-danmu.py
Copy the code

Parameter interpretation

  • -f, -onefile Generates a single executable file
  • -w, –windowed, –noconsolc Specifies that a program run without displaying a command line window (Windows only).

Common options supported by PyInstaller

supplement

  1. After executing the command, the dist folder will be created in the project directory, where the compiled files will be located
  2. Pyinstaller does not compile across platforms. On Windows, it can only compile as a Windows executable (.exe). The same is true for MAC Linux