-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSendDanMu.py
62 lines (58 loc) · 1.82 KB
/
SendDanMu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#coding:utf-8
import urllib
import urllib.request
import http.cookiejar
import json
import time
import os
import sys
import datetime
import time
import var_set
import random
cookie = var_set.cookie
roomid = var_set.roomid
csrf_token = var_set.csrf_token
#发送弹幕函数,通过post完成,具体可以自行使用浏览器,进入审查元素,监控network选项卡研究
def send_dm(s):
global cookie
global roomid
global dm_lock
global csrf_token
try:
url = "https://api.live.bilibili.com/msg/send"
postdata =urllib.parse.urlencode({
'color':'16777215',
'fontsize':'25',
'mode':'1',
'msg':s,
'rnd':str(int(time.time())),
'roomid':roomid,
'csrf_token':csrf_token,
'csrf':csrf_token
}).encode('utf-8')
header = {
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"utf-8",
"Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection":"keep-alive",
"Cookie":cookie,
"Host":"api.live.bilibili.com",
"Referer":"http://live.bilibili.com/"+roomid,
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 Vivaldi/2.2.1388.37"
}
req = urllib.request.Request(url,postdata,header)
dm_result = json.loads(urllib.request.urlopen(req,timeout=3).read().decode('utf-8'))
if len(dm_result['msg']) > 0:
print('[error]弹幕发送失败:'+s)
print(dm_result)
else:
print('[log]发送弹幕:'+s)
except Exception as e:
print('[error]send dm error')
print(e)
time.sleep(1.5)
def send_dm_long(s):
n=var_set.dm_size
for i in range(0, len(s), n):
send_dm(s[i:i+n])