Skip to content

Commit

Permalink
🐛 修复由于分片确认请求发送失败导致视频无法过审
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoMiku01 committed Oct 1, 2022
1 parent 284a132 commit 80ef5d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.mp4
*.jpg
*.png
*.json
*.exe
.idea
Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ nfpms:
- deb
- rpm
maintainer: XiaoMiku01

2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# biliup-go

**测试中,目前上传的视频过不了审,原因未知待解决**

B 站命令行投稿工具 Golang 实现,支持 **扫码登录**, 并将登录后返回的 cookie 和 token 保存在 `cookie.json` 中,可用于其他项目。

[biliup-rs](https://github.com/ForgQi/biliup-rs) 的 Golang 实现。
Expand Down
43 changes: 34 additions & 9 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewUp(cookiePath string) *Up {
var client = req.C().SetCommonHeaders(map[string]string{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53",
"cookie": cookie,
"Connection": "keep-alive",
})
resp, _ := client.R().Get("https://api.bilibili.com/x/web-interface/nav")
uname := gjson.ParseBytes(resp.Bytes()).Get("data.uname").String()
Expand Down Expand Up @@ -159,6 +160,7 @@ func (u *Up) Up() {
u.upVideo.biliFileName = strings.Split(strings.Split(strings.Split(preupinfo.UposUri, "//")[1], "/")[1], ".")[0]
u.upVideo.chunkSize = preupinfo.ChunkSize
u.upVideo.auth = preupinfo.Auth
u.upVideo.bizId = preupinfo.BizId
u.upload()
var addreq = AddReqJson{
Copyright: u.upType,
Expand Down Expand Up @@ -190,6 +192,7 @@ func (u *Up) Up() {
LosslessMusic: 0,
Csrf: u.csrf,
}
//_ = addreq
resp, _ := u.client.R().SetQueryParams(map[string]string{
"csrf": u.csrf,
}).SetBodyJsonMarshal(addreq).Post("https://member.bilibili.com/x/vu/web/add/v3")
Expand Down Expand Up @@ -225,20 +228,21 @@ func (u *Up) upload() {
reqjson.Parts = append(reqjson.Parts, p)
}
}()
//for i := 0; int64(i) < chunks; i++ {
for {
buf := make([]byte, u.upVideo.chunkSize)
n, err := file.Read(buf)
size, err := file.Read(buf)
if err != nil && err != io.EOF {
break
}
buf = buf[:n]
size := len(buf)
buf = buf[:size]
//size := n
if size > 0 {
wg.Add(1)
end += size
go func(chunk int, start, end, size int, buf []byte) {
go func(chunk int, start, end, size int, buf []byte, bar *progressbar.ProgressBar) {
defer wg.Done()
u.client.R().SetHeaders(map[string]string{
resp, _ := u.client.R().SetHeaders(map[string]string{
"Content-Type": "application/octet-stream",
"Content-Length": strconv.Itoa(size),
}).SetQueryParams(map[string]string{
Expand All @@ -250,13 +254,22 @@ func (u *Up) upload() {
"start": strconv.Itoa(start),
"end": strconv.Itoa(end),
"total": strconv.FormatInt(u.upVideo.videoSize, 10),
}).SetBodyBytes(buf).Put(u.upVideo.uploadBaseUrl)
}).SetBodyBytes(buf).SetRetryCount(5).AddRetryHook(func(resp *req.Response, err error) {
log.Println("重试发送分片", chunk)
return
}).
AddRetryCondition(func(resp *req.Response, err error) bool {
return err != nil || resp.StatusCode != 200
}).Put(u.upVideo.uploadBaseUrl)
bar.Add(len(buf) / 1024 / 1024)
if resp.StatusCode != 200 {
log.Println("分片", chunk, "上传失败", resp.StatusCode, "size", size)
}
partchan <- Part{
PartNumber: int64(chunk + 1),
ETag: "etag",
}
}(chunk, start, end, size, buf)
}(chunk, start, end, size, buf, bar)
start += size
chunk++
}
Expand All @@ -266,13 +279,25 @@ func (u *Up) upload() {
}
wg.Wait()
close(partchan)
u.client.R().SetQueryParams(map[string]string{
jsonString, _ := json.Marshal(&reqjson)
log.Println(string(jsonString))
u.client.R().SetHeaders(map[string]string{
"Content-Type": "application/json",
"Origin": "https://member.bilibili.com",
"Referer": "https://member.bilibili.com/",
}).SetQueryParams(map[string]string{
"output": "json",
"profile": "ugcfx/bup",
"name": u.upVideo.videoName,
"uploadId": u.upVideo.uploadId,
"biz_id": strconv.FormatInt(u.upVideo.bizId, 10),
}).SetBodyJsonMarshal(reqjson).SetResult(&upinfo).Post(u.upVideo.uploadBaseUrl)
}).SetBodyString(string(jsonString)).SetResult(&upinfo).SetRetryCount(5).AddRetryHook(func(resp *req.Response, err error) {
log.Println("重试发送分片确认请求")
return
}).
AddRetryCondition(func(resp *req.Response, err error) bool {
return err != nil || resp.StatusCode != 200
}).Post(u.upVideo.uploadBaseUrl)
}

func (u *Up) getMetaUposUri() string {
Expand Down

0 comments on commit 80ef5d2

Please sign in to comment.