• 大小: 3KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Python
  • 标签: 开源  

资源简介

bilibili_danmu, B 站直播弹幕姬 Python 版

资源截图

代码片段和文件信息

import asyncio
import aiohttp
import xml.dom.minidom
import random
import json
from struct import *
import json
import config
import re

class bilibiliClient():
    def __init__(self):
        self._CIDInfoUrl = ‘http://live.bilibili.com/api/player?id=cid:‘
        self._roomId = 0
        self._ChatPort = 788
        self._protocolversion = 1
        self._reader = 0
        self._writer = 0
        self.connected = False
        self._UserCount = 0
        self._ChatHost = ‘livecmt-1.bilibili.com‘

        self._roomId = input(‘请输入房间号:‘)
        self._roomId = int(self._roomId)

    async def connectServer(self):
        print (‘正在进入房间。。。。。‘)
        with aiohttp.ClientSession() as s:
            async with s.get(‘http://live.bilibili.com/‘ + str(self._roomId)) as r:
                html = await r.text()
                m = re.findall(r‘ROOMID\s=\s(\d+)‘ html)
                ROOMID = m[0]
            self._roomId = int(ROOMID)
            async with s.get(self._CIDInfoUrl + ROOMID) as r:
                xml_string = ‘‘ + await r.text() + ‘
                dom = xml.dom.minidom.parseString(xml_string)
                root = dom.documentElement
                server = root.getElementsByTagName(‘server‘)
                self._ChatHost = server[0].firstChild.data



        reader writer = await asyncio.open_connection(self._ChatHost self._ChatPort)
        self._reader = reader
        self._writer = writer
        print (‘链接弹幕中。。。。。‘)
        if (await self.SendJoinChannel(self._roomId) == True):
            self.connected = True
            print (‘进入房间成功。。。。。‘)
            print (‘链接弹幕成功。。。。。‘)
            await self.ReceiveMessageLoop()

    async def HeartbeatLoop(self):
        while self.connected == False:
            await asyncio.sleep(0.5)

        while self.connected == True:
            await self.SendSocketData(0 16 self._protocolversion 2 1 ““)
            await asyncio.sleep(30)


    async def SendJoinChannel(self channelId):
        self._uid = (int)(100000000000000.0 + 200000000000000.0*random.random())
        body = ‘{“roomid“:%s“uid“:%s}‘ % (channelId self._uid)
        await self.SendSocketData(0 16 self._protocolversion 7 1 body)
        return True


    async def SendSocketData(self packetlength magic ver action param body):
        bytearr = body.encode(‘utf-8‘)
        if packetlength == 0:
            packetlength = len(bytearr) + 16
        sendbytes = pack(‘!IHHII‘ packetlength magic ver action param)
        if len(bytearr) != 0:
            sendbytes = sendbytes + bytearr
        self._writer.write(sendbytes)
        await self._writer.drain()


    async def ReceiveMessageLoop(self):
        while self.connected == True:
            tmp = await self._reader.read(4)
            expr = unpack(‘!I‘ tmp)
            tmp = await self._reader.read(2)
            tmp = await self._reader.read(2)
            tmp = await self._reader.read(4)
            num = unpack(‘!I‘ tmp)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-07-14 13:43  bilibili_danmu-master\
     文件          50  2017-07-14 13:43  bilibili_danmu-master\.gitignore
     文件         720  2017-07-14 13:43  bilibili_danmu-master\README.md
     文件        5796  2017-07-14 13:43  bilibili_danmu-master\bilibiliClient.py
     文件          31  2017-07-14 13:43  bilibili_danmu-master\config.py
     文件         423  2017-07-14 13:43  bilibili_danmu-master\main.py

评论

共有 条评论