Skip to content

Commit

Permalink
临时修复B站EP视频无法下载 (#1389)
Browse files Browse the repository at this point in the history
* Update bilibili.go

* Update types.go

* Update bilibili.go
  • Loading branch information
buddingworld authored Oct 12, 2024
1 parent 1608a8f commit 51d23d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
40 changes: 17 additions & 23 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,26 @@ type bilibiliOptions struct {

func extractBangumi(url, html string, extractOption extractors.Options) ([]*extractors.Data, error) {
dataString := utils.MatchOneOf(html, `<script\s+id="__NEXT_DATA__"\s+type="application/json"\s*>(.*?)</script\s*>`)[1]
epArrayString := utils.MatchOneOf(dataString, `"episodes"\s*:\s*(.+?)\s*,\s*"user_status"`)[1]
epArrayString := utils.MatchOneOf(dataString, `"episode_info"\s*:\s*(.+?)\s*,\s*"season_info"`)[1]
fullVideoIdString := utils.MatchOneOf(dataString, `"videoId"\s*:\s*"(ep|ss)(\d+)"`)
epSsString := fullVideoIdString[1] // "ep" or "ss"
videoIdString := fullVideoIdString[2]

var epArray []json.RawMessage
var epArray EpVideoInfo
err := json.Unmarshal([]byte(epArrayString), &epArray)
if err != nil {
return nil, errors.WithStack(err)
}
var data bangumiData
for _, jsonByte := range epArray {
var epInfo bangumiEpData
err := json.Unmarshal(jsonByte, &epInfo)
if err != nil {
return nil, errors.WithStack(err)
}
videoId, err := strconv.ParseInt(videoIdString, 10, 0)
if err != nil {
return nil, errors.WithStack(err)
}
if epInfo.ID == int(videoId) || (epSsString == "ss" && epInfo.TitleFormat == "第1话") {
data.EpInfo = epInfo
}
data.EpList = append(data.EpList, epInfo)

videoId, err := strconv.ParseInt(videoIdString, 10, 0)
if err != nil {
return nil, errors.WithStack(err)
}
if epArray.EpID == int(videoId) || (epSsString == "ss" && epArray.Title == "第1话") {
data.EpInfo = epArray
}
data.EpList = append(data.EpList, epArray)

sort.Slice(data.EpList, func(i, j int) bool {
return data.EpList[i].EpID < data.EpList[j].EpID
Expand All @@ -130,14 +124,14 @@ func extractBangumi(url, html string, extractOption extractors.Options) ([]*extr
if !extractOption.Playlist {
aid := data.EpInfo.Aid
cid := data.EpInfo.Cid
bvid := data.EpInfo.BVid
titleFormat := data.EpInfo.TitleFormat
bvid := data.EpInfo.Bvid
titleFormat := data.EpInfo.Title
longTitle := data.EpInfo.LongTitle
if aid <= 0 || cid <= 0 || bvid == "" {
aid = data.EpList[0].Aid
cid = data.EpList[0].Cid
bvid = data.EpList[0].BVid
titleFormat = data.EpList[0].TitleFormat
bvid = data.EpList[0].Bvid
titleFormat = data.EpList[0].Title
longTitle = data.EpList[0].LongTitle
}
options := bilibiliOptions{
Expand Down Expand Up @@ -165,17 +159,17 @@ func extractBangumi(url, html string, extractOption extractors.Options) ([]*extr
wgp.Add()
id := u.EpID
if id == 0 {
id = u.ID
id = u.EpID
}
// html content can't be reused here
options := bilibiliOptions{
url: fmt.Sprintf("https://www.bilibili.com/bangumi/play/ep%d", id),
bangumi: true,
aid: u.Aid,
cid: u.Cid,
bvid: u.BVid,
bvid: u.Bvid,

subtitle: fmt.Sprintf("%s %s", u.TitleFormat, u.LongTitle),
subtitle: fmt.Sprintf("%s %s", u.Title, u.LongTitle),
}
go func(index int, options bilibiliOptions, extractedData []*extractors.Data) {
defer wgp.Done()
Expand Down
27 changes: 17 additions & 10 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ type token struct {
Data tokenData `json:"data"`
}

type bangumiEpData struct {
Aid int `json:"aid"`
Cid int `json:"cid"`
BVid string `json:"bvid"`
ID int `json:"id"`
EpID int `json:"ep_id"`
TitleFormat string `json:"titleFormat"`
LongTitle string `json:"long_title"`
type Interaction struct {
Interaction bool `json:"interaction"`
}

type EpVideoInfo struct {
Aid int `json:"aid"`
Bvid string `json:"bvid"`
Cid int `json:"cid"`
DeliveryBusinessFragmentVideo bool `json:"delivery_business_fragment_video"`
DeliveryFragmentVideo bool `json:"delivery_fragment_video"`
EpID int `json:"ep_id"`
EpStatus int `json:"ep_status"`
Interaction Interaction `json:"interaction"`
LongTitle string `json:"long_title"`
Title string `json:"title"`
}

type bangumiData struct {
EpInfo bangumiEpData `json:"epInfo"`
EpList []bangumiEpData `json:"epList"`
EpInfo EpVideoInfo `json:"epInfo"`
EpList []EpVideoInfo `json:"epList"`
}

type videoPagesData struct {
Expand Down

0 comments on commit 51d23d2

Please # to comment.