Skip to content

Commit 8658d04

Browse files
committed
fix: 修复解析网易带有#地址错误的问题
1 parent 614dee8 commit 8658d04

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

musicapi/musicapi.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ func NeteaseUrlToMp3(urlstr string) string {
1616
if err != nil {
1717
return ""
1818
}
19-
q := u.Query()
20-
musicId := q.Get("id")
19+
var q url.Values
20+
var musicId string
21+
if u.Fragment != "" {
22+
q, err = url.ParseQuery(u.Fragment)
23+
if err != nil {
24+
return ""
25+
}
26+
musicId = q.Get("/song?id")
27+
} else {
28+
q = u.Query()
29+
musicId = q.Get("id")
30+
}
2131
if musicId != "" {
2232
return toMp3UrlWithId(musicId)
2333
}

musicapi/musicapi_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package musicapi
33
import "testing"
44

55
var urls = []string{
6+
"https://music.163.com/#/song?id=4875306",
67
"https://music.163.com/song/4875306/",
78
"https://music.163.com/song?id=4875306",
89
"https://music.163.com/song/4875306",
@@ -15,7 +16,7 @@ func TestNeteaseUrlToMp3(t *testing.T) {
1516
for _, urlstr := range urls {
1617
res := NeteaseUrlToMp3(urlstr)
1718
if res != expect {
18-
t.Fatalf("Expect %s, Got %s", expect, res)
19+
t.Fatalf("Test %s Expect %s, Got %s", urlstr, expect, res)
1920
}
2021
}
2122
}

0 commit comments

Comments
 (0)