func GetMusics(singerId string) (Musics []Song, err error) { singerId = strings.TrimSpace(singerId) if singerId == "" { err = errors.New("singer_id is empty") return } var limit = 100 var offset = 0 var tingResult TingResult for { req := httplib.Get("http://tingapi.ting.baidu.com/v1/restserver/ting") req.Param("method", "baidu.ting.artist.getSongList") req.Param("format", "json") req.Param("tinguid", singerId) req.Param("offset", strconv.Itoa(offset)) req.Param("limits", strconv.Itoa(limit)) offset += limit if err := req.ToJson(&tingResult); err != nil { break } else { for _, song := range tingResult.Songlist { Musics = append(Musics, song) } if tingResult.HaveMore == 0 { break } } } return }
func (artist *Artist) Supplement() (err error) { if strings.TrimSpace(artist.ArtistId) == "" { err = errors.New("artistid is empty") return } req := httplib.Get("http://tingapi.ting.baidu.com/v1/restserver/ting") req.Param("from", "qianqian") req.Param("version", "2.1.0") req.Param("method", "baidu.ting.artist.getinfo") req.Param("format", "json") req.Param("tinguid", artist.ArtistId) if err = req.ToJson(artist); err != nil { return } return }
func (song *Song) Supplement() (err error) { var musicSupplement MusicSupplement var mapMusicSupplement = make(map[int]MusicDownloadInfo, 0) var mapMusicSupplementKeys = []int{} req := httplib.Get("http://tingapi.ting.baidu.com/v1/restserver/ting") req.Param("from", "qianqian") req.Param("version", "2.1.0") req.Param("method", "baidu.ting.song.getInfos") req.Param("format", "json") req.Param("songid", song.SongId) req.Param("e", "JoN56kTXnnbEpd9MVczkYJCSx/E1mkLx+PMIkTcOEu4=") if err = req.ToJson(&musicSupplement); err != nil { fmt.Println(req.String()) return } if musicSupplement.ErrorCode != 22000 { err = errors.New("err: " + musicSupplement.ErrorMessage) return } for _, musicDownloadInfo := range musicSupplement.SongUrl.MusicDownloadInfos { if musicDownloadInfo.FileDuration == 0 || strings.Contains(musicDownloadInfo.FileLink, "http://") == false || musicDownloadInfo.FileSize == 0 { continue } if _, hit := mapMusicSupplement[musicDownloadInfo.FileSize]; hit == true { continue } mapMusicSupplementKeys = append(mapMusicSupplementKeys, musicDownloadInfo.FileSize) mapMusicSupplement[musicDownloadInfo.FileSize] = musicDownloadInfo } if len(mapMusicSupplementKeys) == 0 { err = errors.New("not find music download addr") return } sort.Sort(sort.Reverse(sort.IntSlice(mapMusicSupplementKeys))) song.FileDuration = strconv.Itoa(mapMusicSupplement[mapMusicSupplementKeys[0]].FileDuration) song.FileLink = mapMusicSupplement[mapMusicSupplementKeys[0]].FileLink song.FileSize = mapMusicSupplement[mapMusicSupplementKeys[0]].FileSize return }
func FromBaiduGetSingerInfo(singerName string) (artist Artist, err error) { singerName = strings.TrimSpace(singerName) if singerName == "" { err = errors.New("sinegr_name is empty") return } var suggestionResult SuggestionResult req := httplib.Get("http://sug.music.baidu.com/info/suggestion") req.Param("format", "json") req.Param("word", singerName) req.Param("version", "2") req.Param("from", "0") if err = req.ToJson(&suggestionResult); err != nil { return } for _, row := range suggestionResult.Data.Artists { if row.ArtistName == singerName { artist = row return } } return }