-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbili_web_api.py
654 lines (603 loc) · 26.6 KB
/
bili_web_api.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
import asyncio
import base64
import hashlib
import json
import math
import os
import sys
import time
import urllib
from dataclasses import dataclass, asdict, field, InitVar
from json import JSONDecodeError
from os.path import splitext, basename
from typing import Union, Any
from urllib import parse
from urllib.parse import quote
from PIL import Image
from io import BytesIO
import aiohttp
import requests
import requests.utils
import rsa
from requests.adapters import HTTPAdapter
from urllib3 import Retry
import xml.etree.ElementTree as ET
# From https://github.com/biliup/biliup/blob/c11324a133b10db8c3f3c2c7f87ee295034e4375/biliup/plugins/bili_webup.py
class BiliBili:
def __init__(self, video: 'Data'):
self.app_key = None
self.appsec = None
if self.app_key is None or self.appsec is None:
self.app_key = 'ae57252b0c09105d'
self.appsec = 'c75875c596a69eb55bd119e74b07cfe3'
self.__session = requests.Session()
self.video = video
self.__session.mount('https://', HTTPAdapter(max_retries=Retry(total=5)))
self.__session.headers.update({
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108",
"Referer": "https://www.bilibili.com/", 'Connection': 'keep-alive'
})
self.cookies = None
self.access_token = None
self.refresh_token = None
self.account = None
self.__bili_jct = None
self._auto_os = None
self.persistence_path = 'engine/bili.cookie'
def check_tag(self, tag):
r = self.__session.get("https://member.bilibili.com/x/vupre/web/topic/tag/check?tag=" + tag).json()
if r["code"] == 0:
return True
else:
return False
def get_qrcode(self):
params = {
"appkey": "4409e2ce8ffd12b8",
"local_id": "0",
"ts": int(time.time()),
}
params["sign"] = hashlib.md5(
f"{urllib.parse.urlencode(params)}59b43e04ad6965f34319062b478f83dd".encode()).hexdigest()
response = self.__session.post("http://passport.bilibili.com/x/passport-tv-login/qrcode/auth_code", data=params,
timeout=5)
r = response.json()
if r and r["code"] == 0:
return r
async def login_by_qrcode(self, value):
params = {
"appkey": "4409e2ce8ffd12b8",
"auth_code": value["data"]["auth_code"],
"local_id": "0",
"ts": int(time.time()),
}
params["sign"] = hashlib.md5(
f"{urllib.parse.urlencode(params)}59b43e04ad6965f34319062b478f83dd".encode()).hexdigest()
for i in range(0, 120):
await asyncio.sleep(1)
response = self.__session.post("http://passport.bilibili.com/x/passport-tv-login/qrcode/poll", data=params,
timeout=5)
r = response.json()
if r and r["code"] == 0:
return r
raise "Qrcode timeout"
def tid_archive(self, cookies):
requests.utils.add_dict_to_cookiejar(self.__session.cookies, cookies)
response = self.__session.get("https://member.bilibili.com/x/vupre/web/archive/pre")
return response.json()
def login(self, persistence_path, user):
self.persistence_path = persistence_path
if os.path.isfile(persistence_path):
print('使用持久化内容上传')
self.load()
if user.get('cookies'):
self.cookies = user['cookies']
if user.get('access_token'):
self.access_token = user['access_token']
if user.get('account'):
self.account = user['account']
if self.cookies:
try:
self.login_by_cookies(self.cookies)
except:
print('login error')
self.login_by_password(**self.account)
else:
self.login_by_password(**self.account)
self.store()
def load(self):
try:
with open(self.persistence_path) as f:
self.cookies = json.load(f)
self.access_token = self.cookies['access_token']
except (JSONDecodeError, KeyError):
print('加载cookie出错')
def store(self):
with open(self.persistence_path, "w") as f:
json.dump({**self.cookies,
'access_token': self.access_token,
'refresh_token': self.refresh_token
}, f)
def send_sms(self, phone_number, country_code):
params = {
"actionKey": "appkey",
"appkey": "783bbb7264451d82",
"build": 6510400,
"channel": "bili",
"cid": country_code,
"device": "phone",
"mobi_app": "android",
"platform": "android",
"tel": phone_number,
"ts": int(time.time()),
}
sign = hashlib.md5(f"{urllib.parse.urlencode(params)}2653583c8873dea268ab9386918b1d65".encode()).hexdigest()
payload = f"{urllib.parse.urlencode(params)}&sign={sign}"
response = self.__session.post("https://passport.bilibili.com/x/passport-login/sms/send", data=payload,
timeout=5)
return response.json()
def login_by_sms(self, code, params):
params["code"] = code
params["sign"] = hashlib.md5(
f"{urllib.parse.urlencode(params)}59b43e04ad6965f34319062b478f83dd".encode()).hexdigest()
response = self.__session.post("https://passport.bilibili.com/x/passport-login/login/sms", data=params,
timeout=5)
r = response.json()
if r and r["code"] == 0:
return r
def login_by_password(self, username, password):
print('使用账号上传')
key_hash, pub_key = self.get_key()
encrypt_password = base64.b64encode(rsa.encrypt(f'{key_hash}{password}'.encode(), pub_key))
payload = {
"actionKey": 'appkey',
"appkey": self.app_key,
"build": 6270200,
"captcha": '',
"challenge": '',
"channel": 'bili',
"device": 'phone',
"mobi_app": 'android',
"password": encrypt_password,
"permission": 'ALL',
"platform": 'android',
"seccode": "",
"subid": 1,
"ts": int(time.time()),
"username": username,
"validate": "",
}
response = self.__session.post("https://passport.bilibili.com/x/passport-login/oauth2/login", timeout=5,
data={**payload, 'sign': self.sign(parse.urlencode(payload))})
r = response.json()
if r['code'] != 0 or r.get('data') is None or r['data'].get('cookie_info') is None:
raise RuntimeError(r)
try:
for cookie in r['data']['cookie_info']['cookies']:
self.__session.cookies.set(cookie['name'], cookie['value'])
if 'bili_jct' == cookie['name']:
self.__bili_jct = cookie['value']
self.cookies = self.__session.cookies.get_dict()
self.access_token = r['data']['token_info']['access_token']
self.refresh_token = r['data']['token_info']['refresh_token']
except:
raise RuntimeError(r)
return r
def login_by_cookies(self, cookie):
print('使用cookies上传')
requests.utils.add_dict_to_cookiejar(self.__session.cookies, cookie)
if 'bili_jct' in cookie:
self.__bili_jct = cookie["bili_jct"]
data = self.__session.get("https://api.bilibili.com/x/web-interface/nav", timeout=5).json()
if data["code"] != 0:
raise Exception(data)
def sign(self, param):
return hashlib.md5(f"{param}{self.appsec}".encode()).hexdigest()
def get_key(self):
url = "https://passport.bilibili.com/x/passport-login/web/key"
payload = {
'appkey': f'{self.app_key}',
'sign': self.sign(f"appkey={self.app_key}"),
}
response = self.__session.get(url, data=payload, timeout=5)
r = response.json()
if r and r["code"] == 0:
return r['data']['hash'], rsa.PublicKey.load_pkcs1_openssl_pem(r['data']['key'].encode())
def probe(self):
ret = self.__session.get('https://member.bilibili.com/preupload?r=probe', timeout=5).json()
print(f"线路:{ret['lines']}")
data, auto_os = None, None
min_cost = 0
if ret['probe'].get('get'):
method = 'get'
else:
method = 'post'
data = bytes(int(1024 * 0.1 * 1024))
for line in ret['lines']:
start = time.perf_counter()
test = self.__session.request(method, f"https:{line['probe_url']}", data=data, timeout=30)
cost = time.perf_counter() - start
print(line['query'], cost)
if test.status_code != 200:
return
if not min_cost or min_cost > cost:
auto_os = line
min_cost = cost
auto_os['cost'] = min_cost
return auto_os
def upload_file(self, filepath: str, lines='AUTO', tasks=3):
"""上传本地视频文件,返回视频信息dict
b站目前支持4种上传线路upos, kodo, gcs, bos
gcs: {"os":"gcs","query":"bucket=bvcupcdngcsus&probe_version=20221109",
"probe_url":"//storage.googleapis.com/bvcupcdngcsus/OK"},
bos: {"os":"bos","query":"bucket=bvcupcdnboshb&probe_version=20221109",
"probe_url":"??"}
"""
if not self._auto_os:
if lines == 'kodo':
self._auto_os = {"os": "kodo", "query": "bucket=bvcupcdnkodobm&probe_version=20221109",
"probe_url": "//up-na0.qbox.me/crossdomain.xml"}
elif lines == 'bda2':
self._auto_os = {"os": "upos", "query": "upcdn=bda2&probe_version=20221109",
"probe_url": "//upos-sz-upcdnbda2.bilivideo.com/OK"}
elif lines == 'cs-bda2':
self._auto_os = {"os": "upos", "query": "upcdn=bda2&probe_version=20221109",
"probe_url": "//upos-cs-upcdnbda2.bilivideo.com/OK"}
elif lines == 'bldsa':
self._auto_os = {"os": "upos", "query": "upcdn=bldsa&probe_version=20221109",
"probe_url": "//upos-cs-upcdnbldsa.bilivideo.com/OK"}
elif lines == 'ws':
self._auto_os = {"os": "upos", "query": "upcdn=ws&probe_version=20221109",
"probe_url": "//upos-sz-upcdnws.bilivideo.com/OK"}
elif lines == 'qn':
self._auto_os = {"os": "upos", "query": "upcdn=qn&probe_version=20221109",
"probe_url": "//upos-sz-upcdnqn.bilivideo.com/OK"}
elif lines == 'cs-qn':
self._auto_os = {"os": "upos", "query": "upcdn=qn&probe_version=20221109",
"probe_url": "//upos-cs-upcdnqn.bilivideo.com/OK"}
elif lines == 'cos':
self._auto_os = {"os": "cos", "query": "",
"probe_url": ""}
elif lines == 'cos-internal':
self._auto_os = {"os": "cos-internal", "query": "",
"probe_url": ""}
else:
self._auto_os = self.probe()
print(f"线路选择 => {self._auto_os['os']}: {self._auto_os['query']}. time: {self._auto_os.get('cost')}")
if self._auto_os['os'] == 'upos':
upload = self.upos
elif self._auto_os['os'] == 'cos':
upload = self.cos
elif self._auto_os['os'] == 'cos-internal':
upload = lambda *args, **kwargs: self.cos(*args, **kwargs, internal=True)
elif self._auto_os['os'] == 'kodo':
upload = self.kodo
else:
print(f"NoSearch:{self._auto_os['os']}")
raise NotImplementedError(self._auto_os['os'])
print(f"os: {self._auto_os['os']}")
total_size = os.path.getsize(filepath)
with open(filepath, 'rb') as f:
query = {
'r': self._auto_os['os'] if self._auto_os['os'] != 'cos-internal' else 'cos',
'profile': 'ugcupos/bup' if 'upos' == self._auto_os['os'] else "ugcupos/bupfetch",
'ssl': 0,
'version': '2.8.12',
'build': 2081200,
'name': f.name,
'size': total_size,
}
ret = self.__session.get(
f"https://member.bilibili.com/preupload?{self._auto_os['query']}", params=query,
timeout=5)
return asyncio.run(upload(f, total_size, ret.json(), tasks=tasks))
async def cos(self, file, total_size, ret, chunk_size=10485760, tasks=3, internal=False):
filename = file.name
url = ret["url"]
if internal:
url = url.replace("cos.accelerate", "cos-internal.ap-shanghai")
biz_id = ret["biz_id"]
post_headers = {
"Authorization": ret["post_auth"],
}
put_headers = {
"Authorization": ret["put_auth"],
}
initiate_multipart_upload_result = ET.fromstring(self.__session.post(f'{url}?uploads&output=json', timeout=5,
headers=post_headers).content)
upload_id = initiate_multipart_upload_result.find('UploadId').text
# 开始上传
parts = [] # 分块信息
chunks = math.ceil(total_size / chunk_size) # 获取分块数量
async def upload_chunk(session, chunks_data, params):
async with session.put(url, params=params, raise_for_status=True,
data=chunks_data, headers=put_headers) as r:
end = time.perf_counter() - start
parts.append({"Part": {"PartNumber": params['chunk'] + 1, "ETag": r.headers['Etag']}})
sys.stdout.write(f"\r{params['end'] / 1000 / 1000 / end:.2f}MB/s "
f"=> {params['partNumber'] / chunks:.1%}")
start = time.perf_counter()
await self._upload({
'uploadId': upload_id,
'chunks': chunks,
'total': total_size
}, file, chunk_size, upload_chunk, tasks=tasks)
cost = time.perf_counter() - start
fetch_headers = {
"X-Upos-Fetch-Source": ret["fetch_headers"]["X-Upos-Fetch-Source"],
"X-Upos-Auth": ret["fetch_headers"]["X-Upos-Auth"],
"Fetch-Header-Authorization": ret["fetch_headers"]["Fetch-Header-Authorization"]
}
parts = sorted(parts, key=lambda x: x['Part']['PartNumber'])
complete_multipart_upload = ET.Element('CompleteMultipartUpload')
for part in parts:
part_et = ET.SubElement(complete_multipart_upload, 'Part')
part_number = ET.SubElement(part_et, 'PartNumber')
part_number.text = str(part['Part']['PartNumber'])
e_tag = ET.SubElement(part_et, 'ETag')
e_tag.text = part['Part']['ETag']
xml = ET.tostring(complete_multipart_upload)
ii = 0
while ii <= 3:
try:
res = self.__session.post(url, params={'uploadId': upload_id}, data=xml, headers=post_headers,
timeout=15)
if res.status_code == 200:
break
raise IOError(res.text)
except IOError:
ii += 1
print("请求合并分片出现问题,尝试重连,次数:" + str(ii))
time.sleep(15)
ii = 0
while ii <= 3:
try:
res = self.__session.post("https:" + ret["fetch_url"], headers=fetch_headers, timeout=15).json()
if res.get('OK') == 1:
print(f'{filename} uploaded >> {total_size / 1000 / 1000 / cost:.2f}MB/s. {res}')
return {"title": splitext(filename)[0], "filename": ret["bili_filename"], "desc": ""}
raise IOError(res)
except IOError:
ii += 1
print("上传出现问题,尝试重连,次数:" + str(ii))
time.sleep(15)
async def kodo(self, file, total_size, ret, chunk_size=4194304, tasks=3):
filename = file.name
bili_filename = ret['bili_filename']
key = ret['key']
endpoint = f"https:{ret['endpoint']}"
token = ret['uptoken']
fetch_url = ret['fetch_url']
fetch_headers = ret['fetch_headers']
url = f'{endpoint}/mkblk'
headers = {
'Authorization': f"UpToken {token}",
}
# 开始上传
parts = [] # 分块信息
chunks = math.ceil(total_size / chunk_size) # 获取分块数量
async def upload_chunk(session, chunks_data, params):
async with session.post(f'{url}/{len(chunks_data)}',
data=chunks_data, headers=headers) as response:
end = time.perf_counter() - start
ctx = await response.json()
parts.append({"index": params['chunk'], "ctx": ctx['ctx']})
sys.stdout.write(f"\r{params['end'] / 1000 / 1000 / end:.2f}MB/s "
f"=> {params['partNumber'] / chunks:.1%}")
start = time.perf_counter()
await self._upload({}, file, chunk_size, upload_chunk, tasks=tasks)
cost = time.perf_counter() - start
print(f'{filename} uploaded >> {total_size / 1000 / 1000 / cost:.2f}MB/s')
parts.sort(key=lambda x: x['index'])
self.__session.post(f"{endpoint}/mkfile/{total_size}/key/{base64.urlsafe_b64encode(key.encode()).decode()}",
data=','.join(map(lambda x: x['ctx'], parts)), headers=headers, timeout=10)
r = self.__session.post(f"https:{fetch_url}", headers=fetch_headers, timeout=5).json()
if r["OK"] != 1:
raise Exception(r)
return {"title": splitext(filename)[0], "filename": bili_filename, "desc": ""}
async def upos(self, file, total_size, ret, tasks=3):
filename = file.name
chunk_size = ret['chunk_size']
auth = ret["auth"]
endpoint = ret["endpoint"]
biz_id = ret["biz_id"]
upos_uri = ret["upos_uri"]
url = f"https:{endpoint}/{upos_uri.replace('upos://', '')}" # 视频上传路径
headers = {
"X-Upos-Auth": auth
}
# 向上传地址申请上传,得到上传id等信息
upload_id = self.__session.post(f'{url}?uploads&output=json', timeout=5,
headers=headers).json()["upload_id"]
# 开始上传
parts = [] # 分块信息
chunks = math.ceil(total_size / chunk_size) # 获取分块数量
async def upload_chunk(session, chunks_data, params):
async with session.put(url, params=params, raise_for_status=True,
data=chunks_data, headers=headers):
end = time.perf_counter() - start
parts.append({"partNumber": params['chunk'] + 1, "eTag": "etag"})
sys.stdout.write(f"\r{params['end'] / 1000 / 1000 / end:.2f}MB/s "
f"=> {params['partNumber'] / chunks:.1%}")
start = time.perf_counter()
await self._upload({
'uploadId': upload_id,
'chunks': chunks,
'total': total_size
}, file, chunk_size, upload_chunk, tasks=tasks)
cost = time.perf_counter() - start
p = {
'name': filename,
'uploadId': upload_id,
'biz_id': biz_id,
'output': 'json',
'profile': 'ugcupos/bup'
}
ii = 0
while ii <= 3:
try:
r = self.__session.post(url, params=p, json={"parts": parts}, headers=headers, timeout=15).json()
if r.get('OK') == 1:
print(f'{filename} uploaded >> {total_size / 1000 / 1000 / cost:.2f}MB/s. {r}')
return {"title": splitext(filename)[0], "filename": splitext(basename(upos_uri))[0], "desc": ""}
raise IOError(r)
except IOError:
ii += 1
print("上传出现问题,尝试重连,次数:" + str(ii))
time.sleep(15)
@staticmethod
async def _upload(params, file, chunk_size, afunc, tasks=3):
params['chunk'] = -1
async def upload_chunk():
while True:
chunks_data = file.read(chunk_size)
if not chunks_data:
return
params['chunk'] += 1
params['size'] = len(chunks_data)
params['partNumber'] = params['chunk'] + 1
params['start'] = params['chunk'] * chunk_size
params['end'] = params['start'] + params['size']
clone = params.copy()
for i in range(10):
try:
await afunc(session, chunks_data, clone)
break
except (asyncio.TimeoutError, aiohttp.ClientError) as e:
print(f"retry chunk{clone['chunk']} >> {i + 1}. {e}")
async with aiohttp.ClientSession() as session:
await asyncio.gather(*[upload_chunk() for _ in range(tasks)])
def submit(self, submit_api=None):
if not self.video.title:
self.video.title = self.video.videos[0]["title"]
self.__session.get('https://member.bilibili.com/x/geetest/pre/add', timeout=5)
if submit_api is None:
total_info = self.__session.get('http://api.bilibili.com/x/space/myinfo', timeout=15).json()
if total_info.get('data') is None:
print(total_info)
total_info = total_info.get('data')
if total_info['level'] > 3 and total_info['follower'] > 1000:
user_weight = 2
else:
user_weight = 1
print(f'用户权重: {user_weight}')
submit_api = 'web' if user_weight == 2 else 'client'
ret = None
if submit_api == 'web':
ret = self.submit_web()
if ret["code"] == 21138:
print(f'改用客户端接口提交{ret}')
submit_api = 'client'
if submit_api == 'client':
ret = self.submit_client()
if not ret:
raise Exception(f'不存在的选项:{submit_api}')
if ret["code"] == 0:
return ret
else:
raise Exception(ret)
def submit_web(self):
print('使用网页端api提交')
return self.__session.post(f'https://member.bilibili.com/x/vu/web/add?csrf={self.__bili_jct}', timeout=5,
json=asdict(self.video)).json()
def submit_client(self):
print('使用客户端api端提交')
if not self.access_token:
if self.account is None:
raise RuntimeError("Access token is required, but account and access_token does not exist!")
self.login_by_password(**self.account)
self.store()
while True:
ret = self.__session.post(f'http://member.bilibili.com/x/vu/client/add?access_key={self.access_token}',
timeout=5, json=asdict(self.video)).json()
if ret['code'] == -101:
print(f'刷新token{ret}')
self.login_by_password(**config['user']['account'])
self.store()
continue
return ret
def cover_up(self, img: str):
"""
:param img: img path or stream
:return: img URL
"""
with Image.open(img) as im:
# 宽和高,需要16:10
xsize, ysize = im.size
if xsize / ysize > 1.6:
delta = xsize - ysize * 1.6
region = im.crop((int(delta / 2), 0, int(xsize - delta / 2), ysize))
else:
delta = ysize - xsize * 10 / 16
region = im.crop((0, int(delta / 2), xsize, int(ysize - delta / 2)))
buffered = BytesIO()
region.save(buffered, format=im.format)
r = self.__session.post(
url='https://member.bilibili.com/x/vu/web/cover/up',
data={
'cover': b'data:image/jpeg;base64,' + (base64.b64encode(buffered.getvalue())),
'csrf': self.__bili_jct
}, timeout=30
)
buffered.close()
res = r.json()
if res.get('data') is None:
raise Exception(res)
return res['data']['url']
def get_tags(self, upvideo, typeid="", desc="", cover="", groupid=1, vfea=""):
"""
上传视频后获得推荐标签
:param vfea:
:param groupid:
:param typeid:
:param desc:
:param cover:
:param upvideo:
:return: 返回官方推荐的tag
"""
url = f'https://member.bilibili.com/x/web/archive/tags?' \
f'typeid={typeid}&title={quote(upvideo["title"])}&filename=filename&desc={desc}&cover={cover}' \
f'&groupid={groupid}&vfea={vfea}'
return self.__session.get(url=url, timeout=5).json()
def __enter__(self):
return self
def __exit__(self, e_t, e_v, t_b):
self.close()
def close(self):
"""Closes all adapters and as such the session"""
self.__session.close()
@dataclass
class Data:
"""
cover: 封面图片,可由recovers方法得到视频的帧截图
"""
copyright: int = 2
source: str = ''
tid: int = 21
cover: str = ''
title: str = ''
desc_format_id: int = 0
desc: str = ''
dynamic: str = ''
subtitle: dict = field(init=False)
tag: Union[list, str] = ''
videos: list = field(default_factory=list)
dtime: Any = None
open_subtitle: InitVar[bool] = False
# interactive: int = 0
# no_reprint: int 1
# open_elec: int 1
def __post_init__(self, open_subtitle):
self.subtitle = {"open": int(open_subtitle), "lan": ""}
if self.dtime and self.dtime - int(time.time()) <= 14400:
self.dtime = None
if isinstance(self.tag, list):
self.tag = ','.join(self.tag)
def delay_time(self, dtime: int):
"""设置延时发布时间,距离提交大于2小时,格式为10位时间戳"""
if dtime - int(time.time()) > 7200:
self.dtime = dtime
def set_tag(self, tag: list):
"""设置标签,tag为数组"""
self.tag = ','.join(tag)
def append(self, video):
self.videos.append(video)