-
Notifications
You must be signed in to change notification settings - Fork 0
/
biliDmShooter.py
184 lines (157 loc) · 6.72 KB
/
biliDmShooter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import requests
import random
import time
import re
class postDmData:
def __init__(self):
self.cookie = ''
self.aid = ''
self.cid = ''
self.csrf = ''
self.uid = ''
pass
def getVideoId(self,html):
htmlList = html.split("\n")
linenum = 0
for lineItem in htmlList:
if ('cid' in lineItem) and ('aid' in lineItem):
linenum += 1
pattenStr = r"cid=(\d+)&aid=(\d+)&pre"
patten = re.compile(pattenStr)
idList = patten.findall(lineItem)[0]
self.cid = idList[0]
self.aid = idList[1]
print ("get cid = %s,and aid = %s" % (self.cid,self.aid))
return True
else:
print("get video data fail")
return False
def getCookie(self,webCookie):
self.cookie = webCookie
pattenCsrfStr = r"bili_jct=(.+?);"
findCsrfList = re.findall(pattenCsrfStr, webCookie)
pattenUidStr = r"DedeUserID=(.+?);"
findUidList = re.findall(pattenUidStr, webCookie)
if len(findCsrfList) != 0 and len(findUidList) != 0:
self.csrf = findCsrfList[0]
self.uid = findUidList[0]
print("get uid = %s,and token = %s" % (self.uid, self.csrf))
return True
else:
print("get user data fail")
return False
class dmShooter:
postLink = "https://interface.bilibili.com/dmpost?cid=21044350&aid=12802603&pid=1&ct=1"
heads = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Connection': 'keep-alive',
'Content-Length': '176',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'UM_distinctid=123-12-23-23-23; pgv_pvi=; fts=123; sid=123; rpdid=123; buvid3=; _ga=8; tma=1.1; tmd=31.; _qddaz=QD.xek9q5; HTML5PlayerCRC32=56757; finger=54764576; uTZ=-76; LIVE_BUVID=46577; LIVE_BUVID__ckMd5=456; DedeUserID=123456; DedeUserID__ckMd5=233333;SESSDATA=123456; bili_jct=123456; user_face=123456; pgv_si=123456; purl_token=123456; biliMzIsnew=1; biliMzTs=null; _cnt_pm=0; _cnt_notify=0; _dfcaptcha=123456',
'Host': 'interface.bilibili.com',
'Origin': 'https://www.bilibili.com',
'Referer': 'https://www.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
}
params = {
'fontsize': '25',
'pool': '0',
'mode': '4',
'color': '16777215',
'rnd': '1501590340951250',
'message': '23333',
'playTime': '1636.963084',
'cid': '21044350',
'date': '2017-08-01 20:48:56',
'csrf': '9f179a962285670f35d1c2392aa2d330'
}
def __init__(self, postDmData):
self.postDmData = postDmData
self.session = requests.session()
def postDm(self, videoTime, dmText):
#get current time
sendDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#set post Link
self.postLink="https://interface.bilibili.com/dmpost?cid=%s&aid=%s&pid=1&ct=1"%(self.postDmData.cid,self.postDmData.aid)
self.params['date'] = str(sendDate)
self.params['playTime'] = videoTime
self.params['message'] = dmText
self.params['csrf'] = self.postDmData.csrf
self.params['cid'] = self.postDmData.cid
self.heads['Cookie'] = self.postDmData.cookie
rndNum = random.randint(1502109879020920, 1503617969263783)
self.params['rnd'] = str(rndNum)
print (str(sendDate),videoTime,str(rndNum),dmText)
jscontent = requests.session().post(self.postLink, headers=self.heads, data=self.params).text
return jscontent
if __name__ == '__main__':
dmData = postDmData()
#input website
#biliLink = "https://www.bilibili.com/video/av12802603/"
biliLink = input("please input your bilibili video link:")
htm = ""
try:
htm =requests.get(biliLink)
except requests.exceptions.SSLError:
print("exit because of SSL error")
exit(1)
except requests.exceptions.ConnectionError:
print("exit because of network connectionError")
exit(1)
except requests.exceptions.MissingSchema:
print("exit because of url entry error")
exit(1)
if(dmData.getVideoId(htm.text) == False):
print("exit because of web page fault")
exit(1)
#input cookie
webCookie = input("please input your cookie:")
dmData.getCookie(webCookie)
dmShooter = dmShooter(dmData)
fileNmae = input("please input your lyric file(ass or lrc file) name:" )
if fileNmae.endswith('.ass'):
try:
file_object = open(fileNmae, 'r', encoding='UTF-8')
except IOError:
print("exit because the file don't exist")
exit()
assLines = file_object.readlines()
file_object.close()
for lintItem in assLines:
if lintItem.count('Dialogue') == 1:
time.sleep(5.0 + random.random())
currentLine = (lintItem.split('\n')[0]).split(",")
timeMarkList = currentLine[1].split(':')
timeMark = float(timeMarkList[0]) * 3600 + float(timeMarkList[1]) * 60 + float(timeMarkList[2])
# print(currentLine[1],'%6f'% timeMark, currentLine[9])
postText = dmShooter.postDm('%6f' % timeMark, currentLine[9])
print(postText)
else:
continue
elif fileNmae.endswith('.lrc'):
try:
file_object = open(fileNmae, 'r', encoding='UTF-8')
except IOError:
print("exit because the file don't exist")
exit()
lrcLines = file_object.readlines()
file_object.close()
for lintItem in lrcLines:
lintItem = lintItem.strip('\n')
pattenStr = r"\[(\d+):(\d+)\.(\d+)\](.+)"
if(re.match(pattenStr, lintItem)):
time.sleep(5.0 + random.random())
dataList = re.findall(pattenStr, lintItem)
timeMark = float(str(dataList[0][0])) * 60 + float(str(dataList[0][1])) + float(str(dataList[0][2])) / 100
postText = dmShooter.postDm('%6f' % timeMark, dataList[0][3])
print(postText)
pass
else:
print("miss")
pass
else:
pass
else:
print("this file format is not support")