diff --git a/README.md b/README.md index 65b3680b0..8d2f7339d 100644 --- a/README.md +++ b/README.md @@ -565,7 +565,7 @@ $ lux -j "https://www.bilibili.com/video/av20203945" -o string Specify the output path -O string - Specify the output file name + Specify the output file name. supports placeholder %author %date %title. ``` #### Subtitle: diff --git a/app/app.go b/app/app.go index 13a3da127..d3c1c11a4 100644 --- a/app/app.go +++ b/app/app.go @@ -274,6 +274,7 @@ func New() *cli.App { func download(c *cli.Context, videoURL string) error { data, err := extractors.Extract(videoURL, extractors.Options{ Playlist: c.Bool("playlist"), + AudioOnly: c.Bool("audio-only"), Items: c.String("items"), ItemStart: int(c.Uint("start")), ItemEnd: int(c.Uint("end")), diff --git a/downloader/downloader.go b/downloader/downloader.go index 6abd1a447..61f0d73d1 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -5,6 +5,8 @@ import ( "encoding/binary" "encoding/json" "fmt" + "github.com/cheggaaa/pb/v3" + "github.com/pkg/errors" "io" "net/http" "os" @@ -12,12 +14,10 @@ import ( "path/filepath" "regexp" "sort" + "strings" "sync" "time" - "github.com/cheggaaa/pb/v3" - "github.com/pkg/errors" - "github.com/iawia002/lux/extractors" "github.com/iawia002/lux/request" "github.com/iawia002/lux/utils" @@ -558,9 +558,26 @@ func (downloader *Downloader) Download(data *extractors.Data) error { return nil } + /*title := downloader.option.OutputName + if title == "" { + title = data.Title + }*/ title := downloader.option.OutputName if title == "" { title = data.Title + } else { + // 创建占位符映射 + placeholders := map[string]string{ + "%title": data.Title, + // 可以根据需要添加更多占位符 + "%author": data.Author, + "%date": data.Date, + } + + // 替换所有占位符 + for placeholder, value := range placeholders { + title = strings.ReplaceAll(title, placeholder, value) + } } title = utils.FileName(title, "", downloader.option.FileNameLength) diff --git a/extractors/bilibili/bilibili.go b/extractors/bilibili/bilibili.go index 313197dac..da1a1d494 100644 --- a/extractors/bilibili/bilibili.go +++ b/extractors/bilibili/bilibili.go @@ -485,6 +485,17 @@ func bilibiliDownload(options bilibiliOptions, extractOption extractors.Options) return extractors.EmptyData(options.url, err) } title := parser.Title(doc) + var author = strings.TrimSpace(doc.Find("a.up-name").Text()) + var datePart = doc.Find("div.pubdate-ip-text").Text() + if len(strings.TrimSpace(datePart)) > 0 { + parts := strings.Split(datePart, " ") + // 获取第一部分,即年月日 + if len(parts) > 0 { + datePart = parts[0] + } + } + //title = fmt.Sprintf("%s %s", datePart, title) + title = fmt.Sprintf("%s", title) if options.subtitle != "" { pageString := "" if options.page > 0 { @@ -500,6 +511,8 @@ func bilibiliDownload(options bilibiliOptions, extractOption extractors.Options) return &extractors.Data{ Site: "哔哩哔哩 bilibili.com", Title: title, + Date: datePart, + Author: author, Type: extractors.DataTypeVideo, Streams: streams, Captions: map[string]*extractors.CaptionPart{ diff --git a/extractors/bilibili/bilibili_test.go b/extractors/bilibili/bilibili_test.go index f1ad0ecbc..79e13341a 100644 --- a/extractors/bilibili/bilibili_test.go +++ b/extractors/bilibili/bilibili_test.go @@ -9,9 +9,10 @@ import ( func TestBilibili(t *testing.T) { tests := []struct { - name string - args test.Args - playlist bool + name string + args test.Args + playlist bool + audioonly bool }{ { name: "normal test 1", @@ -27,7 +28,8 @@ func TestBilibili(t *testing.T) { URL: "https://www.bilibili.com/video/av41301960", Title: "【英雄联盟】2019赛季CG 《觉醒》", }, - playlist: false, + playlist: false, + audioonly: true, }, { name: "bangumi test", @@ -87,6 +89,13 @@ func TestBilibili(t *testing.T) { ThreadNumber: 9, }) test.CheckError(t, err) + } + if tt.audioonly { + // for playlist, we don't check the data + _, err = New().Extract(tt.args.URL, extractors.Options{ + AudioOnly: true, + }) + test.CheckError(t, err) } else { data, err = New().Extract(tt.args.URL, extractors.Options{}) test.CheckError(t, err) diff --git a/extractors/types.go b/extractors/types.go index 7e56d09c6..60bbb7a7d 100644 --- a/extractors/types.go +++ b/extractors/types.go @@ -45,10 +45,12 @@ const ( // Data is the main data structure for the whole video data. type Data struct { // URL is used to record the address of this download - URL string `json:"url"` - Site string `json:"site"` - Title string `json:"title"` - Type DataType `json:"type"` + URL string `json:"url"` + Site string `json:"site"` + Title string `json:"title"` + Author string `json:"author"` + Date string `json:"date"` + Type DataType `json:"type"` // each stream has it's own Parts and Quality Streams map[string]*Stream `json:"streams"` // danmaku, subtitles, etc @@ -116,6 +118,9 @@ type Options struct { // EpisodeTitleOnly indicates file name of each bilibili episode doesn't include the playlist title EpisodeTitleOnly bool + // AudioOnly + AudioOnly bool + YoukuCcode string YoukuCkey string YoukuPassword string