Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ankikong committed Mar 5, 2020
1 parent 4540656 commit e73a6b8
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 96 deletions.
Binary file modified README.md
Binary file not shown.
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"strconv"
"strings"

"github.com/ankikong/goMusic/qq"
"github.com/ankikong/goMusic/tool"

"github.com/ankikong/goMusic/kugou"
"github.com/ankikong/goMusic/songbean"
"github.com/ankikong/goMusic/provider/bilibili"

"github.com/ankikong/goMusic/netease"
"github.com/ankikong/goMusic/provider/qq"

"github.com/ankikong/goMusic/provider/kugou"
"github.com/ankikong/goMusic/provider/songbean"

"github.com/ankikong/goMusic/provider/netease"
"github.com/jedib0t/go-pretty/table"
)

Expand Down Expand Up @@ -62,7 +66,7 @@ func search(text string) {
}
rss := result[num].GetURL(320)
fmt.Println(rss.SongURL)
Download(rss.SongURL, result[num].GetFileName(), "")
tool.Download(rss.SongURL, result[num].GetFileName(), "")
}

func GetByNeteaseId(URL string) {
Expand All @@ -82,7 +86,7 @@ func GetByNeteaseId(URL string) {
}
rs := netease.GetSongURL([]string{fmt.Sprint(id)}, 320)[0]
fmt.Println("开始下载:", rs.SongName)
Download(rs.SongURL, rs.SongName, "")
tool.Download(rs.SongURL, rs.SongName, "")
}

func GetByQQId(URL string) {
Expand All @@ -95,9 +99,9 @@ func GetByQQId(URL string) {
fmt.Println("error: ", URL)
return
}
rs := qq.GetSongURL(id)
rs := qq.GetSongURL(id, "320")
fmt.Println("开始下载", rs.SongName)
Download(rs.SongURL, rs.SongName, "")
tool.Download(rs.SongURL, rs.SongName, "")
}

func main() {
Expand All @@ -115,8 +119,8 @@ func main() {
GetByNeteaseId(URL)
} else if strings.Contains(URL, "qq.com") {
GetByQQId(URL)
} else {

} else if strings.Contains(URL, "bilibili") {
bilibili.Deal(URL)
}
}
}
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get

RELEASE_VERSION=V0.0.2
RELEASE_VERSION=V0.1.0
BINARY_NAME=goMusic-$(RELEASE_VERSION)
LOCAL_BINARY_NAME=goMusic
BINARY_DIR=bin
Expand Down
79 changes: 34 additions & 45 deletions provider/kugou/api.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package kugou

import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"log"
"net/http"

"github.com/ankikong/goMusic/songbean"
"github.com/ankikong/goMusic/tool"

"github.com/ankikong/goMusic/provider/songbean"
)

type kugouSongURL struct {
Expand All @@ -18,7 +15,8 @@ type kugouSongURL struct {
SongName string `json:"fileName"`
}

type kugouSearchPerResult struct {
// SearchResult 酷狗搜索的结果
type SearchResult struct {
LQHash string `json:"FileHash"`
LQSize int `json:"FileSize"`
HQHash string `json:"HQFileHash"`
Expand All @@ -31,23 +29,28 @@ type kugouSearchPerResult struct {
Source string
}

func (kg kugouSearchPerResult) GetFileName() string {
// GetFileName 生成歌曲文件名
func (kg SearchResult) GetFileName() string {
return kg.FileName
}

func (kg kugouSearchPerResult) GetArtistName() string {
// GetArtistName 获取歌手名字
func (kg SearchResult) GetArtistName() string {
return kg.ArtistName
}

func (kg kugouSearchPerResult) GetAlbumName() string {
// GetAlbumName 获取专辑名
func (kg SearchResult) GetAlbumName() string {
return kg.AlbumName
}

func (kg kugouSearchPerResult) GetSource() string {
// GetSource 获取来源
func (kg SearchResult) GetSource() string {
return kg.Source
}

func (kg kugouSearchPerResult) GetURL(br int) songbean.SongInfo {
// GetURL 搜索结果中是不包含歌曲链接的,所以要获取链接就必须调用此方法
func (kg SearchResult) GetURL(br int) songbean.SongInfo {
var rs songbean.SongInfo
if br == 990 && kg.SQHash != "" {
rs = GetSongURL([]string{kg.SQHash})[0]
Expand All @@ -64,46 +67,28 @@ func (kg kugouSearchPerResult) GetURL(br int) songbean.SongInfo {
}

type kugouData struct {
List []kugouSearchPerResult `json:"lists"`
List []SearchResult `json:"lists"`
}
type kugouSearchResult struct {
Data kugouData
}

func MD5(text string) string {
data := []byte(text)
hash := md5.Sum(data)
return hex.EncodeToString(hash[:])
}

func doGet(URL string) []byte {
rs, err := http.Get(URL)
if err != nil {
log.Println(err)
return nil
}
defer rs.Body.Close()
buf := new(bytes.Buffer)
tmpBuf := make([]byte, 4096)
for {
len, err := rs.Body.Read(tmpBuf)
buf.Write(tmpBuf[:len])
if err != nil {
break
}
}
return buf.Bytes()
}

// GetSongURL 根据id来获取歌曲链接
func GetSongURL(ids []string) []songbean.SongInfo {
ansRet := make([]songbean.SongInfo, len(ids))
index := 0
for _, id := range ids {
tmpHash := MD5(id + "kgcloudv2")
tmpHash := tool.MD5(id + "kgcloudv2")
api := `http://trackercdn.kugou.com/i/v2/?key=` + tmpHash + `&hash=` + id + `&br=hq&appid=1005&pid=2&cmd=25&behavior=play`
tmpBuf := doGet(api)
tmpBuf, err := tool.DoHTTP("GET", api, "", "", "", "")
if err != nil {
return nil
}
var song kugouSongURL
json.Unmarshal(tmpBuf, &song)
err = json.Unmarshal([]byte(tmpBuf), &song)
if err != nil {
return nil
}
ansRet[index].SongBr = song.SongBr
ansRet[index].SongName = song.SongName
ansRet[index].SongSize = song.SongSize
Expand All @@ -116,12 +101,16 @@ func GetSongURL(ids []string) []songbean.SongInfo {
return ansRet
}

func Search(word string) []kugouSearchPerResult {
// Search 根据word搜索
func Search(word string) (ret []SearchResult) {
ret = *new([]SearchResult)
URL := `http://songsearch.kugou.com/song_search_v2?pagesize=5&keyword=` + word
rs := doGet(URL)
// fmt.Println(string(rs))
rs, err := tool.DoHTTP("GET", URL, "", "", "", "")
if err != nil {
return
}
var ans kugouSearchResult
json.Unmarshal(rs, &ans)
err = json.Unmarshal([]byte(rs), &ans)
for i := 0; i < len(ans.Data.List); i++ {
ans.Data.List[i].Source = "KuGou"
}
Expand Down
12 changes: 6 additions & 6 deletions provider/kugou/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"fmt"
"testing"

"github.com/ankikong/goMusic/songBean"
"github.com/ankikong/goMusic/provider/songbean"
)

func TestGetSongUrl(t *testing.T) {
rs := GetSongUrl([]string{"4D870D0DEB13AA6700BEECA513C6B03C", "40AD169093CDE5523A13DA8E7A09066B"})
func TestGetSongURL(t *testing.T) {
rs := GetSongURL([]string{"4D870D0DEB13AA6700BEECA513C6B03C", "40AD169093CDE5523A13DA8E7A09066B"})
fmt.Println(rs)
}

func TestSearch(t *testing.T) {
rs := Search("claris")
fmt.Println(rs)
rs[0].GetUrl(320)
var val songBean.SongData = rs[0]
fmt.Println(val.GetUrl(320))
rs[0].GetURL(320)
var val songbean.SongData = rs[0]
fmt.Println(val.GetURL(320))
}
27 changes: 20 additions & 7 deletions provider/netease/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

// module

func weapi(text string) (rs map[string]string, err error) {
func weapi(text string) (rs map[string]string) {
secretKey := make([]byte, 16)
for i := 0; i < 16; i++ {
secretKey[i] = byte(base62[rand.Int31n(62)])
Expand All @@ -46,7 +46,7 @@ func weapi(text string) (rs map[string]string, err error) {
data := tool.RsaEncrypt(secretKey, modulus)
rs = make(map[string]string)
rs["params"], rs["encSecKey"] = param, data
return rs, nil
return rs
}

func linuxAPI(text string) map[string]string {
Expand All @@ -71,7 +71,7 @@ type neteaseSongRes struct {
func doHTTP(method, URL, data, encryptoMethod string) string {
rs := make(map[string]string)
if encryptoMethod == "web" {
rs, _ = weapi(data)
rs = weapi(data)
} else if encryptoMethod == "linux" {
rs = linuxAPI(data)
} else {
Expand Down Expand Up @@ -115,14 +115,25 @@ func doHTTP(method, URL, data, encryptoMethod string) string {
func GetSongURL(ids []string, br int) []songbean.SongInfo {
input := `{"method":"POST","url":"https://music.163.com/api/song/enhance/player/url","params":{"ids":"[` +
strings.Join(ids, ",") + `]","br":` + fmt.Sprintf("%d", br*1000) + `}}`
rs := doHTTP("POST", "https://music.163.com/api/linux/forward", input, "linux")
// rs := doHTTP("POST", "https://music.163.com/api/linux/forward", input, "linux")

postData := tool.MapToURLParams(linuxAPI(input))

rs, err := tool.DoHTTP("POST", "https://music.163.com/api/linux/forward", postData, "application/x-www-form-urlencoded", "https://music.163.com", "")
if err != nil {
return nil
}
var ans neteaseSongRes
json.Unmarshal([]byte(rs), &ans)
ansRet := make([]songbean.SongInfo, len(ids))
reg, _ := regexp.Compile(`"title": "[^"]+`)
index := 0
for _, id := range ids {
tmpRs := doHTTP("GET", "https://music.163.com/song?id="+id, "", "null")
// tmpRs := doHTTP("GET", "https://music.163.com/song?id="+id, "", "null")
tmpRs, err := tool.DoHTTP("GET", "https://music.163.com/song?id="+id, "", "", "", "https://music.163.com")
if err != nil {
return nil
}
songname := reg.FindAllString(tmpRs, 1)[0][10:]
ansRet[index].SongBr = ans.Data[index].Br
ansRet[index].SongURL = ans.Data[index].URL
Expand Down Expand Up @@ -212,9 +223,11 @@ func Search(text string) []SearchResult {
query.Limit = 10
query.Offset = 0
rs, _ := json.Marshal(query)
ans := doHTTP("POST", "https://music.163.com/weapi/search/get", string(rs), "web")
// ans := doHTTP("POST", "https://music.163.com/weapi/search/get", string(rs), "web")
postData := tool.MapToURLParams(weapi(string(rs)))
ans, err := tool.DoHTTP("POST", "https://music.163.com/weapi/search/get", postData, "application/x-www-form-urlencoded", "https://music.163.com", "")
var tmpAns neteaseSearch
err := json.Unmarshal([]byte(ans), &tmpAns)
err = json.Unmarshal([]byte(ans), &tmpAns)
if err != nil {
log.Println(ans)
log.Println(err)
Expand Down
8 changes: 2 additions & 6 deletions provider/netease/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ import (
func TestWeapi(t *testing.T) {
// http://music.163.com/weapi/song/enhance/player/url?csrf_token=
testInput := `{"ids":["28528420","347230"],"br":320000,"csrf_token":""}`
param, err := weapi(testInput)
if err != nil {
t.Error(err)
} else {
fmt.Println(param)
}
param := weapi(testInput)
fmt.Println(param)
}

func TestLinuxApi(t *testing.T) {
Expand Down
Loading

0 comments on commit e73a6b8

Please sign in to comment.