feat: add translated and phonetic lyric previews
This commit is contained in:
+47
-2
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -15,6 +17,49 @@ func TestUnwrapJSONP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQQClientMusicuLyricsRequestsTranslationAndPhonetics(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost || r.Header.Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.Header.Get("Content-Type"))
|
||||
}
|
||||
var body struct {
|
||||
Request struct {
|
||||
Module string `json:"module"`
|
||||
Method string `json:"method"`
|
||||
Param struct {
|
||||
MID string `json:"songMID"`
|
||||
Trans int `json:"trans"`
|
||||
Roma int `json:"roma"`
|
||||
QRC int `json:"qrc"`
|
||||
} `json:"param"`
|
||||
} `json:"req_1"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body.Request.Module != "music.musichallSong.PlayLyricInfo" || body.Request.Method != "GetPlayLyricInfo" || body.Request.Param.MID != "0004jPDk2eB2dt" {
|
||||
t.Fatalf("unexpected request body: %#v", body)
|
||||
}
|
||||
if body.Request.Param.Trans != 1 || body.Request.Param.Roma != 1 || body.Request.Param.QRC != 0 {
|
||||
t.Fatalf("required lyric variants were not requested: %#v", body.Request.Param)
|
||||
}
|
||||
lyric := base64.StdEncoding.EncodeToString([]byte("[00:01.00]原文"))
|
||||
trans := base64.StdEncoding.EncodeToString([]byte("[00:01.00]translation"))
|
||||
_, _ = w.Write([]byte(`{"code":0,"req_1":{"code":0,"data":{"lyric":"` + lyric + `","trans":"` + trans + `"}}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := newQQClient()
|
||||
client.musicuEndpoint = server.URL
|
||||
result, err := client.musicuLyrics("0004jPDk2eB2dt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Lyric != "[00:01.00]原文" || result.Trans != "[00:01.00]translation" {
|
||||
t.Fatalf("unexpected lyrics: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQQClientSearchUsesFixedShape(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Origin") != "https://y.qq.com" || r.Header.Get("Referer") != "https://y.qq.com/" {
|
||||
@@ -44,8 +89,8 @@ func TestQQClientRejectsOversizedResponse(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
client := newQQClient()
|
||||
client.lyricsEndpoint = server.URL
|
||||
_, err := client.Lyrics("0004jPDk2eB2dt")
|
||||
client.musicuEndpoint = server.URL
|
||||
_, err := client.musicuLyrics("0004jPDk2eB2dt")
|
||||
if err == nil || !strings.Contains(err.Error(), "safety limit") {
|
||||
t.Fatalf("expected response limit error, got %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user