From 3a32ba84977e2b31ad62b8a48e2c1cd61611c92b Mon Sep 17 00:00:00 2001 From: foamzou Date: Tue, 6 Sep 2022 21:32:43 +0800 Subject: [PATCH] fix(bilibili): search api needs the cookie --- LATEST_VERSION | 2 +- processor/bilibili/search.go | 29 +++++++++++++++++++++++++++-- processor/bilibili/search_entity.go | 9 +++++++++ processor/download.go | 7 ++++--- version/version.go | 4 ++-- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/LATEST_VERSION b/LATEST_VERSION index 967b33f..08456a4 100644 --- a/LATEST_VERSION +++ b/LATEST_VERSION @@ -1 +1 @@ -0.2.7 \ No newline at end of file +0.2.8 \ No newline at end of file diff --git a/processor/bilibili/search.go b/processor/bilibili/search.go index 46a772d..e19c1bd 100644 --- a/processor/bilibili/search.go +++ b/processor/bilibili/search.go @@ -11,14 +11,39 @@ import ( ) const APISearch = "https://api.bilibili.com/x/web-interface/search/all/v2?page=1&page_size=10&platform=pc&single_column=0&keyword=%s&preload=true" +const APIFinger = "https://api.bilibili.com/x/frontend/finger/spi" +const SearchPageUrl = "https://search.bilibili.com/all" func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) { + cookie, err := utils.GetCookie(SearchPageUrl, map[string]string{ + "User-Agent": consts.UAMac, + "Referer": "https://search.bilibili.com/", + }, false) + if err != nil { + return nil, err + } + + fingerJsonStr, err := utils.HttpGet(APIFinger, map[string]string{ + "User-Agent": consts.UAMac, + "Referer": "https://search.bilibili.com/all", + "Cookie": cookie, + }) + if err != nil { + return nil, err + } + var fingerInfo FingerInfo + err = json.Unmarshal([]byte(fingerJsonStr), &fingerInfo) + if err != nil { + return nil, err + } + cookie = fmt.Sprintf("buvid3=%s; buvid4=%s; %s", fingerInfo.Data.B3, fingerInfo.Data.B4, cookie) + var searchSongItems []*meta.SearchSongItem api := fmt.Sprintf(APISearch, url.QueryEscape(c.Opts.Search.Keyword)) - jsonStr, err := utils.HttpGet(api, map[string]string{ "User-Agent": consts.UAMac, - "Referer": "https://search.bilibili.com/", + "Referer": "https://search.bilibili.com/all", + "Cookie": cookie, }) if err != nil { return nil, err diff --git a/processor/bilibili/search_entity.go b/processor/bilibili/search_entity.go index f4f62b8..29c1c33 100644 --- a/processor/bilibili/search_entity.go +++ b/processor/bilibili/search_entity.go @@ -55,3 +55,12 @@ type SearchSongResponse struct { } `json:"result"` } `json:"data"` } + +type FingerInfo struct { + Code int `json:"code"` + Data struct { + B3 string `json:"b_3"` + B4 string `json:"b_4"` + } `json:"data"` + Message string `json:"message"` +} diff --git a/processor/download.go b/processor/download.go index ea9cc65..70a688d 100644 --- a/processor/download.go +++ b/processor/download.go @@ -2,14 +2,15 @@ package processor import ( "fmt" + "os" + "path" + "strings" + "github.com/foamzou/audio-get/consts" "github.com/foamzou/audio-get/ffmpeg" "github.com/foamzou/audio-get/logger" "github.com/foamzou/audio-get/meta" "github.com/foamzou/audio-get/utils" - "os" - "path" - "strings" ) func (p *Processor) download(mediaMeta *meta.MediaMeta) error { diff --git a/version/version.go b/version/version.go index e582c66..1278352 100644 --- a/version/version.go +++ b/version/version.go @@ -5,8 +5,8 @@ import ( ) const ( - BuildCode = 12 - BuildName = "0.2.7" + BuildCode = 13 + BuildName = "0.2.8" Repo = "https://github.com/foamzou/media-get" )