forked from Hsury/Bilibili-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili.py
1647 lines (1584 loc) · 80.3 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
"""Bilibili Toolkit 哔哩哔哩工具箱
https://github.com/Hsury/Bilibili-Toolkit"""
banner = r"""
\\ //
\\ //
##################### ________ ___ ___ ___ ________ ___ ___ ___
## ## |\ __ \ |\ \ |\ \ |\ \ |\ __ \ |\ \ |\ \ |\ \
## // \\ ## \ \ \|\ /_\ \ \\ \ \ \ \ \\ \ \|\ /_\ \ \\ \ \ \ \ \
## // \\ ## \ \ __ \\ \ \\ \ \ \ \ \\ \ __ \\ \ \\ \ \ \ \ \
## ## \ \ \|\ \\ \ \\ \ \____ \ \ \\ \ \|\ \\ \ \\ \ \____ \ \ \
## www ## \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\
## ## \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|
#####################
\/ \/ 哔哩哔哩 (゜-゜)つロ 干杯~
"""
import base64
import chardet
import functools
import hashlib
import json
import os
import platform
import random
import requests
import rsa
import shutil
import subprocess
import sys
import threading
import time
import toml
from multiprocessing import freeze_support, Manager, Pool, Process
from selenium import webdriver
from urllib import parse
__author__ = "Hsury"
__email__ = "[email protected]"
__license__ = "SATA"
__version__ = "2020.7.20"
class Bilibili:
app_key = "bca7e84c2d947ac6"
patterns = {
'video': {
'id': 1,
'prefix': "https://www.bilibili.com/video/av",
},
'activity': {
'id': 4,
'prefix': "https://www.bilibili.com/blackboard/",
},
'gallery': {
'id': 11,
'prefix': "https://h.bilibili.com/",
},
'article': {
'id': 12,
'prefix': "https://www.bilibili.com/read/cv",
},
}
def __init__(self, https=True, queue=None):
self._session = requests.Session()
self._session.headers.update({'User-Agent': "Mozilla/5.0 BiliDroid/6.4.0 ([email protected]) os/android model/M1903F11I mobi_app/android build/6040500 channel/bili innerVer/6040500 osVer/9.0.0 network/2"})
self.__queue = queue
self.get_cookies = lambda: self._session.cookies.get_dict(domain=".bilibili.com")
self.get_csrf = lambda: self.get_cookies().get("bili_jct", "")
self.get_sid = lambda: self.get_cookies().get("sid", "")
self.get_uid = lambda: self.get_cookies().get("DedeUserID", "")
self.access_token = ""
self.refresh_token = ""
self.username = ""
self.password = ""
self.info = {
'ban': False,
'coins': 0,
'experience': {
'current': 0,
'next': 0,
},
'face': "",
'level': 0,
'nickname': "",
}
self.protocol = "https" if https else "http"
self.proxy = None
self.proxy_pool = set()
def _log(self, message):
log = f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}][{self.username if self.username else '#' + self.get_uid() if self.get_uid() else ''}] {message}"
print(log)
self.__push_to_queue("log", log)
def _requests(self, method, url, decode_level=2, enable_proxy=True, retry=10, timeout=15, **kwargs):
if method in ["get", "post"]:
for _ in range(retry + 1):
try:
response = getattr(self._session, method)(url, timeout=timeout, proxies=self.proxy if enable_proxy else None, **kwargs)
return response.json() if decode_level == 2 else response.content if decode_level == 1 else response
except:
if enable_proxy:
self.set_proxy()
return None
def _solve_captcha(self, image):
url = "https://bili.dev:2233/captcha"
payload = {'image': base64.b64encode(image).decode("utf-8")}
response = self._requests("post", url, json=payload)
return response['message'] if response and response.get("code") == 0 else None
def __bvid_handle(args_index=None, kwargs_key="aid"):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
self = args[0]
if args_index is not None and args_index < len(args):
result = Bilibili.bvid_to_aid(args[args_index])
if result:
args = list(args)
self._log(f"{args[args_index]}被自动转换为av{result}")
args[args_index] = result
if kwargs_key is not None and kwargs_key in kwargs:
result = Bilibili.bvid_to_aid(kwargs[kwargs_key])
if result:
self._log(f"{kwargs[kwargs_key]}被自动转换为av{result}")
kwargs[kwargs_key] = result
return func(*args, **kwargs)
return wrapper
return decorator
def __push_to_queue(self, manufacturer, data):
if self.__queue:
self.__queue.put({
'uid': self.get_uid(),
'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
'manufacturer': manufacturer,
'data': data,
})
@staticmethod
def bvid_to_aid(bvid="BV17x411w7KC"):
# Snippet source: https://www.zhihu.com/question/381784377/answer/1099438784
table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"
tr = {}
for i in range(58):
tr[table[i]] = i
s = [11, 10, 3, 8, 4, 6]
xor = 177451812
add = 8728348608
r = 0
try:
for i in range(6):
r += tr[bvid[s[i]]] * 58 ** i
return (r - add) ^ xor
except:
return None
@staticmethod
def calc_sign(param):
salt = "60698ba2f68e01ce44738920a0ffe768"
sign_hash = hashlib.md5()
sign_hash.update(f"{param}{salt}".encode())
return sign_hash.hexdigest()
def set_proxy(self, add=None):
if isinstance(add, str):
self.proxy_pool.add(add)
elif isinstance(add, list):
self.proxy_pool.update(add)
if self.proxy_pool:
proxy = random.sample(self.proxy_pool, 1)[0]
self.proxy = {self.protocol: f"{self.protocol}://{proxy}"}
# self._log(f"使用{self.protocol.upper()}代理: {proxy}")
else:
self.proxy = None
return self.proxy
# 登录
def login(self, **kwargs):
def by_cookie():
url = f"{self.protocol}://api.bilibili.com/x/space/myinfo"
headers = {'Host': "api.bilibili.com"}
response = self._requests("get", url, headers=headers)
if response and response.get("code") != -101:
self._log("Cookie仍有效")
return True
else:
self._log("Cookie已失效")
return False
def by_token(force_refresh=False):
if not force_refresh:
param = f"access_key={self.access_token}&appkey={Bilibili.app_key}&ts={int(time.time())}"
url = f"{self.protocol}://passport.bilibili.com/api/v2/oauth2/info?{param}&sign={self.calc_sign(param)}"
response = self._requests("get", url)
if response and response.get("code") == 0:
self._session.cookies.set('DedeUserID', str(response['data']['mid']), domain=".bilibili.com")
self._log(f"Token仍有效, 有效期至{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + int(response['data']['expires_in'])))}")
param = f"access_key={self.access_token}&appkey={Bilibili.app_key}&gourl={self.protocol}%3A%2F%2Faccount.bilibili.com%2Faccount%2Fhome&ts={int(time.time())}"
url = f"{self.protocol}://passport.bilibili.com/api/login/sso?{param}&sign={self.calc_sign(param)}"
self._requests("get", url, decode_level=0)
if all(key in self.get_cookies() for key in ["bili_jct", "DedeUserID", "DedeUserID__ckMd5", "sid", "SESSDATA"]):
self._log("Cookie获取成功")
return True
else:
self._log("Cookie获取失败")
url = f"{self.protocol}://passport.bilibili.com/api/v2/oauth2/refresh_token"
param = f"access_key={self.access_token}&appkey={Bilibili.app_key}&refresh_token={self.refresh_token}&ts={int(time.time())}"
payload = f"{param}&sign={self.calc_sign(param)}"
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
for cookie in response['data']['cookie_info']['cookies']:
self._session.cookies.set(cookie['name'], cookie['value'], domain=".bilibili.com")
self.access_token = response['data']['token_info']['access_token']
self.refresh_token = response['data']['token_info']['refresh_token']
self._log(f"Token刷新成功, 有效期至{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + int(response['data']['token_info']['expires_in'])))}")
return True
else:
self.access_token = ""
self.refresh_token = ""
self._log("Token刷新失败")
return False
def by_password():
def get_key():
url = f"{self.protocol}://passport.bilibili.com/api/oauth2/getKey"
payload = {
'appkey': Bilibili.app_key,
'sign': self.calc_sign(f"appkey={Bilibili.app_key}"),
}
while True:
response = self._requests("post", url, data=payload)
if response and response.get("code") == 0:
return {
'key_hash': response['data']['hash'],
'pub_key': rsa.PublicKey.load_pkcs1_openssl_pem(response['data']['key'].encode()),
}
else:
time.sleep(1)
while True:
key = get_key()
key_hash, pub_key = key['key_hash'], key['pub_key']
url = f"{self.protocol}://passport.bilibili.com/api/v2/oauth2/login"
param = f"appkey={Bilibili.app_key}&password={parse.quote_plus(base64.b64encode(rsa.encrypt(f'{key_hash}{self.password}'.encode(), pub_key)))}&username={parse.quote_plus(self.username)}"
payload = f"{param}&sign={self.calc_sign(param)}"
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = self._requests("post", url, data=payload, headers=headers)
while True:
if response and response.get("code") is not None:
if response['code'] == -105:
url = f"{self.protocol}://passport.bilibili.com/captcha"
headers = {'Host': "passport.bilibili.com"}
response = self._requests("get", url, headers=headers, decode_level=1)
captcha = self._solve_captcha(response)
if captcha:
self._log(f"登录验证码识别结果: {captcha}")
key = get_key()
key_hash, pub_key = key['key_hash'], key['pub_key']
url = f"{self.protocol}://passport.bilibili.com/api/v2/oauth2/login"
param = f"appkey={Bilibili.app_key}&captcha={captcha}&password={parse.quote_plus(base64.b64encode(rsa.encrypt(f'{key_hash}{self.password}'.encode(), pub_key)))}&username={parse.quote_plus(self.username)}"
payload = f"{param}&sign={self.calc_sign(param)}"
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = self._requests("post", url, data=payload, headers=headers)
else:
self._log(f"登录验证码识别服务暂时不可用, {'尝试更换代理' if self.proxy else '10秒后重试'}")
if not self.set_proxy():
time.sleep(10)
break
elif response['code'] == -449:
self._log("服务繁忙, 尝试使用V3接口登录")
url = f"{self.protocol}://passport.bilibili.com/api/v3/oauth2/login"
param = f"access_key=&actionKey=appkey&appkey={Bilibili.app_key}&build=6040500&captcha=&challenge=&channel=bili&cookies=&device=phone&mobi_app=android&password={parse.quote_plus(base64.b64encode(rsa.encrypt(f'{key_hash}{self.password}'.encode(), pub_key)))}&permission=ALL&platform=android&seccode=&subid=1&ts={int(time.time())}&username={parse.quote_plus(self.username)}&validate="
payload = f"{param}&sign={self.calc_sign(param)}"
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = self._requests("post", url, data=payload, headers=headers)
elif response['code'] == 0 and response['data']['status'] == 0:
for cookie in response['data']['cookie_info']['cookies']:
self._session.cookies.set(cookie['name'], cookie['value'], domain=".bilibili.com")
self.access_token = response['data']['token_info']['access_token']
self.refresh_token = response['data']['token_info']['refresh_token']
self._log("登录成功")
return True
else:
self._log(f"登录失败 {response}")
return False
else:
self._log(f"当前IP登录过于频繁, {'尝试更换代理' if self.proxy else '1分钟后重试'}")
if not self.set_proxy():
time.sleep(60)
break
self._session.cookies.clear()
for name in ["bili_jct", "DedeUserID", "DedeUserID__ckMd5", "sid", "SESSDATA"]:
value = kwargs.get(name)
if value:
self._session.cookies.set(name, value, domain=".bilibili.com")
self.access_token = kwargs.get("access_token", "")
self.refresh_token = kwargs.get("refresh_token", "")
self.username = kwargs.get("username", "")
self.password = kwargs.get("password", "")
force_refresh_token = kwargs.get("force_refresh_token", False)
if (not force_refresh_token or not self.access_token or not self.refresh_token) and all(key in self.get_cookies() for key in ["bili_jct", "DedeUserID", "DedeUserID__ckMd5", "sid", "SESSDATA"]) and by_cookie():
return True
elif self.access_token and self.refresh_token and by_token(force_refresh_token):
return True
elif self.username and self.password and by_password():
return True
else:
self._session.cookies.clear()
return False
# 获取用户信息
def get_user_info(self):
url = f"{self.protocol}://api.bilibili.com/x/space/myinfo?jsonp=jsonp"
headers = {
'Host': "api.bilibili.com",
'Referer': f"https://space.bilibili.com/{self.get_uid()}/",
}
response = self._requests("get", url, headers=headers)
if response and response.get("code") == 0:
self.info['ban'] = bool(response['data']['silence'])
self.info['coins'] = response['data']['coins']
self.info['experience']['current'] = response['data']['level_exp']['current_exp']
self.info['experience']['next'] = response['data']['level_exp']['next_exp']
self.info['face'] = response['data']['face']
self.info['level'] = response['data']['level']
self.info['nickname'] = response['data']['name']
self._log(f"{self.info['nickname']}(UID={self.get_uid()}), Lv.{self.info['level']}({self.info['experience']['current']}/{self.info['experience']['next']}), 拥有{self.info['coins']}枚硬币, 账号{'状态正常' if not self.info['ban'] else '被封禁'}")
return True
else:
self._log("用户信息获取失败")
return False
# 修改隐私设置
def set_privacy(self, show_favourite=None, show_bangumi=None, show_tag=None, show_reward=None, show_info=None, show_game=None):
# show_favourite = 展示[我的收藏夹]
# show_bangumi = 展示[订阅番剧]
# show_tag = 展示[订阅标签]
# show_reward = 展示[最近投币的视频]
# show_info = 展示[个人资料]
# show_game = 展示[最近玩过的游戏]
privacy = {
'fav_video': show_favourite,
'bangumi': show_bangumi,
'tags': show_tag,
'coins_video': show_reward,
'user_info': show_info,
'played_game': show_game,
}
url = f"{self.protocol}://space.bilibili.com/ajax/settings/getSettings?mid={self.get_uid()}"
headers = {
'Host': "space.bilibili.com",
'Referer': f"https://space.bilibili.com/{self.get_uid()}/",
}
response = self._requests("get", url, headers=headers)
if response and response.get("status") == True:
for key, value in privacy.items():
if response['data']['privacy'][key] == value:
privacy[key] = None
else:
self._log(f"隐私设置获取失败 {response}")
return False
url = f"{self.protocol}://space.bilibili.com/ajax/settings/setPrivacy"
headers = {
'Host': "space.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': f"https://space.bilibili.com/{self.get_uid()}/",
}
fail = []
for key, value in privacy.items():
if value is not None:
payload = {
key: 1 if value else 0,
'csrf': self.get_csrf(),
}
response = self._requests("post", url, data=payload, headers=headers)
if not response or response.get("status") != True:
fail.append(key)
if not fail:
self._log("隐私设置修改成功")
return True
else:
self._log(f"隐私设置修改失败 {fail}")
return False
# 银瓜子兑换硬币
def silver_to_coin(self, app=True, pc=False):
# app = APP通道
# pc = PC通道
if app:
param = f"access_key={self.access_token}&appkey={Bilibili.app_key}&ts={int(time.time())}"
url = f"{self.protocol}://api.live.bilibili.com/AppExchange/silver2coin?{param}&sign={self.calc_sign(param)}"
response = self._requests("get", url)
if response and response.get("code") == 0:
self._log("银瓜子兑换硬币(APP通道)成功")
else:
self._log(f"银瓜子兑换硬币(APP通道)失败 {response}")
if pc:
url = f"{self.protocol}://api.live.bilibili.com/pay/v1/Exchange/silver2coin"
payload = {
'platform': "pc",
'csrf_token': self.get_csrf(),
}
headers = {
'Host': "api.live.bilibili.com",
'Origin': "https://live.bilibili.com",
'Referer': "https://live.bilibili.com/exchange",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log("银瓜子兑换硬币(PC通道)成功")
else:
self._log(f"银瓜子兑换硬币(PC通道)失败 {response}")
# 观看
@__bvid_handle(1, "aid")
def watch(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/view?aid={aid}"
response = self._requests("get", url)
if response and response.get("data") is not None:
cid = response['data']['cid']
duration = response['data']['duration']
else:
self._log(f"av{aid}信息解析失败")
return False
url = f"{self.protocol}://api.bilibili.com/x/report/click/h5"
payload = {
'aid': aid,
'cid': cid,
'part': 1,
'did': self.get_sid(),
'ftime': int(time.time()),
'jsonp': "jsonp",
'lv': None,
'mid': self.get_uid(),
'csrf': self.get_csrf(),
'stime': int(time.time()),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
url = f"{self.protocol}://api.bilibili.com/x/report/web/heartbeat"
payload = {
'aid': aid,
'cid': cid,
'jsonp': "jsonp",
'mid': self.get_uid(),
'csrf': self.get_csrf(),
'played_time': 0,
'pause': False,
'realtime': duration,
'dt': 7,
'play_type': 1,
'start_ts': int(time.time()),
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
time.sleep(5)
payload['played_time'] = duration - 1
payload['play_type'] = 0
payload['start_ts'] = int(time.time())
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}观看成功")
return True
self._log(f"av{aid}观看失败 {response}")
return False
# 点赞
@__bvid_handle(1, "aid")
def like(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/archive/like"
payload = {
'aid': aid,
'like': 1,
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}点赞成功")
return True
else:
self._log(f"av{aid}点赞失败 {response}")
return False
# 投币
@__bvid_handle(1, "aid")
def reward(self, aid, double=True):
# aid = 稿件av号
# double = 双倍投币
url = f"{self.protocol}://api.bilibili.com/x/web-interface/coin/add"
payload = {
'aid': aid,
'multiply': 2 if double else 1,
'cross_domain': "true",
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}投{2 if double else 1}枚硬币成功")
return True
else:
self._log(f"av{aid}投{2 if double else 1}枚硬币失败 {response}")
return self.reward(aid, False) if double else False
# 收藏
@__bvid_handle(1, "aid")
def favour(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/v2/fav/folder"
headers = {'Host': "api.bilibili.com"}
response = self._requests("get", url, headers=headers)
if response and response.get("data"):
fid = response['data'][0]['fid']
else:
self._log("fid获取失败")
return False
url = f"{self.protocol}://api.bilibili.com/x/v2/fav/video/add"
payload = {
'aid': aid,
'fid': fid,
'jsonp': "jsonp",
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}收藏成功")
return True
else:
self._log(f"av{aid}收藏失败 {response}")
return False
# 三连推荐
@__bvid_handle(1, "aid")
def combo(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/archive/like/triple"
payload = {
'aid': aid,
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}三连推荐成功")
return True
else:
self._log(f"av{aid}三连推荐失败 {response}")
return False
# 分享
@__bvid_handle(1, "aid")
def share(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/share/add"
payload = {
'aid': aid,
'jsonp': "jsonp",
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"av{aid}分享成功")
return True
else:
self._log(f"av{aid}分享失败 {response}")
return False
# 关注
def follow(self, mid, secret=False):
# mid = 被关注用户UID
# secret = 悄悄关注
url = f"{self.protocol}://api.bilibili.com/x/relation/modify"
payload = {
'fid': mid,
'act': 3 if secret else 1,
're_src': 11,
'jsonp': "jsonp",
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': f"https://space.bilibili.com/{mid}/",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"用户{mid}{'悄悄' if secret else ''}关注成功")
return True
else:
self._log(f"用户{mid}{'悄悄' if secret else ''}关注失败 {response}")
return False
# 批量关注
def follow_batch(self, mids):
# mids = 被关注用户UID
url = f"{self.protocol}://api.bilibili.com/x/relation/batch/modify"
payload = {
'fids': ",".join(map(str, mids)),
'act': 1,
'csrf': self.get_csrf(),
're_src': 222,
}
headers = {
'Host': "api.bilibili.com",
'Referer': "https://www.bilibili.com/blackboard/live/activity-NfUS01P8.html",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"用户{', '.join(map(str, mids))}批量关注成功")
return True
else:
self._log(f"用户{', '.join(map(str, mids))}批量关注失败 {response}")
return False
# 拉黑
def ban(self, mid):
# mid = 被拉黑用户UID
url = f"{self.protocol}://api.bilibili.com/x/relation/modify"
payload = {
'fid': mid,
'act': 5,
're_src': 15,
'csrf': self.get_csrf(),
}
headers = {
'Host': "api.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': f"https://space.bilibili.com/{mid}/",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"用户{mid}拉黑成功")
return True
else:
self._log(f"用户{mid}拉黑失败 {response}")
return False
# 批量拉黑
def ban_batch(self, mids):
# mids = 被拉黑用户UID
url = f"{self.protocol}://api.bilibili.com/x/relation/batch/modify"
payload = {
'fids': ",".join(map(str, mids)),
'act': 5,
'csrf': self.get_csrf(),
're_src': 222, # 理论上这个值不起作用...
}
headers = {
'Host': "api.bilibili.com",
'Referer': "https://www.bilibili.com/blackboard/live/activity-NfUS01P8.html",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"用户{', '.join(map(str, mids))}批量拉黑成功")
return True
else:
self._log(f"用户{', '.join(map(str, mids))}批量拉黑失败 {response}")
return False
# 弹幕发送
@__bvid_handle(1, "aid")
def danmaku_post(self, aid, message, page=1, moment=-1):
# aid = 稿件av号
# message = 弹幕内容
# page = 分P
# moment = 弹幕发送时间
url = f"{self.protocol}://api.bilibili.com/x/web-interface/view?aid={aid}"
response = self._requests("get", url)
if response and response.get("data") is not None:
page_info = {page['page']: {
'cid': page['cid'],
'duration': page['duration'],
} for page in response['data']['pages']}
if page in page_info:
oid = page_info[page]['cid']
duration = page_info[page]['duration']
else:
self._log(f"av{aid}不存在P{page}")
return False
else:
self._log(f"av{aid}信息解析失败")
return False
url = f"{self.protocol}://api.bilibili.com/x/v2/dm/post"
headers = {
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
}
while True:
payload = {
'type': 1,
'oid': oid,
'msg': message,
'aid': aid,
'progress': int(moment * 1E3) if moment != -1 else random.randint(0, duration * 1E3),
'color': 16777215,
'fontsize': 25,
'pool': 0,
'mode': 1,
'rnd': int(time.time() * 1E6),
'plat': 1,
'csrf': self.get_csrf(),
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") is not None:
if response['code'] == 0:
self._log(f"av{aid}(P{page})弹幕\"{message}\"发送成功")
return True
elif response['code'] == 36703:
self._log(f"av{aid}(P{page})弹幕发送频率过快, 10秒后重试")
time.sleep(10)
else:
self._log(f"av{aid}(P{page})弹幕\"{message}\"发送失败 {response}")
return False
# 评论点赞
def comment_like(self, otype, oid, rpid):
# otype = 作品类型
# oid = 作品ID
# rpid = 评论ID
if Bilibili.patterns.get(otype) is None:
return False
url = f"{self.protocol}://api.bilibili.com/x/v2/reply/action"
payload = {
'oid': oid,
'type': Bilibili.patterns[otype]['id'],
'rpid': rpid,
'action': 1,
'jsonp': "jsonp",
'csrf': self.get_csrf(),
}
headers = {
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"{Bilibili.patterns[otype]['prefix']}{oid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"评论{rpid}点赞成功")
return True
else:
self._log(f"评论{rpid}点赞失败 {response}")
return False
# 评论发表
def comment_post(self, otype, oid, message):
# otype = 作品类型
# oid = 作品ID
# message = 评论内容
if Bilibili.patterns.get(otype) is None:
return False
url = f"{self.protocol}://api.bilibili.com/x/v2/reply/add"
while True:
payload = {
'oid': oid,
'type': Bilibili.patterns[otype]['id'],
'message': message,
'plat': 1,
'jsonp': "jsonp",
'csrf': self.get_csrf(),
}
headers = {
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"{Bilibili.patterns[otype]['prefix']}{oid}",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") is not None:
if response['code'] == 0:
self._log(f"作品{oid}提交评论\"{message}\"成功")
return True
elif response['code'] == 12015:
response = self._requests("get", response['data']['url'], headers=headers, decode_level=1)
captcha = self._solve_captcha(response)
if captcha:
self._log(f"评论验证码识别结果: {captcha}")
payload['code'] = captcha
else:
self._log(f"评论验证码识别服务暂时不可用, 1分钟后重试")
time.sleep(60)
elif response['code'] == 12035:
self._log(f"作品{oid}提交评论\"{message}\"失败, 该账号被UP主列入评论黑名单")
return False
elif response['code'] == -105:
if "code" in payload:
payload.pop("code")
else:
self._log(f"作品{oid}提交评论\"{message}\"失败 {response}")
return False
# 动态点赞
def dynamic_like(self, did):
# did = 动态ID
url = f"{self.protocol}://api.vc.bilibili.com/dynamic_like/v1/dynamic_like/thumb"
payload = {
'uid': self.get_uid(),
'dynamic_id': did,
'up': 1,
'csrf_token': self.get_csrf(),
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Host': "api.vc.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': "https://space.bilibili.com/208259/",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"动态{did}点赞成功")
return True
else:
self._log(f"动态{did}点赞失败 {response}")
return False
# 动态转发
def dynamic_repost(self, did, message="转发动态", ats=[]):
# did = 动态ID
# message = 转发内容
# ats = 被@用户UID列表
def uid_to_nickname(mid):
url = f"{self.protocol}://api.bilibili.com/x/web-interface/card?mid={mid}"
response = self._requests("get", url)
if response and response.get("code") == 0:
return response['data']['card']['name']
else:
return ""
url = f"{self.protocol}://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/repost"
ctrl = []
for at in zip(ats, [uid_to_nickname(mid) for mid in ats]):
ctrl.append({
'data': str(at[0]),
'location': len(message) + 1,
'length': len(at[1]) + 1,
'type': 1,
})
message = f"{message} @{at[1]}"
payload = {
'uid': self.get_uid(),
'dynamic_id': did,
'content': message,
'at_uids': ",".join([str(at) for at in ats]),
'ctrl': json.dumps(ctrl),
'csrf_token': self.get_csrf(),
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Host': "api.vc.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': "https://space.bilibili.com/208259/",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
self._log(f"动态{did}转发成功")
return True
else:
self._log(f"动态{did}转发失败 {response}")
return False
# 动态清理
def dynamic_purge(self):
def get_lottery_dynamics():
headers = {
'Host': "api.vc.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': f"https://space.bilibili.com/{self.get_uid()}/dynamic",
}
dynamics = []
offset = 0
while True:
url = f"{self.protocol}://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?visitor_uid={self.get_uid()}&host_uid={self.get_uid()}&offset_dynamic_id={offset}"
response = self._requests("get", url, headers=headers)
if response and response.get("code") == 0:
if response['data']['has_more']:
dynamics.extend([{
'did': card['desc']['dynamic_id'],
'lottery_did': card['desc']['orig_dy_id'],
} for card in response['data']['cards'] if card['desc']['orig_type'] == 2 or card['desc']['orig_type'] == 1024])
offset = response['data']['cards'][-1]['desc']['dynamic_id']
else:
return dynamics
dynamics = get_lottery_dynamics()
self._log(f"发现{len(dynamics)}条互动抽奖动态")
delete = 0
for dynamic in dynamics:
url = f"{self.protocol}://api.vc.bilibili.com/lottery_svr/v2/lottery_svr/lottery_notice?dynamic_id={dynamic['lottery_did']}"
headers = {
'Host': "api.vc.bilibili.com",
'Origin': "https://t.bilibili.com",
'Referer': "https://t.bilibili.com/lottery/h5/index/",
}
response = self._requests("get", url, headers=headers)
if response and response.get("code") == 0:
expired = response['data']['status'] == 2 or response['data']['status'] == -1
winning = any(self.get_uid() in winners for winners in [response['data'].get("lottery_result", {}).get(f"{level}_prize_result", []) for level in ["first", "second", "third"]])
if not expired:
self._log(f"动态{dynamic['lottery_did']}尚未开奖({time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(response['data']['lottery_time']))}), 跳过")
else:
if winning:
self._log(f"动态{dynamic['lottery_did']}中奖, 跳过")
else:
url = f"{self.protocol}://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/rm_rp_dyn"
payload = {
'uid': self.get_uid(),
'dynamic_id': dynamic['did'],
'csrf_token': self.get_csrf(),
}
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Host': "api.vc.bilibili.com",
'Origin': "https://space.bilibili.com",
'Referer': f"https://space.bilibili.com/{self.get_uid()}/dynamic",
}
response = self._requests("post", url, data=payload, headers=headers)
if response and response.get("code") == 0:
delete += 1
self._log(f"动态{dynamic['lottery_did']}未中奖, 清理成功")
else:
self._log(f"动态{dynamic['lottery_did']}未中奖, 清理失败")
time.sleep(1)
self._log(f"清理了{delete}条动态")
# 系统通知查询
def system_notice(self, time_span=["", ""], keyword=[]):
# time_span = 时间范围
# keyword = 包含关键字
cursor_span = [int(time.mktime(time.strptime(element, "%Y-%m-%d %H:%M:%S")) * 1E9) if element else "" for element in time_span]
headers = {
'Host': "message.bilibili.com",
'Referer': "https://message.bilibili.com/",
}
notice_list = []
cursor = cursor_span[1]
while True:
url = f"{self.protocol}://message.bilibili.com/api/notify/query.sysnotify.list.do?data_type=1{'&cursor=' + str(cursor) if cursor else ''}"
response = self._requests("get", url, headers=headers)
if response and response.get("code") == 0:
for notice in response['data']:
if not cursor_span[0] or notice['cursor'] > cursor_span[0]:
if not keyword or any(keyword in notice['title'] or keyword in notice['content'] for keyword in keyword):
notice_list.append({
'time': notice['time_at'],
'title': notice['title'],
'content': notice['content'],
})
else:
break
else:
if len(response['data']) == 20:
cursor = notice['cursor']
continue
self._log(f"系统通知获取成功, 总计{len(notice_list)}条通知")
for notice in notice_list:
self._log(f"{notice['title']}({notice['time']}): {notice['content']}")
self.__push_to_queue("system_notice", notice_list)
return notice_list
# 会员购抢购
def mall_rush(self, item_id, thread=1, headless=True, timeout=10):