-
Notifications
You must be signed in to change notification settings - Fork 8
/
bilive.py
173 lines (155 loc) · 4.9 KB
/
bilive.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
'''
Create at 2016-11-08
@author latelan
'''
import sys
import os
import time
import requests
import json
import re
import rsa
from base64 import b64encode
from yundama import YunDaMa
import config
reload(sys)
sys.setdefaultencoding('utf-8')
COOKIE_FILE = 'BILIVE_COOKIE_FILE'
def login(requester):
'''
login bilibili and save cookie to file
'''
userid = config.B_USER
pwd = config.B_PWD
# get key, use rsa encrypt pwd
getkey_url = 'https://passport.bilibili.com/login'
param_data = {
'act': 'getkey',
'_': str(int(time.time()*1000))
}
res = requester.get(getkey_url, params=param_data)
hashkey = res.json()
pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(hashkey['key'])
enpwd = b64encode(rsa.encrypt((hashkey['hash'] + pwd).encode(), pub_key)).decode()
# get captcha
captcha_url = 'https://passport.bilibili.com/captcha'
res = requester.get(captcha_url)
captcha_content = res.content
vdcode = get_captcha(captcha_content)
# login
login_url = 'https://passport.bilibili.com/login/dologin'
post_data = {
'act': 'login',
'gourl': 'http://www.bilibili.com/',
'keeptime':2592000,
'vdcode': vdcode,
'userid': userid,
'pwd': enpwd
}
res = requester.post(login_url, data=post_data)
cookie = res.request.headers['Cookie']
save_cookie(COOKIE_FILE, cookie)
def get_captcha(captcha):
'''
get result of the captcha, user can implement, here using YunDaMa
@param captcha - capthcha content,
@return the result of captcha
'''
username = config.YDM_USER
password = config.YDM_PWD
ydm = YunDaMa(username, password)
captcha = ydm.get_captcha(captcha, 'capthcha.jpg', 'image/jpeg')
if not captcha:
print'Get captcha failed'
exit()
return captcha
def check_login(requester):
'''
check login status
'''
user_url = 'http://live.bilibili.com/User/getUserInfo'
res = requester.get(user_url)
user = res.json()
if user['code'] == 'REPONSE_OK':
return user
else:
print'Login Failed: ' + res.text.decode('unicode_escape')
os.remove(COOKIE_FILE)
time.sleep(3)
exit()
def heart(requester):
'''
heartbeat
'''
heart_url = 'http://live.bilibili.com/User/userOnlineHeart'
room_id = get_room_id(requester)
print'RoomId: ' + room_id
referer_header = 'http://live.bilibili.com/' + room_id
headers = {'Referer': referer_header}
res = requester.get(heart_url, headers=headers)
return res.text
def get_room_id(requester):
'''
get live room id
'''
live_url = 'http://live.bilibili.com/'
res = requester.get(live_url)
room_id = re.search(r'data-roomid="(\d+)"', res.text)
if room_id:
return room_id.group(1)
else:
roomid_url = 'http://api.live.bilibili.com/area/newRecom?area=all'
res = requests.get(roomid_url)
data = res.json()
if 'roomid' in data['data'][0]:
return str(data['data'][0]['roomid'])
else:
print'Get roomid failed'
exit()
def save_cookie(filepath, cookie):
'''
save cookies as json formt
"k1=v1; k2=v2" -> "{k1:v1, k2:v2}"
'''
matcher = re.findall(r'(\S+)=(\S[^;]+)', cookie)
cookies = {}
for (k,v) in matcher:
cookies[k] = v
cookies = json.dumps(cookies)
file = open(filepath, 'w')
file.write(cookies)
file.close()
def get_cookie(filepath):
file = open(filepath, 'r')
cookie = file.read()
file.close()
return json.loads(cookie)
s = requests.Session()
if not os.path.exists(COOKIE_FILE):
login(s)
else:
cookie_dict = get_cookie(COOKIE_FILE)
cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
s.cookies = cookies
d = check_login(s)
print'Hello, '+d["data"]["uname"]+'!'
upr = str(d["data"]['user_next_intimacy'] - d["data"]['user_intimacy'])
result = heart(s)
h = json.loads(result)
if h["code"] != 0:
for x in xrange(1,6):
print "Heart Status: Error, Retrying......("+ str(x) +")"
if x != 5:
time.sleep(2)
else:
time.sleep(10)
result = heart(s)
h = json.loads(result)
if h ["code"] != 0:
continue
else:
print 'Live Level: '+ str(d["data"]["user_level"]) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(d["data"]["user_level_rank"]) +'\nHeart Status: Successful\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')
time.sleep(3)
print 'Live Level: '+ str(d["data"]["user_level"]) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(d["data"]["user_level_rank"]) +'\nHeart Status: Error\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')
else:
print 'Live Level: '+ str(d["data"]["user_level"]) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(d["data"]["user_level_rank"]) +'\nHeart Status: Successful\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')