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

feature(migu): support more detail of migu in the search API #16

Merged
merged 1 commit into from
Feb 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion LATEST_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.11
0.2.12
8 changes: 4 additions & 4 deletions processor/migu/fetch_song.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const (
ApiUrlSong = "https://c.musicapp.migu.cn/MIGUM2.0/v1.0/content/resourceinfo.do?copyrightId=%s&resourceType=2"
)

func (c *Core) fetchFromSong() (mediaMeta *meta.MediaMeta, err error) {
songId := utils.RegexSingleMatchIgnoreError(c.Opts.Url, `song/(.+)`, "0")
func (c *Core) fetchFromSong(url string) (mediaMeta *meta.MediaMeta, err error) {
songId := utils.RegexSingleMatchIgnoreError(url, `song/(.+)`, "0")
if songId == "0" {
return
}

songJson, err := utils.HttpGet(consts.SourceNameMigu, fmt.Sprintf(ApiUrlSong, songId), map[string]string{
"user-agent": consts.UAMac,
"referer": c.Opts.Url,
"referer": url,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -52,7 +52,7 @@ func (c *Core) fetchFromSong() (mediaMeta *meta.MediaMeta, err error) {
ResourceType: consts.ResourceTypeAudio,
Headers: map[string]string{
"user-agent": consts.UAMac,
"referer": c.Opts.Url,
"referer": url,
},
}

Expand Down
2 changes: 1 addition & 1 deletion processor/migu/migo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Core) Domains() []string {

func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error) {
if strings.Contains(c.Opts.Url, "/song/") {
return c.fetchFromSong()
return c.fetchFromSong(c.Opts.Url)
}
return nil, err
}
40 changes: 28 additions & 12 deletions processor/migu/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"sync"

"github.com/foamzou/audio-get/consts"
"github.com/foamzou/audio-get/meta"
Expand Down Expand Up @@ -40,20 +41,35 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
return nil, err
}

var mutex sync.Mutex
wg := sync.WaitGroup{}
for _, item := range searchSongResponse.Data.SongsData.Items {
artist := ""
if len(item.Singers) > 0 {
artist = item.Singers[0].Name
}
searchSongItems = append(searchSongItems, &meta.SearchSongItem{
Name: item.Name,
Artist: artist,
Album: item.Album.Name,
Duration: 0, // unknown in the API
Url: fmt.Sprintf("https://music.migu.cn/v3/music/song/%s", item.CopyrightId),
Source: consts.SourceNameMigu,
})
wg.Add(1)
item := item
go func() {
defer func() {
wg.Done()
}()
artist := ""
if len(item.Singers) > 0 {
artist = item.Singers[0].Name
}
songUrl := fmt.Sprintf("https://music.migu.cn/v3/music/song/%s", item.CopyrightId)
songMeta, _ := c.fetchFromSong(songUrl)
mutex.Lock()
searchSongItems = append(searchSongItems, &meta.SearchSongItem{
Name: item.Name,
Artist: artist,
Album: item.Album.Name,
Duration: songMeta.Duration,
Url: songUrl,
Source: consts.SourceNameMigu,
ResourceForbidden: songMeta.ResourceForbidden,
})
mutex.Unlock()
}()
}
wg.Wait()

return searchSongItems, nil
}
4 changes: 2 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

const (
BuildCode = 16
BuildName = "0.2.11"
BuildCode = 17
BuildName = "0.2.12"
Repo = "https://github.com/foamzou/media-get"
)

Expand Down