This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
/
bilibili.py
438 lines (389 loc) · 17.2 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
# -*- coding: utf-8 -*-
"""
:author: comwrg
:license: MIT
:time: 2017/06/09
"""
import base64
import hashlib
import logging
import math
import os
import re
import requests
import rsa
import time
from io import BufferedReader
from typing import *
from urllib import parse
from requests.adapters import HTTPAdapter
from urllib3 import Retry
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class VideoPart:
def __init__(self, path, title='', desc=''):
self.path = path
self.title = title
self.desc = desc
def __repr__(self):
return '<{clazz}, path: {path}, title: {title}, desc: {desc}>'.format(clazz=self.__class__.__name__, path=self.path, title=self.title, desc=self.desc)
class Bilibili:
def __init__(self, cookie=None):
self.session = requests.session()
# debug
def debug_response(r, *args, **kwargs):
log.debug(r.text)
self.session.hooks = {'response': debug_response}
#
if cookie:
self.session.headers["cookie"] = cookie
self.csrf = re.search('bili_jct=(.*?)(;|$)', cookie).group(1)
self.mid = re.search('DedeUserID=(.*?)(;|$)', cookie).group(1)
self.session.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
self.session.headers['Referer'] = 'https://space.bilibili.com/{mid}/#!/'.format(mid=self.mid)
# session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
# session.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
def login(self, user, pwd):
"""
:param user: username
:type user: str
:param pwd: password
:type pwd: str
:return: if success return True
else raise Exception
"""
APPKEY = '4409e2ce8ffd12b8'
ACTIONKEY = 'appkey'
BUILD = 101800
DEVICE = 'android_tv_yst'
MOBI_APP = 'android_tv_yst'
PLATFORM = 'android'
APPSECRET = '59b43e04ad6965f34319062b478f83dd'
def md5(s):
h = hashlib.md5()
h.update(s.encode('utf-8'))
return h.hexdigest()
def sign(s):
"""
:return: return sign
"""
return md5(s + APPSECRET)
def signed_body(body):
"""
:return: body which be added sign
"""
if isinstance(body, str):
return body + '&sign=' + sign(body)
elif isinstance(body, dict):
ls = []
for k, v in body.items():
ls.append(k + '=' + v)
body['sign'] = sign('&'.join(ls))
return body
def getkey():
"""
:return: hash, key
"""
r = self.session.post(
'https://passport.bilibili.com/api/oauth2/getKey',
signed_body({'appkey': APPKEY}),
)
# {"ts":1544152439,"code":0,"data":{"hash":"99c7573759582e0b","key":"-----BEGIN PUBLIC----- -----END PUBLIC KEY-----\n"}}
json = r.json()
data = json['data']
return data['hash'], data['key']
def access_token_2_cookie(access_token):
r = self.session.get(
'https://passport.bilibili.com/api/login/sso?' + \
signed_body(
'access_key={access_token}&appkey={appkey}&gourl=https%3A%2F%2Faccount.bilibili.com%2Faccount%2Fhome'
.format(access_token=access_token, appkey=APPKEY),
),
allow_redirects=False,
)
return r.cookies.get_dict(domain=".bilibili.com")
self.session.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
h, k = getkey()
pwd = base64.b64encode(
rsa.encrypt(
(h + pwd).encode('utf-8'),
rsa.PublicKey.load_pkcs1_openssl_pem(k.encode()),
),
)
user = parse.quote_plus(user)
pwd = parse.quote_plus(pwd)
r = self.session.post(
'https://passport.snm0516.aisee.tv/api/tv/login',
signed_body(
'appkey={appkey}&build={build}&captcha=&channel=master&'
'guid=XYEBAA3E54D502E37BD606F0589A356902FCF&mobi_app={mobi_app}&'
'password={password}&platform={platform}&token=5598158bcd8511e2&ts=0&username={username}'
.format(appkey=APPKEY, build=BUILD, platform=PLATFORM, mobi_app=MOBI_APP, username=user, password=pwd)),
)
json = r.json()
if json['code'] == -105:
# need captcha
raise Exception('TODO: login with captcha')
if json['code'] != 0:
raise Exception(r.text)
access_token = json['data']['token_info']['access_token']
cookie_dict = access_token_2_cookie(access_token)
cookie = '; '.join(
'%s=%s' % (k, v)
for k, v in cookie_dict.items()
)
self.session.headers["cookie"] = cookie
self.csrf = re.search('bili_jct=(.*?)(;|$)', cookie).group(1)
self.mid = re.search('DedeUserID=(.*?)(;|$)', cookie).group(1)
self.session.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
self.session.headers['Referer'] = 'https://space.bilibili.com/{mid}/#!/'.format(mid=self.mid)
return True
def upload(self,
parts: Union[VideoPart, List[VideoPart]],
title: str,
tid: int,
tag: List[str],
desc: str,
source='',
cover='',
no_reprint: bool = True,
dynamic='',
dtime=None,
open_elec: bool = True,
open_subtitle: bool = True,
max_retry=5,
):
"""
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
:type parts: Union[VideoPart, List[VideoPart]]
:param title: video's title
:type title: str
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
:type tid: int
:param tag: video's tag
:type tag: List[str]
:param desc: video's description
:type desc: str
:param dtime: (optional) publish date timestamp (10 digits Unix timestamp e.g. 1551533438)
:type dtime: int
:param source: (optional) 转载地址
:type source: str
:param cover: (optional) cover's URL, use method *cover_up* to get
:type cover: str
:param no_reprint: (optional) Is reprint allowed
:type no_reprint: bool
:param dynamic: 粉丝动态
:type dynamic: str
:param open_elec: (optional) whether to open charging panel (充电面板)
:type open_elec: bool
:param open_subtitle: (optional) Is uploading subtitles allowed
:type open_subtitle: bool
:param max_retry: (optional) max retry times per chunk
:type max_retry: int
"""
if len(title) > 80:
raise Exception("标题长度超过80字")
if len(source) > 200:
raise Exception("转载地址长度超过200字")
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
if not isinstance(parts, list):
parts = [parts]
# retry by status
retries = Retry(
total=max_retry,
backoff_factor=1,
status_forcelist=(504, ),
)
self.session.mount('https://', HTTPAdapter(max_retries=retries))
self.session.mount('http://', HTTPAdapter(max_retries=retries))
#
videos = []
for part in parts:
filepath = part.path
filename = os.path.basename(filepath)
filesize = os.path.getsize(filepath)
r = self.session.get('https://member.bilibili.com/preupload?'
'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0'
.format(name=parse.quote_plus(filename), size=filesize))
"""return example
{
"upos_uri": "upos://ugc/i181012ws18x52mti3gg0h33chn3tyhp.mp4",
"biz_id": 58993125,
"endpoint": "//upos-hz-upcdnws.acgvideo.com",
"endpoints": [
"//upos-hz-upcdnws.acgvideo.com",
"//upos-hz-upcdntx.acgvideo.com"
],
"chunk_retry_delay": 3,
"chunk_retry": 200,
"chunk_size": 4194304,
"threads": 2,
"timeout": 900,
"auth": "os=upos&cdn=upcdnws&uid=&net_state=4&device=&build=&os_version=&ak=×tamp=&sign=",
"OK": 1
}
"""
json = r.json()
upos_uri = json['upos_uri']
endpoint = json['endpoint']
auth = json['auth']
biz_id = json['biz_id']
chunk_size = json['chunk_size']
self.session.headers['X-Upos-Auth'] = auth # add auth header
r = self.session.post('https:{}/{}?uploads&output=json'.format(endpoint, upos_uri.replace('upos://', '')))
# {"upload_id":"72eb747b9650b8c7995fdb0efbdc2bb6","key":"\/i181012ws2wg1tb7tjzswk2voxrwlk1u.mp4","OK":1,"bucket":"ugc"}
json = r.json()
upload_id = json['upload_id']
with open(filepath, 'rb') as f:
chunks_num = math.ceil(filesize / chunk_size)
chunks_index = -1
while True:
chunks_data = f.read(chunk_size)
if not chunks_data:
break
chunks_index += 1 # start with 0
def upload_chunk():
r = self.session.put('https:{endpoint}/{upos_uri}?'
'partNumber={part_number}&uploadId={upload_id}&chunk={chunk}&chunks={chunks}&size={size}&start={start}&end={end}&total={total}'
.format(endpoint=endpoint,
upos_uri=upos_uri.replace('upos://', ''),
part_number=chunks_index+1, # starts with 1
upload_id=upload_id,
chunk=chunks_index,
chunks=chunks_num,
size=len(chunks_data),
start=chunks_index * chunk_size,
end=chunks_index * chunk_size + len(chunks_data),
total=filesize,
),
chunks_data,
)
return r
def retry_upload_chunk():
"""return :class:`Response` if upload success, else return None."""
for i in range(max_retry):
r = upload_chunk()
if r.status_code == 200:
return r
log.info(r.text)
log.info('{}/{} retry stage {}/{}'.format(chunks_index, chunks_num, i, max_retry))
log.info('sleep %ds', 5 * i)
time.sleep(5 * i)
return None
r = retry_upload_chunk()
if r:
log.info('upload part {}/{}'.format(chunks_index, chunks_num))
else:
raise Exception('upload reach max retry times at part {}/{}'.format(chunks_index, chunks_num))
# NOT DELETE! Refer to https://github.com/comwrg/bilibiliupload/issues/15#issuecomment-424379769
self.session.post('https:{endpoint}/{upos_uri}?'
'output=json&name={name}&profile=ugcupos%2Fyb&uploadId={upload_id}&biz_id={biz_id}'
.format(endpoint=endpoint,
upos_uri=upos_uri.replace('upos://', ''),
name=filename,
upload_id=upload_id,
biz_id=biz_id,
),
{"parts": [{"partNumber": i, "eTag": "etag"} for i in range(1, chunks_num+1)]},
)
videos.append({'filename': upos_uri.replace('upos://ugc/', '').split('.')[0],
'title' : part.title,
'desc' : part.desc})
# if source is empty, copyright=1, else copyright=2
copyright = 2 if source else 1
def add():
r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
json={
"copyright" : copyright,
"source" : source,
"title" : title,
"tid" : tid,
"tag" : ','.join(tag),
"no_reprint": int(no_reprint),
"desc" : desc,
"cover" : cover,
"mission_id": 0,
"order_id" : 0,
"videos" : videos,
"dtime" : dtime,
"open_elec" : int(open_elec),
"dynamic" : dynamic,
"subtitle" : {
"lan" : "",
"open": int(open_subtitle),
},
},
)
return r
def retry_add():
for i in range(max_retry):
r = add()
json = r.json()
code = json['code']
if code == 0:
return r
# {"code":20001,"message":"投稿服务异常","ttl":1}
if code in (20001, ):
log.info('retry add video {}/{}, {}'.format(i, max_retry, r.text))
else:
raise Exception('Fail to add video, {}'.format(r.text))
log.info('sleep %ds', 5 * i)
time.sleep(5 * i)
raise Exception('Add video reach max retry times.')
r = retry_add()
return r.json()
def addChannel(self, name, intro=''):
"""
:param name: channel's name
:type name: str
:param intro: channel's introduction
:type intro: str
"""
r = self.session.post(
url='https://api.bilibili.com/x/space/channel/add',
data={
'name' : name,
'intro': intro,
'jsonp' : 'jsonp',
'csrf' : self.csrf,
},
# name=123&intro=123&aids=&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497077610768
)
# return
# {"status":true,"data":{"cid":"15812"}}
def channel_addVideo(self, cid, aids):
"""
:param cid: channel's id
:type cid: int
:param aids: videos' id
:type aids: list<int>
"""
r = self.session.post(
url='https://api.bilibili.com/x/space/channel/video/add',
data={
'aids': '%2C'.join(aids),
'cid' : cid,
'csrf': self.csrf,
},
# aids=9953555%2C9872953&cid=15814&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497079332679
)
def cover_up(self, img: Union[str, BufferedReader]):
"""
:param img: img path or stream
:return: img URL
"""
if isinstance(img, str):
f = open(img, 'rb')
else:
f = img
self.session.headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = self.session.post(
url='https://member.bilibili.com/x/vu/web/cover/up',
data={
'cover': b'data:image/jpeg;base64,' + (base64.b64encode(f.read())),
'csrf': self.csrf,
},
)
# {"code":0,"data":{"url":"http://i0.hdslb.com/bfs/archive/67db4a6eae398c309244e74f6e85ae8d813bd7c9.jpg"},"message":"","ttl":1}
return r.json()['data']['url']