Spaces:
Sleeping
Sleeping
File size: 6,503 Bytes
0aee47a |
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 |
"""
bilibili_api.utils.upos
"""
import os
import json
import httpx
import asyncio
from asyncio.tasks import create_task
from .utils import get_api
from .network import get_session
from ..exceptions.NetworkException import NetworkException
from ..exceptions.ResponseCodeException import ResponseCodeException
from ..exceptions.ApiException import ApiException
class UposFile:
"""
Upos 文件对象
"""
path: str
size: int
def __init__(self, path: str) -> None:
self.path = path
self.size = self._get_size()
def _get_size(self) -> int:
"""
获取文件大小
Returns:
int: 文件大小
"""
size: int = 0
stream = open(self.path, "rb")
while True:
s: bytes = stream.read(1024)
if not s:
break
size += len(s)
stream.close()
return size
class UposFileUploader:
"""
Upos 文件上传
"""
_upload_id: str
_upload_url: str
_session: httpx.AsyncClient
def __init__(self, file: UposFile, preupload: dict) -> None:
self.file = file
self.preupload = preupload
self._upload_id = preupload["upload_id"]
self._upload_url = f'https:{preupload["endpoint"]}/{preupload["upos_uri"].removeprefix("upos://")}'
self._session = get_session()
async def upload(self) -> dict:
"""
上传文件
Returns:
dict: filename, cid
"""
page_size = self.file.size
# 所有分块起始位置
chunk_offset_list = list(range(0, page_size, self.preupload["chunk_size"]))
# 分块总数
total_chunk_count = len(chunk_offset_list)
# 并发上传分块
chunk_number = 0
# 上传队列
chunks_pending = []
for offset in chunk_offset_list:
chunks_pending.insert(
0,
self._upload_chunk(offset, chunk_number, total_chunk_count),
)
chunk_number += 1
while chunks_pending:
tasks = []
while len(tasks) < self.preupload["threads"] and len(chunks_pending) > 0:
tasks.append(create_task(chunks_pending.pop()))
result = await asyncio.gather(*tasks)
for r in result:
if not r["ok"]:
chunks_pending.insert(
0,
self._upload_chunk(
r["offset"],
r["chunk_number"],
total_chunk_count,
),
)
data = await self._complete_file(total_chunk_count)
return data
async def _upload_chunk(
self,
offset: int,
chunk_number: int,
total_chunk_count: int,
) -> dict:
"""
上传视频分块
Args:
offset (int): 分块起始位置
chunk_number (int): 分块编号
total_chunk_count (int): 总分块数
Returns:
dict: 上传结果和分块信息。
"""
chunk_event_callback_data = {
"offset": offset,
"chunk_number": chunk_number,
"total_chunk_count": total_chunk_count,
}
stream = open(self.file.path, "rb")
stream.seek(offset)
chunk = stream.read(self.preupload["chunk_size"])
stream.close()
err_return = {
"ok": False,
"chunk_number": chunk_number,
"offset": offset,
}
real_chunk_size = len(chunk)
params = {
"partNumber": str(chunk_number + 1),
"uploadId": str(self._upload_id),
"chunk": str(chunk_number),
"chunks": str(total_chunk_count),
"size": str(real_chunk_size),
"start": str(offset),
"end": str(offset + real_chunk_size),
"total": self.file.size,
}
ok_return = {
"ok": True,
"chunk_number": chunk_number,
"offset": offset,
}
try:
resp = await self._session.put(
self._upload_url,
data=chunk, # type: ignore
params=params,
headers={"x-upos-auth": self.preupload["auth"]},
)
if resp.status_code >= 400:
chunk_event_callback_data["info"] = f"Status {resp.status_code}"
return err_return
data = resp.text
if data != "MULTIPART_PUT_SUCCESS" and data != "":
chunk_event_callback_data["info"] = "分块上传失败"
return err_return
except Exception as e:
chunk_event_callback_data["info"] = str(e)
return err_return
return ok_return
async def _complete_file(self, chunks: int) -> dict:
"""
提交文件
Args:
chunks (int): 分块数量
Returns:
dict: filename: 该分 P 的标识符,用于最后提交视频。cid: 分 P 的 cid
"""
data = {
"parts": list(
map(lambda x: {"partNumber": x, "eTag": "etag"}, range(1, chunks + 1))
)
}
params = {
"output": "json",
"name": os.path.basename(os.path.split(self.file.path)[1]),
"profile": "ugcfx/bup",
"uploadId": self._upload_id,
"biz_id": self.preupload["biz_id"],
}
resp = await self._session.post(
url=self._upload_url,
data=json.dumps(data), # type: ignore
headers={
"x-upos-auth": self.preupload["auth"],
"content-type": "application/json; charset=UTF-8",
},
params=params,
)
if resp.status_code >= 400:
err = NetworkException(resp.status_code, "状态码错误,提交分 P 失败")
raise err
data = json.loads(resp.read())
if data["OK"] != 1:
err = ResponseCodeException(-1, f'提交分 P 失败,原因: {data["message"]}')
raise err |