This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bilibili.py
116 lines (98 loc) · 3.68 KB
/
bilibili.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
import requests
import json
import rsa
import base64
import random
import time
import os
import sys
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
def _rsa_encrypt(pubkey, hash, password):
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(pubkey)
password = hash + password
return base64.b64encode(rsa.encrypt(password.encode('utf-8'), pubkey))
def get_CN_time():
return time.asctime(time.gmtime(time.time() + 8 * 60 * 60))
def log(string):
with open('log.txt', 'a') as f:
f.write(string + '\r\n')
class Bilibili(object):
def __init__(self):
self.session = requests.session()
self.session.get('https://account.bilibili.com/login')
def _get_vcode(self):
res = self.session.get('https://passport.bilibili.com/captcha')
with open('vcode.png', 'wb') as f:
f.write(res.content)
def _get_key(self):
res = self.session.get('https://passport.bilibili.com/login?act=getkey')
obj = json.loads(res.text)
self.key = obj['key']
self.hash = obj['hash']
def check_login(self, output=1):
self.session.get('http://www.bilibili.com/')
cookies = self.session.cookies.get_dict()
if 'DedeUserID' in cookies.keys():
if output:
print('isLogin = True, ID=', cookies['DedeUserID'])
return True
else:
if output:
print('isLogin = False')
return False
def dumps(self, filename):
print('dumping in file...')
with open(filename, 'w') as f:
cookie = self.session.cookies.get_dict()
f.write(json.dumps(cookie))
def loads(self, filename):
print('loading from file...')
try:
with open(filename, 'r') as f:
cookie = json.loads(f.read())
self.session.cookies.update(cookie)
finally:
return self.check_login()
def login(self, userid, password):
self._get_vcode()
self._get_key()
vcode = input('input vcode:')
self.session.post(url='https://passport.bilibili.com/login/dologin',
data={
'act': 'login',
'gourl': 'https://passport.bilibili.com/login/dologin',
'keeptime': '2592000',
'pwd': _rsa_encrypt(self.key, self.hash, password),
'userid': userid,
'vdcode': vcode
})
return self.check_login()
def give_coin(self, aid):
self.session.headers.update({'Referer': 'http://www.bilibili.com/video/av' + str(aid)})
res = self.session.post('http://www.bilibili.com/plus/comment.php',
data={
'aid': aid,
'multiply': 1,
'player': 1,
'rating': 100
})
print('give 1 coin to aid:', aid, res.text)
def get_video(self):
res = self.session.get('http://api.bilibili.cn/recommend')
obj = json.loads(res.text)
L = [x['aid'] for x in obj['list']]
return L
def main():
user = Bilibili()
#login
if not user.loads('cookie.json'):
user.login('****', '****')
if user.check_login():
user.dumps('cookie.json')
#give corn
L = user.get_video()
user.give_coin(L[random.randint(0, len(L))])
###
log(get_CN_time() + ' started')
if __name__ == '__main__':
main()