Skip to content

Instantly share code, notes, and snippets.

@an9er
Created October 21, 2016 09:03
Show Gist options
  • Save an9er/61f6cdb5cd5687fa2fe471b7f1747fa3 to your computer and use it in GitHub Desktop.
Save an9er/61f6cdb5cd5687fa2fe471b7f1747fa3 to your computer and use it in GitHub Desktop.
import socket
import sys
import random
from struct import pack
from struct import unpack
ROOMID = 58879
HOST, PORT = "dm.live.bilibili.com", 788
data = " ".join(sys.argv[1:])
_protocolversion = 1
def SendSocketData(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)
sock.sendall(sendbytes)
def ReceiveMessageLoop(_reader):
while 1:
tmp = _reader.read(4)
expr, = unpack('!I', tmp)
tmp = _reader.read(2)
tmp = _reader.read(2)
tmp = _reader.read(4)
num, = unpack('!I', tmp)
tmp = _reader.read(4)
num2 = expr - 16
if num2 != 0:
num -= 1
if num==0 or num==1 or num==2:
tmp = _reader.read(4)
num3, = unpack('!I', tmp)
print ('房间人数为 %s' % num3)
continue
elif num==3 or num==4:
tmp = _reader.read(num2)
# strbytes, = unpack('!s', tmp)
try: # 为什么还会出现 utf-8 decode error??????
messages = tmp.decode('utf-8')
except:
continue
parseDanMu(messages)
continue
elif num==5 or num==6 or num==7:
tmp = _reader.read(num2)
continue
else:
if num != 16:
tmp = _reader.read(num2)
else:
continue
return
def parseDanMu(self, messages):
try:
dic = json.loads(messages)
except: # 有些情况会 jsondecode 失败,未细究,可能平台导致
return
cmd = dic['cmd']
if cmd == 'LIVE':
print ('直播开始。。。')
return
if cmd == 'PREPARING':
print ('房主准备中。。。')
return
if cmd == 'DANMU_MSG':
commentText = dic['info'][1]
commentUser = dic['info'][2][1]
isAdmin = dic['info'][2][2] == '1'
isVIP = dic['info'][2][3] == '1'
if isAdmin:
commentUser = '管理员 ' + commentUser
if isVIP:
commentUser = 'VIP ' + commentUser
try:
print (commentUser + ' say: ' + commentText)
except:
pass
return
if cmd == 'SEND_GIFT' and config.TURN_GIFT == 1:
GiftName = dic['data']['giftName']
GiftUser = dic['data']['uname']
Giftrcost = dic['data']['rcost']
GiftNum = dic['data']['num']
try:
print(GiftUser + ' 送出了 ' + str(GiftNum) + ' 个 ' + GiftName)
except:
pass
return
if cmd == 'WELCOME' and config.TURN_WELCOME == 1:
commentUser = dic['data']['uname']
try:
print ('欢迎 ' + commentUser + ' 进入房间。。。。')
except:
pass
return
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
# sock.sendall(data + "\n")
_uid = (int)(100000000000000.0 + 200000000000000.0*random.random())
body = '{"roomid":%s,"uid":%s}' % (ROOMID, _uid)
SendSocketData(0, 16, _protocolversion, 7, 1, body)
# Receive data from the server and shut down
received = sock.recv(1024)
finally:
sock.close()
print "Received: {}".format(received)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment