Skip to content

Commit ad41387

Browse files
authored
Support choose an album from a list (#2)
1 parent 36b7900 commit ad41387

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

main.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@ func main() {
2020
var albumID int
2121
if result, err := broadcast.Search(keyword); err != nil {
2222
panic(err)
23+
} else if len(result.Data.AlbumViews.Albums) == 0 {
24+
fmt.Println("not found any albums by keyword:", keyword)
25+
return
2326
} else {
24-
if len(result.Data.AlbumViews.Albums) > 0 {
25-
album := result.Data.AlbumViews.Albums[0].AlbumInfo
26-
albumID = album.ID
27+
albumTitle := make([]string, 0)
28+
albumMap := make(map[string]int, 0)
29+
for _, v := range result.Data.AlbumViews.Albums {
30+
albumTitle = append(albumTitle, v.AlbumInfo.Title)
31+
albumMap[v.AlbumInfo.Title] = v.AlbumInfo.ID
32+
}
2733

28-
fmt.Println("find album", album.Title)
29-
} else {
30-
fmt.Println("not found any albums by keyword:", keyword)
34+
selector := &survey.Select{
35+
Message: "Select a album",
36+
Options: albumTitle,
37+
}
38+
var choose string
39+
if err = survey.AskOne(selector, &choose); err != nil {
3140
return
3241
}
42+
albumID = albumMap[choose]
3343
}
3444

3545
tracks, err := broadcast.GetTrackList(albumID, 1, false)
@@ -54,15 +64,6 @@ func main() {
5464
fmt.Println("start to play", choose)
5565
trackInfo := trackMap[choose]
5666
play(trackInfo)
57-
58-
//if resp, err := http.Get(playURL); err != nil {
59-
// fmt.Println(err)
60-
//} else {
61-
// data, _ := io.ReadAll(resp.Body)
62-
// buffer := bytes.NewReader(data)
63-
//
64-
// play(playio.SeekerWithoutCloser(buffer))
65-
//}
6667
}
6768
}
6869

pkg/broadcast/core.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111

1212
func Search(keyword string) (result *AlbumSearchResult, err error) {
1313
keyword = url.QueryEscape(keyword)
14-
apiurl := fmt.Sprintf("https://m.ximalaya.com/m-revision/page/search?kw=%s&core=album&page=1&rows=10", keyword)
14+
apiurl := fmt.Sprintf("https://m.ximalaya.com/m-revision/page/search?kw=%s&core=album&page=1&rows=1000", keyword)
1515
resp, err := HttpGet(apiurl, Android)
1616
if err != nil {
17-
return nil, fmt.Errorf("无法获取专辑信息: %v", err)
17+
return nil, fmt.Errorf("cannot found albums by keyword: %s, error %v", keyword, err)
1818
}
1919
defer resp.Body.Close()
2020

2121
result = &AlbumSearchResult{}
2222
err = jsoniter.NewDecoder(resp.Body).Decode(result)
2323
if err != nil {
24-
return nil, fmt.Errorf("无法获取专辑信息: 无法解析Json: %v", err)
24+
return nil, fmt.Errorf("failed to parse json: %v", err)
2525
}
2626
return
2727
}
@@ -151,7 +151,7 @@ func GetAudioInfoListByPageID(albumID, pageID int) (playlist *Playlist, err erro
151151

152152
func GetTrackList(albumID, pageID int, isAsc bool) (tracks *TrackList, err error) {
153153
url := fmt.Sprintf(
154-
"https://mobile.ximalaya.com/mobile/v1/album/track/ts-%d?ac=WIFI&albumId=%d&device=android&isAsc=%t&pageId=%d&pageSize=200",
154+
"https://mobile.ximalaya.com/mobile/v1/album/track/ts-%d?ac=WIFI&albumId=%d&device=android&isAsc=%t&pageId=%d&pageSize=1000",
155155
time.Now().Unix(), albumID, isAsc, pageID)
156156
resp, err := HttpGet(url, Android)
157157
if err != nil {

0 commit comments

Comments
 (0)