//go下载qq音乐 
package main import (
_ "fmt"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
) //练习json解析
//var jsonStr = `{
//"code":0,
//"data":{
//"keyword":"桥边娘",
//"priority":0,
//"qc":[{"text":"桥边姑娘","type":1}],"semantic":{"curnum":0,"curpage":1,"list":[],"totalnum":0},
//"song":{
//"curnum":2,
//"curpage":1,
//"list":[
//{"action":{"alert":2,"icons":8527740,"msg":14,"switch":17413891},
//"album":{"id":9160555,"mid":"0037Yq3H3RznaX","name":"桥边姑娘","pmid":"0037Yq3H3RznaX_1","subtitle":"","title":"桥边姑娘","title_hilight":"<em>桥边姑娘</em>"},"chinesesinger":0,"desc":"","desc_hilight":"","docid":"126909805796459913","file":{"b_30s":0,"e_30s":60000,"media_mid":"001zLvbN1NYMuv","size_128":2931359,"size_128mp3":2931359,"size_320":7328081,"size_320mp3":7328081,"size_aac":4442882,"size_ape":0,"size_dts":0,"size_flac":20665526,"size_ogg":4495348,"size_try":960887,"strMediaMid":"001zLvbN1NYMuv","try_begin":56459,"try_end":84621},"fnote":4009,"genre":0,"grp":[],"id":244625442,"index_album":1,"index_cd":0,"interval":183,"isonly":1,"ksong":{"id":6374470,"mid":"002Rn3IH1mlv0E"},"language":0,"lyric":"","lyric_hilight":"","mid":"001zLvbN1NYMuv","mv":{"id":0,"vid":""},"name":"桥边姑娘","newStatus":1,"nt":87800508,"pay":{"pay_down":1,"pay_month":1,"pay_play":0,"pay_status":0,"price_album":0,"price_track":200,"time_free":0},"pure":0,
//"singer":[{"id":1294564,"mid":"0041vdG63lbLge","name":"海伦","title":"海伦","title_hilight":"海伦","type":0,"uin":0}],
//"status":0,"subtitle":"","t":1,"tag":11,"time_public":"2019-11-09",
//"title":"桥边姑娘",
//"title_hilight":"<em>桥边姑娘</em>",
//"type":0,
//"url":"http://stream10.qqmusic.qq.com/244625442.wma","ver":0,
//"volume":{"gain":-7.768000125885010,"lra":9.609000205993652,"peak":0.9330000281333923}},
//{"action":{"alert":11,"icons":135752,"msg":0,"switch":77587},
//"album":{"id":0,"mid":"","name":"","pmid":"","subtitle":"","title":"","title_hilight":""},
//"chinesesinger":0,"desc":"","desc_hilight":"","docid":"8751829731153775463",
//"file":
//{"b_30s":0,"e_30s":0,"media_mid":"001fkaP431i0UO","size_128":2956833,"size_128mp3":2956833,"size_320":7391798,"size_320mp3":7391798,"size_aac":4415052,"size_ape":0,"size_dts":0,"size_flac":0,"size_ogg":3851434,"size_try":0,"strMediaMid":"001fkaP431i0UO","try_begin":0,"try_end":0},
//"fnote":8013,"genre":0,"grp":[],"id":127400312,"index_album":0,"index_cd":0,"interval":184,"isonly":0,
//"ksong":{"id":0,"mid":""},
//"language":0,"lyric":"","lyric_hilight":"",
//"mid":"002rI3Yb1nZjPy",
//"mv":{"id":0,"vid":""},
//"name":"桥边姑娘","newStatus":1,"nt":1324451929,"pay":{"pay_down":0,"pay_month":0,"pay_play":0,"pay_status":0,"price_album":0,"price_track":0,"time_free":0},"pure":0,
//"singer":[{"id":3209257,"mid":"001a520s0HBgIS","name":"张茜","title":"张茜","title_hilight":"张茜","type":1,"uin":0}],"status":0,"subtitle":"","t":1,"tag":0,"time_public":"","title":"桥边姑娘","title_hilight":"<em>桥边姑娘</em>","type":0,"url":"http://stream10.qqmusic.qq.com/127400312.wma","ver":0,
//"volume":{"gain":0.0,"lra":0.0,"peak":0.0}}],"totalnum":600},"tab":0,"taglist":[],"totaltime":0,"zhida":{"type":null}},
//"message":"",
//"notice":"",
//"subcode":0,
//"time":1578533969,
//"tips":""
//}` type Datas struct {
Code int `json:"code"`
Message string `json:"message"`
Time int `json:"time"`
Data Data `json:"data"`
}
type Data struct {
Keyword string `json:"keyword"`
Song Song `json:"song"`
} type Song struct {
Curnum int `json:"curnum"`
List []List `json:"list"`
} type List struct {
Title string `json:"title"`
TimePublic string `json:"time_public"`
Mid string `json:"mid"`
Name string `json:"name"`
ID int `json:"id"`
File File `json:"file"`
Album Album `json:"album"`
Singer []Singer `json:"singer"`
} type Singer struct {
Name string `json:"name"`
} type File struct {
StrMediaMid string `json:"strMediaMid"`
} type Album struct {
Name string `json:"name"`
} type Lyric struct {
UserIp string `json:"userip"`
Data LyData `json:"data"`
}
type LyData struct {
Expiration int `json:"expiration"`
Items []Item `json:"items"`
}
type Item struct {
Vkey string `json:"vkey"`
} /**
下载QQ音乐
@param string keyword 歌曲名称关键词
@param int num 需要下载的歌曲数量
*/
func download(keyword string, num int) {
pathPwd, _ := os.Getwd()
url := "https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song" +
"&searchid=57124856116396257&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&" +
"n=" + strconv.Itoa(num) + "&w=" + keyword + "&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
resp, err := http.Get(url)
if err != nil {
println("get出错", err)
return
} defer resp.Body.Close()
jsonstr, err := ioutil.ReadAll(resp.Body)
if err != nil {
println(err)
return
}
jsonStr := jsonstr
strMediaMids := []string{}
songmids := []string{}
songnames := []string{}
singers := []string{}
albumns := []string{}
songid := []int{}
data := Datas{}
var json2 = jsoniter.ConfigCompatibleWithStandardLibrary
_ = json2.Unmarshal([]byte(jsonStr), &data)
//fmt.Println(data.Data.Song.List)
for _, v := range data.Data.Song.List {
strMediaMids = append(strMediaMids, v.File.StrMediaMid)
songmids = append(songmids, v.Mid)
songnames = append(songnames, v.Name)
singers = append(singers, v.Singer[0].Name)
albumns = append(albumns, v.Album.Name)
songid = append(songid, v.ID)
} for k, v := range strMediaMids {
//将strMediaMids和songmids重新组合到url中
url2 := "https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?&jsonpCallback=MusicJsonCallback&format=json&cid=205361747&songmid=" + songmids[k] + "&filename=C400" + v + ".m4a&guid=6612300644"
println(url2)
resp, err := http.Get(url2)
if err != nil {
println("get出错", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("出错了", err)
return
}
var filter = Lyric{}
json2.Unmarshal([]byte(body), &filter)
vkey := filter.Data.Items[0].Vkey
//这是最终的歌曲url
song_url := "http://dl.stream.qqmusic.qq.com/C400" + v + ".m4a?vkey=" + vkey + "&guid=6612300644&uin=0&fromtag=66"
//歌词url
lyric_url := "https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric.fcg?nobase64=1&musicid=" + strconv.Itoa(songid[k]) + "&callback=jsonp1&g_tk=5381&jsonpCallback=json&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
client := &http.Client{}
refer := "https://y.qq.com/n/yqq/song/" + songmids[k] + ".html"
request, err := http.NewRequest("GET", lyric_url, nil)
request.Header.Add("Referer", refer)
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36")
response, err := client.Do(request)
defer response.Body.Close()
if err != nil {
return
}
body2, err := ioutil.ReadAll(response.Body)
if err != nil {
println("出错了", err)
return
}
println(string(body2))
println("正在下载--->" + songnames[k])
res, err := http.Get(song_url)
if err != nil {
panic(err)
}
//f, err := os.Create(pathPwd + "/" + songnames[k] + strconv.Itoa(k+1) + ".mp3")
f, err := os.Create(pathPwd + "/" + songnames[k] + ".mp3")
if err != nil {
panic(err)
}
io.Copy(f, res.Body)
println("下载第" + strconv.Itoa(k+1) + "首<" + songnames[k] + ">已经保存成功嘿嘿嘿~~~") }
} func main() {
//传参形式
//if len(os.Args) <= 1 {
// fmt.Println("请输入需要下载的音乐名称.")
// return
//}
//fmt.Println("音乐名称 ", os.Args[1])
//num := os.Args[2]
//println(num)
//todo:可以加入协程来耍耍
download("冯提莫", 3)
}

  go版本下载qq音乐 略有粗糙 见谅 见谅

05-11 20:57