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

Bilibili Festival #1315

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type Downloader struct {
option Options
}

const (
DOWNLOAD_FILE_EXT = ".download"
)

func progressBar(size int64) *pb.ProgressBar {
tmpl := `{{counters .}} {{bar . "[" "=" ">" "-" "]"}} {{speed .}} {{percent . | green}} {{rtime .}}`
return pb.New64(size).
Expand Down Expand Up @@ -135,7 +139,7 @@ func (downloader *Downloader) save(part *extractors.Part, refer, fileName string
return nil
}

tempFilePath := filePath + ".download"
tempFilePath := filePath + DOWNLOAD_FILE_EXT
tempFileSize, _, err := utils.FileSize(tempFilePath)
if err != nil {
return err
Expand Down Expand Up @@ -233,7 +237,7 @@ func (downloader *Downloader) multiThreadSave(dataPart *extractors.Part, refer,
downloader.bar.Add64(fileSize)
return nil
}
tmpFilePath := filePath + ".download"
tmpFilePath := filePath + DOWNLOAD_FILE_EXT
tmpFileSize, tmpExists, err := utils.FileSize(tmpFilePath)
if err != nil {
return err
Expand Down Expand Up @@ -469,7 +473,7 @@ func writeFilePartMeta(file *os.File, meta *FilePartMeta) error {
}

func mergeMultiPart(filepath string, parts []*FilePartMeta) error {
tempFilePath := filepath + ".download"
tempFilePath := filepath + DOWNLOAD_FILE_EXT
tempFile, err := os.OpenFile(tempFilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
Expand Down
34 changes: 32 additions & 2 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ func getMultiPageData(html string) (*multiPage, error) {
return &data, nil
}

func extractFestival(url, html string, extractOption extractors.Options) ([]*extractors.Data, error) {
matches := utils.MatchAll(html, "<\\s*script[^>]*>\\s*window\\.__INITIAL_STATE__=([\\s\\S]*?);\\s?\\(function[\\s\\S]*?<\\/\\s*script\\s*>")
if len(matches) < 1 {
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}
if len(matches[0]) < 2 {
return nil, errors.New("could not find video in page")
}

var festivalData festival
err := json.Unmarshal([]byte(matches[0][1]), &festivalData)
if err != nil {
return nil, errors.WithStack(err)
}

options := bilibiliOptions{
url: url,
html: html,
aid: festivalData.VideoInfo.Aid,
bvid: festivalData.VideoInfo.BVid,
cid: festivalData.VideoInfo.Cid,
page: 0,
}

return []*extractors.Data{bilibiliDownload(options, extractOption)}, nil
}

func extractNormalVideo(url, html string, extractOption extractors.Options) ([]*extractors.Data, error) {
pageData, err := getMultiPageData(html)
if err != nil {
Expand Down Expand Up @@ -333,9 +360,12 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
if strings.Contains(url, "bangumi") {
// handle bangumi
return extractBangumi(url, html, option)
} else if strings.Contains(url, "festival") {
return extractFestival(url, html, option)
} else {
// handle normal video
return extractNormalVideo(url, html, option)
}
// handle normal video
return extractNormalVideo(url, html, option)
}

// bilibiliDownload is the download function for a single URL
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 @@ -66,6 +66,13 @@ func TestBilibili(t *testing.T) {
Title: "【十年榜】2000-2009年最强华语金曲TOP100 P1 100爱转角-罗志祥",
},
},
{
name: "festival test",
args: test.Args{
URL: "https://www.bilibili.com/festival/lty10th?bvid=BV1dZ4y1Y7bt",
Title: "洛天依十周年官方演唱会",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
27 changes: 27 additions & 0 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,30 @@ type bilibiliWebInterface struct {
Code int `json:"code"`
Data bilibiliWebInterfaceData `json:"data"`
}

type festival struct {
VideoSections []struct {
Id int64 `json:"id"`
Title string `json:"title"`
Type int `json:"type"`
} `json:"videoSections"`
Episodes []episode `json:"episodes"`
VideoInfo struct {
Aid int `json:"aid"`
BVid string `json:"bvid"`
Cid int `json:"cid"`
Title string `json:"title"`
Desc string `json:"desc"`
Pages []struct {
Cid int `json:"cid"`
Duration int `json:"duration"`
Page int `json:"page"`
Part string `json:"part"`
Dimension struct {
Width int `json:"width"`
Height int `json:"height"`
Rotate int `json:"rotate"`
} `json:"dimension"`
} `json:"pages"`
} `json:"videoInfo"`
}
Loading