Skip to content

Commit

Permalink
add bvid resolving at bilibili
Browse files Browse the repository at this point in the history
  • Loading branch information
kilosonc committed Dec 11, 2020
1 parent 68e47f1 commit d202fa1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 15 additions & 8 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const referer = "https://www.bilibili.com"

var utoken string

func genAPI(aid, cid, quality int, bangumi bool, cookie string) (string, error) {
func genAPI(aid, cid, quality int, bvid string, bangumi bool, cookie string) (string, error) {
var (
err error
baseAPIURL string
Expand Down Expand Up @@ -54,14 +54,14 @@ func genAPI(aid, cid, quality int, bangumi bool, cookie string) (string, error)
// qn=0 flag makes the CDN address different every time
// quality=120(4k) is the highest quality so far
params = fmt.Sprintf(
"cid=%d&bvid=&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=16",
cid, quality,
"cid=%d&bvid=%s&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=16",
cid, bvid, quality,
)
baseAPIURL = bilibiliBangumiAPI
} else {
params = fmt.Sprintf(
"avid=%d&cid=%d&bvid=&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=16",
aid, cid, quality,
"avid=%d&cid=%d&bvid=%s&qn=%d&type=&otype=json&fourk=1&fnver=0&fnval=16",
aid, cid, bvid, quality,
)
baseAPIURL = bilibiliAPI
}
Expand Down Expand Up @@ -118,6 +118,7 @@ type bilibiliOptions struct {
bangumi bool
aid int
cid int
bvid string
page int
subtitle string
}
Expand All @@ -132,16 +133,19 @@ func extractBangumi(url, html string, extractOption types.Options) ([]*types.Dat
if !extractOption.Playlist {
aid := data.EpInfo.Aid
cid := data.EpInfo.Cid
if aid <= 0 || cid <= 0 {
bvid := data.EpInfo.BVid
if aid <= 0 || cid <= 0 || bvid == "" {
aid = data.EpList[0].Aid
cid = data.EpList[0].Cid
bvid = data.EpList[0].BVid
}
options := bilibiliOptions{
url: url,
html: html,
bangumi: true,
aid: aid,
cid: cid,
bvid: bvid,
}
return []*types.Data{bilibiliDownload(options, extractOption)}, nil
}
Expand All @@ -166,6 +170,7 @@ func extractBangumi(url, html string, extractOption types.Options) ([]*types.Dat
bangumi: true,
aid: u.Aid,
cid: u.Cid,
bvid: u.BVid,
}
go func(index int, options bilibiliOptions, extractedData []*types.Data) {
defer wgp.Done()
Expand Down Expand Up @@ -220,6 +225,7 @@ func extractNormalVideo(url, html string, extractOption types.Options) ([]*types
url: url,
html: html,
aid: pageData.Aid,
bvid: pageData.BVid,
cid: page.Cid,
page: p,
}
Expand Down Expand Up @@ -247,6 +253,7 @@ func extractNormalVideo(url, html string, extractOption types.Options) ([]*types
url: url,
html: html,
aid: pageData.Aid,
bvid: pageData.BVid,
cid: u.Cid,
subtitle: u.Part,
page: u.Page,
Expand Down Expand Up @@ -307,7 +314,7 @@ func bilibiliDownload(options bilibiliOptions, extractOption types.Options) *typ
// 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.bangumi, extractOption.Cookie)
api, err := genAPI(options.aid, options.cid, 120, options.bvid, options.bangumi, extractOption.Cookie)
if err != nil {
return types.EmptyData(options.url, err)
}
Expand Down Expand Up @@ -358,7 +365,7 @@ func bilibiliDownload(options bilibiliOptions, extractOption types.Options) *typ
if _, ok := streams[strconv.Itoa(q)]; ok {
continue
}
api, err := genAPI(options.aid, options.cid, q, options.bangumi, extractOption.Cookie)
api, err := genAPI(options.aid, options.cid, q, options.bvid, options.bangumi, extractOption.Cookie)
if err != nil {
return types.EmptyData(options.url, err)
}
Expand Down
10 changes: 6 additions & 4 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type token struct {
}

type bangumiEpData struct {
Aid int `json:"aid"`
Cid int `json:"cid"`
ID int `json:"id"`
EpID int `json:"ep_id"`
Aid int `json:"aid"`
Cid int `json:"cid"`
BVid string `json:"bvid"`
ID int `json:"id"`
EpID int `json:"ep_id"`
}

type bangumiData struct {
Expand All @@ -37,6 +38,7 @@ type multiPageVideoData struct {

type multiPage struct {
Aid int `json:"aid"`
BVid string `json:"bvid"`
VideoData multiPageVideoData `json:"videoData"`
}

Expand Down

0 comments on commit d202fa1

Please sign in to comment.