Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了对B站8K超高清视频的支持 #978

Merged
merged 16 commits into from
Dec 7, 2021
18 changes: 11 additions & 7 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const referer = "https://www.bilibili.com"

var utoken string

var headers = map[string]string{
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
}
iawia002 marked this conversation as resolved.
Show resolved Hide resolved

func genAPI(aid, cid, quality int, bvid string, bangumi bool, cookie string) (string, error) {
var (
err error
Expand All @@ -33,7 +37,7 @@ func genAPI(aid, cid, quality int, bvid string, bangumi bool, cookie string) (st
utoken, err = request.Get(
fmt.Sprintf("%said=%d&cid=%d", bilibiliTokenAPI, aid, cid),
referer,
nil,
headers,
)
if err != nil {
return "", err
Expand All @@ -60,7 +64,7 @@ func genAPI(aid, cid, quality int, bvid string, bangumi bool, cookie string) (st
baseAPIURL = bilibiliBangumiAPI
} else {
params = fmt.Sprintf(
"avid=%d&cid=%d&bvid=%s&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=16",
"avid=%d&cid=%d&bvid=%s&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=2000",
aid, cid, bvid, quality,
)
baseAPIURL = bilibiliAPI
Expand Down Expand Up @@ -239,7 +243,7 @@ func New() types.Extractor {
// Extract is the main function to extract the data.
func (e *extractor) Extract(url string, option types.Options) ([]*types.Data, error) {
var err error
html, err := request.Get(url, referer, nil)
html, err := request.Get(url, referer, headers)
if err != nil {
return nil, err
}
Expand All @@ -265,20 +269,20 @@ func bilibiliDownload(options bilibiliOptions, extractOption types.Options) *typ
// reuse html string, but this can't be reused in case of playlist
html = options.html
} else {
html, err = request.Get(options.url, referer, nil)
html, err = request.Get(options.url, referer, headers)
if err != nil {
return types.EmptyData(options.url, err)
}
}

// Get "accept_quality" and "accept_description"
// "accept_description":["高清 1080P","高清 720P","清晰 480P","流畅 360P"],
// "accept_quality":[120,112,80,48,32,16],
api, err := genAPI(options.aid, options.cid, 120, options.bvid, options.bangumi, extractOption.Cookie)
// "accept_quality":[127,120,112,80,48,32,16],
api, err := genAPI(options.aid, options.cid, 127, options.bvid, options.bangumi, extractOption.Cookie)
if err != nil {
return types.EmptyData(options.url, err)
}
jsonString, err := request.Get(api, referer, nil)
jsonString, err := request.Get(api, referer, headers)
if err != nil {
return types.EmptyData(options.url, err)
}
Expand Down
7 changes: 7 additions & 0 deletions extractors/bilibili/bilibili_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestBilibili(t *testing.T) {
Title: "你的名字。",
},
},
{
name: "8k test",
args: test.Args{
URL: "https://www.bilibili.com/video/BV1qM4y1w716",
Title: "【8K演示片】B站首发!你的设备还顶得住吗?",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type dash struct {
}

var qualityString = map[int]string{
127: "超高清 8K",
120: "超清 4K",
116: "高清 1080P60",
74: "高清 720P60",
Expand Down