-
Notifications
You must be signed in to change notification settings - Fork 2
/
bili.py
140 lines (128 loc) · 4.35 KB
/
bili.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
import requests
import json
import os
import sys
from os import path
from moviepy.editor import VideoFileClip
from io import BytesIO
import tempfile
import uuid
from urllib.parse import quote_plus
headers = {
'User-Agent': 'PostmanRuntime/7.26.8',
'Referer': 'https://www.bilibili.com/',
}
def fname_escape(name):
return name.replace('\\', '\') \
.replace('/', '/') \
.replace(':', ':') \
.replace('*', '*') \
.replace('?', '?') \
.replace('"', '"') \
.replace('<', '<') \
.replace('>', '>') \
.replace('|', '|')
def batch_home(mid, st, ed, to_audio=False):
for i in range(st, ed + 1):
url = f'https://api.bilibili.com/x/space/arc/search?search_type=video&mid={mid}&pn={i}&order=pubdate'
j = requests.get(url, headers=headers).json()
if j['code'] != 0:
print('解析失败:' + j['message'])
return
for it in j['data']['list']['vlist']:
bv = it['bvid']
download_safe(bv, to_audio)
print('bvid')
def batch_kw(kw, st, ed, to_audio=False):
kw_enco = quote_plus(kw)
for i in range(st, ed + 1):
url = f'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={kw_enco}&page={i}&order=pubdate'
j = requests.get(url, headers=headers).json()
if j['code'] != 0:
print('解析失败:' + j['message'])
return
for it in j['data']['result']:
bv = it['bvid']
download_safe(bv, to_audio)
print('bvid')
def download_safe(id, to_audio=False):
try: download(id, to_audio)
except Exception as ex: print(ex)
def download(id, to_audio=False):
av = ''
bv = ''
if id.lower().startswith('av'):
av = id[2:]
else:
bv = id
url = f'https://api.bilibili.com/x/web-interface/view?bvid={bv}&aid={av}'
j = requests.get(url, headers=headers).json()
if j['code'] != 0:
print('获取 CID 失败:' + j['message'])
return
av = j['data']['aid']
bv = j['data']['bvid']
cid = j['data']['cid']
title = fname_escape(j['data']['title'])
author = fname_escape(j['data']['owner']['name'])
print(title, author)
name = f'{title} - {author} - {bv}'
fname = f'out/{name}'
if to_audio:
fname += '.mp3'
else:
fname += '.flv'
if path.isfile(fname):
print(f'{fname} 已存在')
return
try: os.mkdir('out')
except: pass
url = f'https://api.bilibili.com/x/player/playurl?cid={cid}&otype=json&bvid={bv}&aid={av}'
j = requests.get(url, headers=headers).json()
if j['code'] != 0:
print('解析失败:' + j['message'])
return
video_url = j['data']['durl'][0]['url']
video = requests.get(video_url, headers=headers).content
if not to_audio:
open(fname, 'wb').write(video)
return
tmp_fname = path.join(tempfile.gettempdir(), uuid.uuid4().hex + '.flv')
open(tmp_fname, 'wb').write(video)
vc = VideoFileClip(tmp_fname)
vc.audio.write_audiofile(fname)
vc.reader.close()
os.unlink(tmp_fname)
def main():
cmd = sys.argv[1]
if cmd in ['dl', 'download']:
download(sys.argv[2])
elif cmd in ['dlau', 'dlaudio']:
download(sys.argv[2], True)
elif cmd == 'batchkw':
batch_kw(
sys.argv[2],
int(sys.argv[3]) if len(sys.argv) > 3 else 1,
int(sys.argv[4]) if len(sys.argv) > 4 else 50,
)
elif cmd == 'batchkwau':
batch_kw(
sys.argv[2],
int(sys.argv[3]) if len(sys.argv) > 3 else 1,
int(sys.argv[4]) if len(sys.argv) > 4 else 50,
True,
)
elif cmd == 'batchhome':
batch_home(
sys.argv[2],
int(sys.argv[3]) if len(sys.argv) > 3 else 1,
int(sys.argv[4]) if len(sys.argv) > 4 else 50,
)
elif cmd == 'batchhomeau':
batch_home(
sys.argv[2],
int(sys.argv[3]) if len(sys.argv) > 3 else 1,
int(sys.argv[4]) if len(sys.argv) > 4 else 50,
True,
)
if __name__ == '__main__': main()