-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.py
36 lines (28 loc) · 1010 Bytes
/
run.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
import os
import json
import time
from bili import bilibili
MAX_PAGE = 10000
PAGE_PER_NUM = 300
HISTORY_DIR = 'history/'
def save(data, filename):
if not os.path.exists(HISTORY_DIR):
os.makedirs(HISTORY_DIR)
with open(HISTORY_DIR+filename, 'w') as fp:
json.dump(data, fp, ensure_ascii=False)
def get_all_bili_history(cookie_file):
headers = bilibili.get_header(cookie_file)
history = {'all': []}
for page_num in range(MAX_PAGE):
time.sleep(0.6)
url = 'https://api.bilibili.com/x/v2/history?pn={pn}&ps={ps}&jsonp=jsonp'.format(pn=page_num, ps=PAGE_PER_NUM)
result = bilibili.req_get(headers, url)
print('page = {} code = {} datalen = {}'.format(page_num, result['code'], len(result['data'])))
if len(result['data']) == 0:
break
history['all'].append(result)
return history
if __name__ == '__main__':
cookie = 'cookies.txt'
history = get_all_bili_history(cookie)
save(history, 'history.json')