Skip to content

Commit

Permalink
Merge pull request #1227 from baiyufei/master
Browse files Browse the repository at this point in the history
fix: bilibili extractBangumi
  • Loading branch information
iawia002 authored Apr 20, 2023
2 parents ccd7baf + 976f05c commit 6814219
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
50 changes: 45 additions & 5 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bilibili
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -92,20 +93,51 @@ type bilibiliOptions struct {
}

func extractBangumi(url, html string, extractOption extractors.Options) ([]*extractors.Data, error) {
dataString := utils.MatchOneOf(html, `window.__INITIAL_STATE__=(.+?);\(function`)[1]
var data bangumiData
err := json.Unmarshal([]byte(dataString), &data)
dataString := utils.MatchOneOf(html, `<script\s+id="__NEXT_DATA__"\s+type="application/json"\s*>(.*?)</script\s*>`)[1]
epMapString := utils.MatchOneOf(dataString, `"epMap"\s*:\s*(.+?)\s*,\s*"initEpList"`)[1]
fullVideoIdString := utils.MatchOneOf(dataString, `"videoId"\s*:\s*"(ep|ss)(\d+)"`)
epSsString := fullVideoIdString[1] // "ep" or "ss"
videoIdString := fullVideoIdString[2]

var epMap map[string]json.RawMessage
err := json.Unmarshal([]byte(epMapString), &epMap)
if err != nil {
return nil, errors.WithStack(err)
}
var data bangumiData
for idString, jsonByte := range epMap {
var epInfo bangumiEpData
err := json.Unmarshal(jsonByte, &epInfo)
if err != nil {
return nil, errors.WithStack(err)
}
epID, err := strconv.ParseInt(idString, 10, 0)
if err != nil {
return nil, errors.WithStack(err)
}
epInfo.EpID = int(epID)
if idString == videoIdString || (epSsString == "ss" && epInfo.TitleFormat == "第1话") {
data.EpInfo = epInfo
}
data.EpList = append(data.EpList, epInfo)
}

sort.Slice(data.EpList, func(i, j int) bool {
return data.EpList[i].EpID < data.EpList[j].EpID
})

if !extractOption.Playlist {
aid := data.EpInfo.Aid
cid := data.EpInfo.Cid
bvid := data.EpInfo.BVid
titleFormat := data.EpInfo.TitleFormat
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
longTitle = data.EpList[0].LongTitle
}
options := bilibiliOptions{
url: url,
Expand All @@ -114,6 +146,8 @@ func extractBangumi(url, html string, extractOption extractors.Options) ([]*extr
aid: aid,
cid: cid,
bvid: bvid,

subtitle: fmt.Sprintf("%s %s", titleFormat, longTitle),
}
return []*extractors.Data{bilibiliDownload(options, extractOption)}, nil
}
Expand All @@ -139,6 +173,8 @@ func extractBangumi(url, html string, extractOption extractors.Options) ([]*extr
aid: u.Aid,
cid: u.Cid,
bvid: u.BVid,

subtitle: fmt.Sprintf("%s %s", u.TitleFormat, u.LongTitle),
}
go func(index int, options bilibiliOptions, extractedData []*extractors.Data) {
defer wgp.Done()
Expand Down Expand Up @@ -425,10 +461,14 @@ func bilibiliDownload(options bilibiliOptions, extractOption extractors.Options)
}
title := parser.Title(doc)
if options.subtitle != "" {
pageString := ""
if options.page > 0 {
pageString = fmt.Sprintf("P%d ", options.page)
}
if extractOption.EpisodeTitleOnly {
title = fmt.Sprintf("P%d %s", options.page, options.subtitle)
title = fmt.Sprintf("%s%s", pageString, options.subtitle)
} else {
title = fmt.Sprintf("%s P%d %s", title, options.page, options.subtitle)
title = fmt.Sprintf("%s %s%s", title, pageString, options.subtitle)
}
}

Expand Down
3 changes: 1 addition & 2 deletions extractors/bilibili/bilibili_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ func TestBilibili(t *testing.T) {
args: test.Args{
URL: "https://www.bilibili.com/video/av41301960",
Title: "【英雄联盟】2019赛季CG 《觉醒》",
Size: 70696896,
},
playlist: false,
},
{
name: "bangumi test",
args: test.Args{
URL: "https://www.bilibili.com/bangumi/play/ep167000",
Title: "狐妖小红娘第70话 苏苏智商上线",
Title: "狐妖小红娘 第70话 苏苏智商上线",
},
},
{
Expand Down
12 changes: 7 additions & 5 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ type token struct {
}

type bangumiEpData struct {
Aid int `json:"aid"`
Cid int `json:"cid"`
BVid string `json:"bvid"`
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"`
TitleFormat string `json:"titleFormat"`
LongTitle string `json:"long_title"`
}

type bangumiData struct {
Expand Down

0 comments on commit 6814219

Please sign in to comment.