From fd91b1e13098ee2e45b8e2badee979096d558924 Mon Sep 17 00:00:00 2001 From: Aynakeya Date: Thu, 7 Jul 2022 00:44:10 -0700 Subject: [PATCH] add search for bilivideo --- gui/player_controller.go | 2 +- provider/bilivideo.go | 27 +++++++++++++++++++++++++-- provider/bilivideo_test.go | 13 +++++++++++++ provider/local.go | 28 ++++++++++++++++++++++++++-- provider/local_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 provider/local_test.go diff --git a/gui/player_controller.go b/gui/player_controller.go index 629881b..a730a87 100644 --- a/gui/player_controller.go +++ b/gui/player_controller.go @@ -199,7 +199,7 @@ func registerPlayControllerHandler() { PlayController.SetDefaultCover() } else { uri, err := storage.ParseURI(media.Cover) - if err != nil { + if err != nil || uri == nil { l().Warn("fail to load parse cover url", media.Cover) } // async update diff --git a/provider/bilivideo.go b/provider/bilivideo.go index a3eafef..d563354 100644 --- a/provider/bilivideo.go +++ b/provider/bilivideo.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/jinzhu/copier" "github.com/tidwall/gjson" + "net/url" "regexp" ) @@ -23,7 +24,7 @@ func _newBilibiliVideo() *BilibiliVideo { return &BilibiliVideo{ InfoApi: "https://api.bilibili.com/x/web-interface/view/detail?bvid=%s&aid=&jsonp=jsonp", FileApi: "https://api.bilibili.com/x/player/playurl?type=&otype=json&fourk=1&qn=32&avid=&bvid=%s&cid=%s", - SearchApi: "", + SearchApi: "https://api.bilibili.com/x/web-interface/search/type?search_type=video&page=1&keyword=%s", BVRegex: regexp.MustCompile("^BV[0-9A-Za-z]+"), IdRegex: regexp.MustCompile("^BV[0-9A-Za-z]+(\\?p=[0-9]+)?"), PageRegex: regexp.MustCompile("p=[0-9]+"), @@ -78,7 +79,29 @@ func (b *BilibiliVideo) FormatPlaylistUrl(uri string) string { } func (b *BilibiliVideo) Search(keyword string) ([]*player.Media, error) { - return nil, ErrorExternalApi + resp := httpGetString(fmt.Sprintf(b.SearchApi, url.QueryEscape(keyword)), nil) + if resp == "" { + return nil, ErrorExternalApi + } + jresp := gjson.Parse(resp) + if jresp.Get("code").String() != "0" { + return nil, ErrorExternalApi + } + result := make([]*player.Media, 0) + r := regexp.MustCompile("]*>") + jresp.Get("data.result").ForEach(func(key, value gjson.Result) bool { + result = append(result, &player.Media{ + Title: r.ReplaceAllString(value.Get("title").String(), ""), + Cover: "https:" + value.Get("pic").String(), + Artist: value.Get("author").String(), + Meta: Meta{ + Name: b.GetName(), + Id: value.Get("bvid").String(), + }, + }) + return true + }) + return result, nil } func (b *BilibiliVideo) UpdateMedia(media *player.Media) error { diff --git a/provider/bilivideo_test.go b/provider/bilivideo_test.go index a74289e..c10957c 100644 --- a/provider/bilivideo_test.go +++ b/provider/bilivideo_test.go @@ -84,3 +84,16 @@ func TestBV_GetMusic2(t *testing.T) { //fmt.Println(media) fmt.Println(media.Url) } + +func TestBV_Search(t *testing.T) { + var api MediaProvider = BilibiliVideoAPI + result, err := api.Search("家有女友") + if err != nil { + fmt.Println(1, err) + return + } + fmt.Println(len(result)) + for _, r := range result { + fmt.Println(r.Artist) + } +} diff --git a/provider/local.go b/provider/local.go index b320c08..292f852 100644 --- a/provider/local.go +++ b/provider/local.go @@ -1,8 +1,18 @@ package provider -import "AynaLivePlayer/player" +import ( + "AynaLivePlayer/config" + "AynaLivePlayer/player" + "os" +) + +type _LocalPlaylist struct { + Name string + Medias []*player.Media +} type Local struct { + Playlists []_LocalPlaylist } var LocalAPI *Local @@ -13,12 +23,26 @@ func init() { } func _newLocal() *Local { - return &Local{} + l := &Local{Playlists: make([]_LocalPlaylist, 0)} + if err := os.MkdirAll(config.Provider.LocalDir, 0755); err != nil { + return l + } + + return l } func (l *Local) GetName() string { return "local" } +func (l *Local) MatchMedia(keyword string) *player.Media { + //TODO implement me + panic("implement me") +} + +func (l *Local) UpdateMediaLyric(media *player.Media) error { + //TODO implement me + panic("implement me") +} func (l *Local) FormatPlaylistUrl(uri string) string { return "" diff --git a/provider/local_test.go b/provider/local_test.go new file mode 100644 index 0000000..674d550 --- /dev/null +++ b/provider/local_test.go @@ -0,0 +1,25 @@ +package provider + +import ( + "fmt" + "io/ioutil" + "testing" +) + +func TestLocal_Read(t *testing.T) { + items, _ := ioutil.ReadDir(".") + for _, item := range items { + if item.IsDir() { + subitems, _ := ioutil.ReadDir(item.Name()) + for _, subitem := range subitems { + if !subitem.IsDir() { + // handle file there + fmt.Println(item.Name() + "/" + subitem.Name()) + } + } + } else { + // handle file there + fmt.Println(item.Name()) + } + } +}