Skip to content

Commit

Permalink
修复编码问题,增加来源列
Browse files Browse the repository at this point in the history
  • Loading branch information
ankikong committed Oct 3, 2019
1 parent 670a910 commit 5b8f4c2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/*
*.exe
bin/*
cookie/*
3 changes: 3 additions & 0 deletions download.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -32,13 +33,15 @@ func Download(url, name, path string) error {
log.Println("create file fail:", err.Error())
return err
}
fmt.Println("start download:" + name)
buf := make([]byte, 262144)
for {
len, err := rs.Body.Read(buf)
file.Write(buf[:len])
if err != nil {
break
}
fmt.Print(".")
}
file.Close()
rs.Body.Close()
Expand Down
13 changes: 12 additions & 1 deletion kugou/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type kugouSearchPerResult struct {
FileName string `json:"FileName"`
ArtistName string `json:"SingerName"`
AlbumName string `json:"AlbumName"`
Source string
}

func (kg kugouSearchPerResult) GetFileName() string {
Expand All @@ -39,6 +40,10 @@ func (kg kugouSearchPerResult) GetAlbumName() string {
return kg.AlbumName
}

func (kg kugouSearchPerResult) GetSource() string {
return kg.Source
}

func (kg kugouSearchPerResult) GetUrl(br int) songBean.SongInfo {
if br == 990 && kg.SQHash != "" {
return GetSongUrl([]string{kg.SQHash})[0]
Expand Down Expand Up @@ -84,7 +89,10 @@ func GetSongUrl(ids []string) []songBean.SongInfo {
ansRet[index].SongBr = song.SongBr
ansRet[index].SongName = song.SongName
ansRet[index].SongSize = song.SongSize
ansRet[index].SongUrl = song.Urls[0]

if len(song.Urls) > 0 {
ansRet[index].SongUrl = song.Urls[0]
}
index++
}
return ansRet
Expand All @@ -96,5 +104,8 @@ func Search(word string) []kugouSearchPerResult {
// fmt.Println(string(rs))
var ans kugouSearchResult
json.Unmarshal(rs, &ans)
for i := 0; i < len(ans.Data.List); i++ {
ans.Data.List[i].Source = "KuGou"
}
return ans.Data.List
}
2 changes: 1 addition & 1 deletion kugou/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func TestSearch(t *testing.T) {
rs := Search("claris")
fmt.Println(rs)
rs[0].GetUrl(320)
var val songBean.SongUrl = rs[0]
var val songBean.SongData = rs[0]
fmt.Println(val.GetUrl(320))
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func search(text string) {
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"编号", "歌名", "歌手", "专辑"})
t.AppendHeader(table.Row{"编号", "歌名", "歌手", "专辑", "来源"})
// fmt.Println("歌名\t歌手\t专辑")
// fmt.Printf("%-26s%-26s%-26s\n", "歌名", "歌手", "专辑")
index := 0
for _, rs := range result {
t.AppendRow(table.Row{index, rs.GetFileName(), rs.GetArtistName(), rs.GetAlbumName()})
t.AppendRow(table.Row{index, rs.GetFileName(), rs.GetArtistName(), rs.GetAlbumName(), rs.GetSource()})
index++
// fmt.Printf("%-26s%-26s%-26s\n", rs.GetFileName(), rs.GetArtistName(), rs.GetAlbumName())
}
Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"encoding/json"
"fmt"
"testing"

"github.com/ankikong/goMusic/kugou"
)

func TestKuGou(t *testing.T) {
rs := kugou.Search("Hello Alone")
for _, per := range rs {
fmt.Println(per.GetUrl(320))
}
fmt.Println(rs)
}

type tt struct {
Key string `json:"key"`
}

func TestSearch(t *testing.T) {
a := new(tt)
a.Key = "你好"
rs, _ := json.Marshal(a)
fmt.Println(string(rs), rs)
// search("トゥルーエンド プレイヤー")
}
21 changes: 15 additions & 6 deletions netease/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"strings"

Expand Down Expand Up @@ -32,13 +33,12 @@ func doHttp(method, Url, data, encryptoMethod string) string {
} else {
rs = nil
}
str := new(strings.Builder)
if rs != nil {
param := url.Values{}
for k, v := range rs {
str.WriteString(k + "=" + v + "&")
param.Add(k, v)
}
data = str.String()
data = data[:len(data)-1]
data = param.Encode()
}
req, _ := http.NewRequest(method, Url, strings.NewReader(data))
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
Expand Down Expand Up @@ -119,6 +119,7 @@ type neteaseSearchResult struct {
FileName string
ArtistName string
AlbumName string
Source string
SongId int
}

Expand All @@ -134,12 +135,18 @@ func (kg neteaseSearchResult) GetAlbumName() string {
return kg.AlbumName
}

func (kg neteaseSearchResult) GetSource() string {
return kg.Source
}

func (nt neteaseSearchResult) GetUrl(br int) songBean.SongInfo {
songId := fmt.Sprint(nt.SongId)
if br != 990 && br != 320 && br != 192 && br != 128 {
br = 320
}
return GetSongUrl([]string{songId}, br)[0]
rs := GetSongUrl([]string{songId}, br)[0]
rs.SongName = nt.FileName
return rs
}

func Search(text string) []neteaseSearchResult {
Expand All @@ -154,6 +161,7 @@ func Search(text string) []neteaseSearchResult {
var tmpAns neteaseSearch
err := json.Unmarshal([]byte(ans), &tmpAns)
if err != nil {
log.Println(ans)
log.Println(err)
}
ansRet := make([]neteaseSearchResult, len(tmpAns.Result.Results))
Expand All @@ -169,8 +177,9 @@ func Search(text string) []neteaseSearchResult {
if len(tmpName) > 1 {
tmpName = tmpName[:len(tmpName)-1]
}
ansRet[index].Source = "netease"
ansRet[index].ArtistName = tmpName
ansRet[index].FileName = result.SongName
ansRet[index].FileName = tmpName + " - " + result.SongName
ansRet[index].SongId = result.SongId
index++
}
Expand Down
8 changes: 3 additions & 5 deletions netease/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"math/big"
"regexp"
"testing"

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

func TestRsaEncrypt(t *testing.T) {
Expand Down Expand Up @@ -70,10 +68,10 @@ func TestExp(t *testing.T) {
}

func TestSearch(t *testing.T) {
rs := Search("claris")
rs := Search("トゥルーエンド プレイヤー")
fmt.Println(rs)
var val songBean.SongData = rs[0]
fmt.Println(val.GetUrl(320))
// var val songBean.SongData = rs[0]
// fmt.Println(val.GetUrl(320))
}

// func TestAesEncryptCBC(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions songBean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type SongData interface {
GetArtistName() string
// 获取歌单名字
GetAlbumName() string
// 获取歌曲来源
GetSource() string
}

0 comments on commit 5b8f4c2

Please sign in to comment.