Spaces:
Paused
Paused
update
Browse files
data/porter_tasks/porter_task_chenjieshen_douyin_video_to_bilibili.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
| 6 |
"sec_user_id": "MS4wLjABAAAATGoBrO7yiJ3q9go4fxq9JXjrnP1bFpdkgKckC1IpfXA_vrjSmL9ZtjmTju8ApwbT",
|
| 7 |
"check_interval": 900,
|
| 8 |
"key_of_credentials": "douyin_wentao_credentials",
|
| 9 |
-
"min_date2": "2025-09-
|
| 10 |
"output_video_dir": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森",
|
| 11 |
"output_video_info_file": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森/file_info.json"
|
| 12 |
},
|
|
@@ -17,7 +17,7 @@
|
|
| 17 |
"sec_user_id": "MS4wLjABAAAA49QFP6YhorLIIX9M-FiZeKxmqhqXlttluSsZeaxvxzU",
|
| 18 |
"check_interval": 900,
|
| 19 |
"key_of_credentials": "douyin_wentao_credentials",
|
| 20 |
-
"min_date2": "2025-09-
|
| 21 |
"output_video_dir": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森",
|
| 22 |
"output_video_info_file": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森/file_info.json"
|
| 23 |
},
|
|
@@ -31,6 +31,6 @@
|
|
| 31 |
"target_user_id": "442286660",
|
| 32 |
"key_of_credentials": "bilibili_chenjiesen_credentials",
|
| 33 |
"remove_after_upload": true,
|
| 34 |
-
"
|
| 35 |
}
|
| 36 |
]
|
|
|
|
| 6 |
"sec_user_id": "MS4wLjABAAAATGoBrO7yiJ3q9go4fxq9JXjrnP1bFpdkgKckC1IpfXA_vrjSmL9ZtjmTju8ApwbT",
|
| 7 |
"check_interval": 900,
|
| 8 |
"key_of_credentials": "douyin_wentao_credentials",
|
| 9 |
+
"min_date2": "2025-09-12 00:00:00",
|
| 10 |
"output_video_dir": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森",
|
| 11 |
"output_video_info_file": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森/file_info.json"
|
| 12 |
},
|
|
|
|
| 17 |
"sec_user_id": "MS4wLjABAAAA49QFP6YhorLIIX9M-FiZeKxmqhqXlttluSsZeaxvxzU",
|
| 18 |
"check_interval": 900,
|
| 19 |
"key_of_credentials": "douyin_wentao_credentials",
|
| 20 |
+
"min_date2": "2025-09-12 00:00:00",
|
| 21 |
"output_video_dir": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森",
|
| 22 |
"output_video_info_file": "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森/file_info.json"
|
| 23 |
},
|
|
|
|
| 31 |
"target_user_id": "442286660",
|
| 32 |
"key_of_credentials": "bilibili_chenjiesen_credentials",
|
| 33 |
"remove_after_upload": true,
|
| 34 |
+
"min_date2": "2025-09-12 00:00:00"
|
| 35 |
}
|
| 36 |
]
|
toolbox/bilibili/video/video_manager.py
CHANGED
|
@@ -5,6 +5,7 @@ https://pypi.org/project/biliupload/
|
|
| 5 |
https://github.com/SocialSisterYi/bilibili-API-collect
|
| 6 |
"""
|
| 7 |
import argparse
|
|
|
|
| 8 |
import logging
|
| 9 |
import math
|
| 10 |
from pathlib import Path
|
|
@@ -12,6 +13,7 @@ import re
|
|
| 12 |
|
| 13 |
logger = logging.getLogger("toolbox")
|
| 14 |
|
|
|
|
| 15 |
from tenacity import before_sleep_log, retry, retry_if_exception_type, stop_after_attempt, wait_fixed
|
| 16 |
from tqdm import tqdm
|
| 17 |
|
|
@@ -28,7 +30,7 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 28 |
stop=stop_after_attempt(3),
|
| 29 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 30 |
)
|
| 31 |
-
def
|
| 32 |
url = "https://member.bilibili.com/preupload"
|
| 33 |
params = {
|
| 34 |
"name": filename,
|
|
@@ -42,12 +44,17 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 42 |
"upcdn": "bda2",
|
| 43 |
"probe_version": "20200810"
|
| 44 |
}
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
params=params,
|
| 48 |
-
|
| 49 |
)
|
| 50 |
-
|
| 51 |
if response.status_code != 200:
|
| 52 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 53 |
js = response.json()
|
|
@@ -60,11 +67,16 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 60 |
stop=stop_after_attempt(3),
|
| 61 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 62 |
)
|
| 63 |
-
def get_upload_video_id(self,
|
| 64 |
url = f"https://upos-sz-upcdnbda2.bilivideo.com/{upos_uri}?uploads&output=json"
|
| 65 |
-
response =
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if response.status_code != 200:
|
|
@@ -74,11 +86,11 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 74 |
raise AssertionError(f"request failed;")
|
| 75 |
return js
|
| 76 |
|
| 77 |
-
def upload_video_in_chunks(self,
|
| 78 |
"""
|
| 79 |
Upload the video in chunks.
|
| 80 |
-
:param upos_uri: str, get from `
|
| 81 |
-
:param auth: str, get from `
|
| 82 |
:param upload_id: str, get from `get_upload_video_id`
|
| 83 |
:param fileio: io.BufferedReader, the io stream of the video to be uploaded
|
| 84 |
:param filesize: int, the size of the video to be uploaded
|
|
@@ -107,11 +119,16 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 107 |
params["size"] = len(batchbytes)
|
| 108 |
params["start"] = start
|
| 109 |
params["end"] = fileio.tell()
|
| 110 |
-
response =
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
params=params,
|
| 113 |
data=batchbytes,
|
| 114 |
-
|
| 115 |
)
|
| 116 |
if response.status_code != 200:
|
| 117 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
@@ -122,17 +139,7 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 122 |
stop=stop_after_attempt(3),
|
| 123 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 124 |
)
|
| 125 |
-
def finish_upload(self,
|
| 126 |
-
"""
|
| 127 |
-
Notify the all chunks have been uploaded.
|
| 128 |
-
:param upos_uri: str, get from `preupload`
|
| 129 |
-
:param auth: str, get from `preupload`
|
| 130 |
-
:param filename: str, the name of the video to be uploaded
|
| 131 |
-
:param upload_id: str, get from `get_upload_video_id`
|
| 132 |
-
:param biz_id: int, get from `preupload`
|
| 133 |
-
:param chunks: int, the number of chunks to be uploaded
|
| 134 |
-
:return:
|
| 135 |
-
"""
|
| 136 |
url = f"https://upos-sz-upcdnbda2.bilivideo.com/{upos_uri}"
|
| 137 |
params = {
|
| 138 |
"output": "json",
|
|
@@ -147,11 +154,16 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 147 |
for i in range(chunks, 1)
|
| 148 |
]
|
| 149 |
}
|
| 150 |
-
response =
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
params=params,
|
| 153 |
json=data,
|
| 154 |
-
|
| 155 |
)
|
| 156 |
if response.status_code != 200:
|
| 157 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
@@ -180,13 +192,13 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 180 |
"videos": [
|
| 181 |
{
|
| 182 |
"filename": bilibili_filename,
|
| 183 |
-
"title": metadata["title"],
|
| 184 |
"desc": metadata["desc"]
|
| 185 |
}
|
| 186 |
],
|
| 187 |
"source": metadata["source"],
|
| 188 |
"tid": metadata["tid"],
|
| 189 |
-
"title": metadata["title"],
|
| 190 |
"cover": metadata["cover"],
|
| 191 |
"tag": metadata["tag"],
|
| 192 |
"desc_format_id": 0,
|
|
@@ -201,10 +213,15 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 201 |
# copyright: 1 original 2 reprint
|
| 202 |
data["copyright"] = 1
|
| 203 |
|
| 204 |
-
response =
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
json=data,
|
| 207 |
-
|
| 208 |
)
|
| 209 |
if response.status_code != 200:
|
| 210 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
@@ -215,7 +232,7 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 215 |
filename = Path(filename)
|
| 216 |
filesize = filename.stat().st_size
|
| 217 |
|
| 218 |
-
js = self.
|
| 219 |
upos_uri = js["upos_uri"].split("//")[-1]
|
| 220 |
auth = js["auth"]
|
| 221 |
biz_id = js["biz_id"]
|
|
@@ -229,6 +246,7 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 229 |
bilibili_filename = re.search(r"/(.*)\.", key).group(1)
|
| 230 |
fileio = filename.open(mode="rb")
|
| 231 |
self.upload_video_in_chunks(
|
|
|
|
| 232 |
upos_uri=upos_uri,
|
| 233 |
auth=auth,
|
| 234 |
upload_id=upload_id,
|
|
@@ -253,32 +271,349 @@ class BilibiliVideoUploader20200810(BilibiliClient):
|
|
| 253 |
|
| 254 |
bvid = None
|
| 255 |
status_code = publish_video_response["code"]
|
| 256 |
-
if status_code
|
|
|
|
|
|
|
| 257 |
message = publish_video_response["message"]
|
| 258 |
logger.info(f"publish_video failed; code: {status_code}, message: {message}")
|
| 259 |
else:
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
return bvid
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
class BilibiliVideoUploader20221109(BilibiliVideoUploader20200810):
|
| 265 |
def __init__(self):
|
| 266 |
super().__init__()
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
BilibiliVideoUploader = BilibiliVideoUploader20200810
|
|
|
|
| 270 |
|
| 271 |
|
| 272 |
def get_args():
|
| 273 |
parser = argparse.ArgumentParser()
|
| 274 |
parser.add_argument(
|
| 275 |
-
"--
|
| 276 |
-
default=
|
| 277 |
type=str
|
| 278 |
)
|
| 279 |
parser.add_argument(
|
| 280 |
"--filename",
|
| 281 |
-
default=(project_path / "data/video/douyin/陈杰森/
|
| 282 |
type=str
|
| 283 |
)
|
| 284 |
args = parser.parse_args()
|
|
@@ -287,7 +622,7 @@ def get_args():
|
|
| 287 |
|
| 288 |
def main():
|
| 289 |
import log
|
| 290 |
-
from project_settings import project_path, log_directory
|
| 291 |
|
| 292 |
log.setup_size_rotating(log_directory=log_directory)
|
| 293 |
|
|
@@ -297,7 +632,8 @@ def main():
|
|
| 297 |
|
| 298 |
flag = client.check_login()
|
| 299 |
print(f"flag: {flag}")
|
| 300 |
-
|
|
|
|
| 301 |
flag = client.check_login()
|
| 302 |
print(f"flag: {flag}")
|
| 303 |
|
|
|
|
| 5 |
https://github.com/SocialSisterYi/bilibili-API-collect
|
| 6 |
"""
|
| 7 |
import argparse
|
| 8 |
+
import json
|
| 9 |
import logging
|
| 10 |
import math
|
| 11 |
from pathlib import Path
|
|
|
|
| 13 |
|
| 14 |
logger = logging.getLogger("toolbox")
|
| 15 |
|
| 16 |
+
import requests
|
| 17 |
from tenacity import before_sleep_log, retry, retry_if_exception_type, stop_after_attempt, wait_fixed
|
| 18 |
from tqdm import tqdm
|
| 19 |
|
|
|
|
| 30 |
stop=stop_after_attempt(3),
|
| 31 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 32 |
)
|
| 33 |
+
def pre_upload(self, filename, filesize):
|
| 34 |
url = "https://member.bilibili.com/preupload"
|
| 35 |
params = {
|
| 36 |
"name": filename,
|
|
|
|
| 44 |
"upcdn": "bda2",
|
| 45 |
"probe_version": "20200810"
|
| 46 |
}
|
| 47 |
+
|
| 48 |
+
response = requests.request(
|
| 49 |
+
method="POST",
|
| 50 |
+
url=url,
|
| 51 |
+
headers={
|
| 52 |
+
"TE": "Trailers",
|
| 53 |
+
**self.headers,
|
| 54 |
+
},
|
| 55 |
params=params,
|
| 56 |
+
cookies=self.cookies,
|
| 57 |
)
|
|
|
|
| 58 |
if response.status_code != 200:
|
| 59 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 60 |
js = response.json()
|
|
|
|
| 67 |
stop=stop_after_attempt(3),
|
| 68 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 69 |
)
|
| 70 |
+
def get_upload_video_id(self, upos_uri, auth, **kwargs):
|
| 71 |
url = f"https://upos-sz-upcdnbda2.bilivideo.com/{upos_uri}?uploads&output=json"
|
| 72 |
+
response = requests.request(
|
| 73 |
+
method="POST",
|
| 74 |
+
url=url,
|
| 75 |
+
headers={
|
| 76 |
+
"X-Upos-Auth": auth,
|
| 77 |
+
**self.headers,
|
| 78 |
+
},
|
| 79 |
+
cookies=self.cookies,
|
| 80 |
)
|
| 81 |
|
| 82 |
if response.status_code != 200:
|
|
|
|
| 86 |
raise AssertionError(f"request failed;")
|
| 87 |
return js
|
| 88 |
|
| 89 |
+
def upload_video_in_chunks(self, endpoint, upos_uri, auth, upload_id, fileio, filesize, chunk_size, chunks):
|
| 90 |
"""
|
| 91 |
Upload the video in chunks.
|
| 92 |
+
:param upos_uri: str, get from `pre_upload`
|
| 93 |
+
:param auth: str, get from `pre_upload`
|
| 94 |
:param upload_id: str, get from `get_upload_video_id`
|
| 95 |
:param fileio: io.BufferedReader, the io stream of the video to be uploaded
|
| 96 |
:param filesize: int, the size of the video to be uploaded
|
|
|
|
| 119 |
params["size"] = len(batchbytes)
|
| 120 |
params["start"] = start
|
| 121 |
params["end"] = fileio.tell()
|
| 122 |
+
response = requests.request(
|
| 123 |
+
method="PUT",
|
| 124 |
+
url=url,
|
| 125 |
+
headers={
|
| 126 |
+
"X-Upos-Auth": auth,
|
| 127 |
+
**self.headers,
|
| 128 |
+
},
|
| 129 |
params=params,
|
| 130 |
data=batchbytes,
|
| 131 |
+
cookies=self.cookies,
|
| 132 |
)
|
| 133 |
if response.status_code != 200:
|
| 134 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
|
|
| 139 |
stop=stop_after_attempt(3),
|
| 140 |
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 141 |
)
|
| 142 |
+
def finish_upload(self, upos_uri, auth, filename, upload_id, biz_id, chunks):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
url = f"https://upos-sz-upcdnbda2.bilivideo.com/{upos_uri}"
|
| 144 |
params = {
|
| 145 |
"output": "json",
|
|
|
|
| 154 |
for i in range(chunks, 1)
|
| 155 |
]
|
| 156 |
}
|
| 157 |
+
response = requests.request(
|
| 158 |
+
method="POST",
|
| 159 |
+
url=url,
|
| 160 |
+
headers={
|
| 161 |
+
"X-Upos-Auth": auth,
|
| 162 |
+
**self.headers,
|
| 163 |
+
},
|
| 164 |
params=params,
|
| 165 |
json=data,
|
| 166 |
+
cookies=self.cookies,
|
| 167 |
)
|
| 168 |
if response.status_code != 200:
|
| 169 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
|
|
| 192 |
"videos": [
|
| 193 |
{
|
| 194 |
"filename": bilibili_filename,
|
| 195 |
+
"title": metadata["title"][:80],
|
| 196 |
"desc": metadata["desc"]
|
| 197 |
}
|
| 198 |
],
|
| 199 |
"source": metadata["source"],
|
| 200 |
"tid": metadata["tid"],
|
| 201 |
+
"title": metadata["title"][:80],
|
| 202 |
"cover": metadata["cover"],
|
| 203 |
"tag": metadata["tag"],
|
| 204 |
"desc_format_id": 0,
|
|
|
|
| 213 |
# copyright: 1 original 2 reprint
|
| 214 |
data["copyright"] = 1
|
| 215 |
|
| 216 |
+
response = requests.request(
|
| 217 |
+
method="POST",
|
| 218 |
+
url=url,
|
| 219 |
+
headers={
|
| 220 |
+
"TE": "Trailers",
|
| 221 |
+
**self.headers,
|
| 222 |
+
},
|
| 223 |
json=data,
|
| 224 |
+
cookies=self.cookies,
|
| 225 |
)
|
| 226 |
if response.status_code != 200:
|
| 227 |
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
|
|
|
| 232 |
filename = Path(filename)
|
| 233 |
filesize = filename.stat().st_size
|
| 234 |
|
| 235 |
+
js = self.pre_upload(filename=filename, filesize=filesize)
|
| 236 |
upos_uri = js["upos_uri"].split("//")[-1]
|
| 237 |
auth = js["auth"]
|
| 238 |
biz_id = js["biz_id"]
|
|
|
|
| 246 |
bilibili_filename = re.search(r"/(.*)\.", key).group(1)
|
| 247 |
fileio = filename.open(mode="rb")
|
| 248 |
self.upload_video_in_chunks(
|
| 249 |
+
endpoint="",
|
| 250 |
upos_uri=upos_uri,
|
| 251 |
auth=auth,
|
| 252 |
upload_id=upload_id,
|
|
|
|
| 271 |
|
| 272 |
bvid = None
|
| 273 |
status_code = publish_video_response["code"]
|
| 274 |
+
if status_code != 0:
|
| 275 |
+
# 137022
|
| 276 |
+
# 21104, 第(1)个视频的标题过长,已经超过80个字符
|
| 277 |
message = publish_video_response["message"]
|
| 278 |
logger.info(f"publish_video failed; code: {status_code}, message: {message}")
|
| 279 |
else:
|
| 280 |
+
try:
|
| 281 |
+
bvid = publish_video_response["data"]["bvid"]
|
| 282 |
+
except KeyError as error:
|
| 283 |
+
raise KeyError(f"publish_video failed; KeyError: {error}, publish_video_response: {publish_video_response}")
|
| 284 |
return bvid
|
| 285 |
|
| 286 |
+
@retry(
|
| 287 |
+
wait=wait_fixed(10),
|
| 288 |
+
stop=stop_after_attempt(3),
|
| 289 |
+
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 290 |
+
)
|
| 291 |
+
def predict_tid(self, title: str, upload_id: str, bilibili_filename: str):
|
| 292 |
+
csrf = self.cookies["bili_jct"]
|
| 293 |
+
url = f"https://member.bilibili.com/x/vupre/web/archive/types/predict"
|
| 294 |
+
params = {
|
| 295 |
+
"csrf": csrf,
|
| 296 |
+
}
|
| 297 |
+
data = {
|
| 298 |
+
"title": title,
|
| 299 |
+
"upload_id": upload_id,
|
| 300 |
+
"filename": bilibili_filename,
|
| 301 |
+
}
|
| 302 |
+
response = requests.request(
|
| 303 |
+
method="POST",
|
| 304 |
+
url=url,
|
| 305 |
+
headers={
|
| 306 |
+
"TE": "Trailers",
|
| 307 |
+
**self.headers,
|
| 308 |
+
},
|
| 309 |
+
params=params,
|
| 310 |
+
json=data,
|
| 311 |
+
cookies=self.cookies,
|
| 312 |
+
)
|
| 313 |
+
if response.status_code != 200:
|
| 314 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 315 |
+
js = response.json()
|
| 316 |
+
return js
|
| 317 |
+
|
| 318 |
|
| 319 |
class BilibiliVideoUploader20221109(BilibiliVideoUploader20200810):
|
| 320 |
def __init__(self):
|
| 321 |
super().__init__()
|
| 322 |
+
|
| 323 |
+
@retry(
|
| 324 |
+
wait=wait_fixed(10),
|
| 325 |
+
stop=stop_after_attempt(3),
|
| 326 |
+
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 327 |
+
)
|
| 328 |
+
def pre_upload(self, filename, filesize):
|
| 329 |
+
url = "https://member.bilibili.com/preupload"
|
| 330 |
+
params = {
|
| 331 |
+
"probe_version": "20221109",
|
| 332 |
+
"upcdn": "bldsa",
|
| 333 |
+
"zone": "cs",
|
| 334 |
+
"name": filename,
|
| 335 |
+
"r": "upos",
|
| 336 |
+
"profile": "ugcfx/bup",
|
| 337 |
+
"ssl": 0,
|
| 338 |
+
"version": "2.14.0.0",
|
| 339 |
+
"build": "2140000",
|
| 340 |
+
"size": filesize,
|
| 341 |
+
}
|
| 342 |
+
# response = self.session.get(
|
| 343 |
+
# url,
|
| 344 |
+
# params=params,
|
| 345 |
+
# headers={"TE": "Trailers"}
|
| 346 |
+
# )
|
| 347 |
+
response = requests.request(
|
| 348 |
+
# method="GET",
|
| 349 |
+
method="POST",
|
| 350 |
+
url=url,
|
| 351 |
+
headers={
|
| 352 |
+
"TE": "Trailers",
|
| 353 |
+
**self.headers,
|
| 354 |
+
},
|
| 355 |
+
params=params,
|
| 356 |
+
cookies=self.cookies,
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
if response.status_code != 200:
|
| 360 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 361 |
+
js = response.json()
|
| 362 |
+
if js["OK"] != 1:
|
| 363 |
+
raise AssertionError(f"request failed;")
|
| 364 |
+
return js
|
| 365 |
+
|
| 366 |
+
@retry(
|
| 367 |
+
wait=wait_fixed(10),
|
| 368 |
+
stop=stop_after_attempt(3),
|
| 369 |
+
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 370 |
+
)
|
| 371 |
+
def get_upload_video_id(self, endpoint, upos_uri, auth,
|
| 372 |
+
filesize, chunk_size, biz_id,
|
| 373 |
+
**kwargs
|
| 374 |
+
):
|
| 375 |
+
url = f"https:{endpoint}/{upos_uri}?uploads&output=json"
|
| 376 |
+
params = {
|
| 377 |
+
"profile": "ugcfx/bup",
|
| 378 |
+
"filesize": filesize,
|
| 379 |
+
"partsize": chunk_size,
|
| 380 |
+
# "biz_id": biz_id,
|
| 381 |
+
}
|
| 382 |
+
response = requests.request(
|
| 383 |
+
method="POST",
|
| 384 |
+
url=url,
|
| 385 |
+
headers={
|
| 386 |
+
"X-Upos-Auth": auth,
|
| 387 |
+
**self.headers,
|
| 388 |
+
},
|
| 389 |
+
params=params,
|
| 390 |
+
cookies=self.cookies,
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
if response.status_code != 200:
|
| 394 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 395 |
+
js = response.json()
|
| 396 |
+
if js["OK"] != 1:
|
| 397 |
+
raise AssertionError(f"request failed;")
|
| 398 |
+
return js
|
| 399 |
+
|
| 400 |
+
def upload_video_in_chunks(self, endpoint, upos_uri, auth, upload_id, fileio, filesize, chunk_size, chunks):
|
| 401 |
+
url = f"https:{endpoint}/{upos_uri}"
|
| 402 |
+
|
| 403 |
+
params = {
|
| 404 |
+
"partNumber": None, # start from 1
|
| 405 |
+
"uploadId": upload_id,
|
| 406 |
+
"chunk": None, # start from 0
|
| 407 |
+
"chunks": chunks,
|
| 408 |
+
"size": None, # current batch size
|
| 409 |
+
"start": None,
|
| 410 |
+
"end": None,
|
| 411 |
+
"total": filesize,
|
| 412 |
+
}
|
| 413 |
+
process_bar = tqdm(desc="bilibili_upload_video", total=chunks)
|
| 414 |
+
for chunknum in range(chunks):
|
| 415 |
+
start = fileio.tell()
|
| 416 |
+
batchbytes = fileio.read(chunk_size)
|
| 417 |
+
params["partNumber"] = chunknum + 1
|
| 418 |
+
params["chunk"] = chunknum
|
| 419 |
+
params["size"] = len(batchbytes)
|
| 420 |
+
params["start"] = start
|
| 421 |
+
params["end"] = fileio.tell()
|
| 422 |
+
response = requests.request(
|
| 423 |
+
method="OPTIONS",
|
| 424 |
+
url=url,
|
| 425 |
+
headers={
|
| 426 |
+
"X-Upos-Auth": auth,
|
| 427 |
+
**self.headers,
|
| 428 |
+
},
|
| 429 |
+
params=params,
|
| 430 |
+
data=batchbytes,
|
| 431 |
+
cookies=self.cookies,
|
| 432 |
+
)
|
| 433 |
+
if response.status_code != 200:
|
| 434 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 435 |
+
process_bar.update(1)
|
| 436 |
+
|
| 437 |
+
@retry(
|
| 438 |
+
wait=wait_fixed(10),
|
| 439 |
+
stop=stop_after_attempt(3),
|
| 440 |
+
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 441 |
+
)
|
| 442 |
+
def finish_upload(self, endpoint, upos_uri, auth, filename, upload_id, biz_id, chunks):
|
| 443 |
+
url = f"https:{endpoint}/{upos_uri}"
|
| 444 |
+
|
| 445 |
+
params = {
|
| 446 |
+
"output": "json",
|
| 447 |
+
"name": filename,
|
| 448 |
+
"profile" : "ugcfx/bup",
|
| 449 |
+
"uploadId": upload_id,
|
| 450 |
+
"biz_id": biz_id
|
| 451 |
+
}
|
| 452 |
+
print(params)
|
| 453 |
+
data = {
|
| 454 |
+
"parts": [
|
| 455 |
+
{"partNumber": i+1, "eTag": "etag"}
|
| 456 |
+
for i in range(chunks)
|
| 457 |
+
]
|
| 458 |
+
}
|
| 459 |
+
print(data)
|
| 460 |
+
response = requests.request(
|
| 461 |
+
method="POST",
|
| 462 |
+
url=url,
|
| 463 |
+
headers={
|
| 464 |
+
"X-Upos-Auth": auth,
|
| 465 |
+
**self.headers,
|
| 466 |
+
},
|
| 467 |
+
params=params,
|
| 468 |
+
data=json.dumps(data),
|
| 469 |
+
cookies=self.cookies,
|
| 470 |
+
)
|
| 471 |
+
if response.status_code != 200:
|
| 472 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 473 |
+
js = response.json()
|
| 474 |
+
if js["OK"] != 1:
|
| 475 |
+
raise AssertionError(f"request failed;")
|
| 476 |
+
return js
|
| 477 |
+
|
| 478 |
+
@retry(
|
| 479 |
+
wait=wait_fixed(10),
|
| 480 |
+
stop=stop_after_attempt(3),
|
| 481 |
+
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 482 |
+
)
|
| 483 |
+
def publish_video(self, bilibili_filename, metadata: dict):
|
| 484 |
+
"""
|
| 485 |
+
publish the uploaded video
|
| 486 |
+
|
| 487 |
+
https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/creativecenter/upload.md
|
| 488 |
+
:param bilibili_filename:
|
| 489 |
+
:param metadata:
|
| 490 |
+
:return:
|
| 491 |
+
"""
|
| 492 |
+
url = f'https://member.bilibili.com/x/vu/web/add?csrf={self.cookies["bili_jct"]}'
|
| 493 |
+
|
| 494 |
+
data = {
|
| 495 |
+
"copyright": metadata["copyright"],
|
| 496 |
+
"videos": [
|
| 497 |
+
{
|
| 498 |
+
"filename": bilibili_filename,
|
| 499 |
+
"title": metadata["title"],
|
| 500 |
+
"desc": metadata["desc"]
|
| 501 |
+
}
|
| 502 |
+
],
|
| 503 |
+
"source": metadata["source"],
|
| 504 |
+
"tid": metadata["tid"],
|
| 505 |
+
"title": metadata["title"],
|
| 506 |
+
"cover": metadata["cover"],
|
| 507 |
+
"tag": metadata["tag"],
|
| 508 |
+
"desc_format_id": 0,
|
| 509 |
+
"desc": metadata["desc"],
|
| 510 |
+
"dynamic": metadata["dynamic"],
|
| 511 |
+
"subtitle": {"open": 0, "lan": ""},
|
| 512 |
+
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
if metadata["copyright"] != 2:
|
| 516 |
+
del data["source"]
|
| 517 |
+
# copyright: 1 original 2 reprint
|
| 518 |
+
data["copyright"] = 1
|
| 519 |
+
|
| 520 |
+
response = requests.request(
|
| 521 |
+
method="POST",
|
| 522 |
+
url=url,
|
| 523 |
+
headers={
|
| 524 |
+
"TE": "Trailers",
|
| 525 |
+
**self.headers,
|
| 526 |
+
},
|
| 527 |
+
json=data,
|
| 528 |
+
cookies=self.cookies,
|
| 529 |
+
)
|
| 530 |
+
if response.status_code != 200:
|
| 531 |
+
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 532 |
+
js = response.json()
|
| 533 |
+
return js
|
| 534 |
+
|
| 535 |
+
def upload_video_file(self, filename: str):
|
| 536 |
+
filename = Path(filename)
|
| 537 |
+
filesize = filename.stat().st_size
|
| 538 |
+
|
| 539 |
+
js = self.pre_upload(filename=filename, filesize=filesize)
|
| 540 |
+
endpoint = js["endpoint"]
|
| 541 |
+
upos_uri = js["upos_uri"].split("//")[-1]
|
| 542 |
+
auth = js["auth"]
|
| 543 |
+
biz_id = js["biz_id"]
|
| 544 |
+
chunk_size = js["chunk_size"]
|
| 545 |
+
chunks = math.ceil(filesize / chunk_size)
|
| 546 |
+
|
| 547 |
+
upload_video_id_response = self.get_upload_video_id(
|
| 548 |
+
endpoint=endpoint,
|
| 549 |
+
upos_uri=upos_uri,
|
| 550 |
+
auth=auth,
|
| 551 |
+
filesize=filesize,
|
| 552 |
+
chunk_size=chunk_size,
|
| 553 |
+
biz_id=biz_id,
|
| 554 |
+
)
|
| 555 |
+
upload_id = upload_video_id_response["upload_id"]
|
| 556 |
+
key = upload_video_id_response["key"]
|
| 557 |
+
|
| 558 |
+
bilibili_filename = re.search(r"/(.*)\.", key).group(1)
|
| 559 |
+
fileio = filename.open(mode="rb")
|
| 560 |
+
self.upload_video_in_chunks(
|
| 561 |
+
endpoint=endpoint,
|
| 562 |
+
upos_uri=upos_uri,
|
| 563 |
+
auth=auth,
|
| 564 |
+
upload_id=upload_id,
|
| 565 |
+
fileio=fileio,
|
| 566 |
+
filesize=filesize,
|
| 567 |
+
chunk_size=chunk_size,
|
| 568 |
+
chunks=chunks
|
| 569 |
+
)
|
| 570 |
+
fileio.close()
|
| 571 |
+
|
| 572 |
+
# notify the all chunks have been uploaded
|
| 573 |
+
self.finish_upload(
|
| 574 |
+
endpoint=endpoint,
|
| 575 |
+
upos_uri=upos_uri, auth=auth, filename=filename.name,
|
| 576 |
+
upload_id=upload_id, biz_id=biz_id, chunks=chunks
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
return biz_id, upload_id, bilibili_filename
|
| 580 |
+
|
| 581 |
+
def upload_video_and_publish(self, filename: str, metadata: dict):
|
| 582 |
+
_, upload_id, bilibili_filename = self.upload_video_file(filename)
|
| 583 |
+
logger.info(f"finish uploading, upload_id: {upload_id}, bilibili_filename: {bilibili_filename}, filename: {filename}")
|
| 584 |
+
tid = self.predict_tid(
|
| 585 |
+
title=metadata["title"],
|
| 586 |
+
upload_id=upload_id,
|
| 587 |
+
bilibili_filename=bilibili_filename,
|
| 588 |
+
)
|
| 589 |
+
metadata["tid"] = tid
|
| 590 |
+
logger.info(f"predict tid, tid: {tid}, filename: {filename}")
|
| 591 |
+
publish_video_response = self.publish_video(bilibili_filename=bilibili_filename, metadata=metadata)
|
| 592 |
+
|
| 593 |
+
bvid = None
|
| 594 |
+
status_code = publish_video_response["code"]
|
| 595 |
+
if status_code == 137022:
|
| 596 |
+
message = publish_video_response["message"]
|
| 597 |
+
logger.info(f"publish_video failed; code: {status_code}, message: {message}")
|
| 598 |
+
else:
|
| 599 |
+
bvid = publish_video_response["data"]["bvid"]
|
| 600 |
+
return bvid
|
| 601 |
+
|
| 602 |
|
| 603 |
BilibiliVideoUploader = BilibiliVideoUploader20200810
|
| 604 |
+
# BilibiliVideoUploader = BilibiliVideoUploader20221109
|
| 605 |
|
| 606 |
|
| 607 |
def get_args():
|
| 608 |
parser = argparse.ArgumentParser()
|
| 609 |
parser.add_argument(
|
| 610 |
+
"--key_of_credentials",
|
| 611 |
+
default="bilibili_chenjiesen_credentials",
|
| 612 |
type=str
|
| 613 |
)
|
| 614 |
parser.add_argument(
|
| 615 |
"--filename",
|
| 616 |
+
default=(project_path / "data/tasks/chenjieshen_douyin_video_to_bilibili/video/douyin/陈杰森/7548332163859533083_20250910T170000_千万不要帮朋友担保!否则你可能背上一身债!.mp4").as_posix(),
|
| 617 |
type=str
|
| 618 |
)
|
| 619 |
args = parser.parse_args()
|
|
|
|
| 622 |
|
| 623 |
def main():
|
| 624 |
import log
|
| 625 |
+
from project_settings import project_path, log_directory, environment
|
| 626 |
|
| 627 |
log.setup_size_rotating(log_directory=log_directory)
|
| 628 |
|
|
|
|
| 632 |
|
| 633 |
flag = client.check_login()
|
| 634 |
print(f"flag: {flag}")
|
| 635 |
+
credentials = environment.get(args.key_of_credentials, dtype=json.loads)
|
| 636 |
+
client.login_with_credentials_info(credentials)
|
| 637 |
flag = client.check_login()
|
| 638 |
print(f"flag: {flag}")
|
| 639 |
|
toolbox/bilibili/video/video_manager2.py
CHANGED
|
@@ -373,32 +373,6 @@ class BilibiliVideoUploader(BilibiliClient):
|
|
| 373 |
bvid = publish_video_response["data"]["bvid"]
|
| 374 |
return bvid
|
| 375 |
|
| 376 |
-
@retry(
|
| 377 |
-
wait=wait_fixed(10),
|
| 378 |
-
stop=stop_after_attempt(3),
|
| 379 |
-
before_sleep=before_sleep_log(logger, logging.ERROR),
|
| 380 |
-
)
|
| 381 |
-
def predict_tid(self, title: str, upload_id: str, bilibili_filename: str):
|
| 382 |
-
csrf = self.cookies["bili_jct"]
|
| 383 |
-
url = f"https://member.bilibili.com/x/vupre/web/archive/types/predict"
|
| 384 |
-
params = {
|
| 385 |
-
"csrf": csrf,
|
| 386 |
-
}
|
| 387 |
-
data = {
|
| 388 |
-
"title": title,
|
| 389 |
-
"upload_id": upload_id,
|
| 390 |
-
"filename": bilibili_filename,
|
| 391 |
-
}
|
| 392 |
-
response = self.session.post(
|
| 393 |
-
url,
|
| 394 |
-
params=params,
|
| 395 |
-
json=data,
|
| 396 |
-
headers=self.headers,
|
| 397 |
-
)
|
| 398 |
-
if response.status_code != 200:
|
| 399 |
-
raise AssertionError(f"request failed; status_code: {response.status_code}, text: {response.text}")
|
| 400 |
-
js = response.json()
|
| 401 |
-
return js
|
| 402 |
|
| 403 |
|
| 404 |
def get_args():
|
|
|
|
| 373 |
bvid = publish_video_response["data"]["bvid"]
|
| 374 |
return bvid
|
| 375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
|
| 378 |
def get_args():
|