@@ -21,6 +21,15 @@ func GetHttpClient() *http.Client {
2121 return & http.Client {Transport : transport }
2222}
2323
24+ type LyricResponse struct {
25+ Errors []interface {} `json:"errors"`
26+ Data []struct {
27+ Attributes struct {
28+ TtmlLocalizations string `json:"ttmlLocalizations"`
29+ } `json:"attributes"`
30+ } `json:"data"`
31+ }
32+
2433func GetLyrics (adamID string , region string , language string , token string , musicToken string ) (string , error ) {
2534 req , err := http .NewRequest ("GET" , fmt .Sprintf ("https://amp-api.music.apple.com/v1/catalog/%s/songs/%s/syllable-lyrics?l[lyrics]=%s&extend=ttmlLocalizations&l[script]=en-Latn" , region , adamID , language ), nil )
2635 if err != nil {
@@ -41,14 +50,20 @@ func GetLyrics(adamID string, region string, language string, token string, musi
4150 if err != nil {
4251 return "" , err
4352 }
44- var respJson map [ string ][] interface {}
45- if err := json .Unmarshal (respBody , & respJson ); err != nil {
53+ var result LyricResponse
54+ if err := json .Unmarshal (respBody , & result ); err != nil {
4655 return "" , err
4756 }
48- if respJson ["errors" ] != nil {
49- return "" , errors .New (fmt .Sprintf ("failed to get lyrics: %s" , respJson ["errors" ]))
57+ if len (result .Errors ) > 0 {
58+ return "" , fmt .Errorf ("failed to get lyrics: %v" , result .Errors )
59+ }
60+ if len (result .Data ) == 0 {
61+ return "" , errors .New ("no data found" )
62+ }
63+ ttml := result .Data [0 ].Attributes .TtmlLocalizations
64+ if ttml == "" {
65+ return "" , errors .New ("no ttml found" )
5066 }
51- ttml := respJson ["data" ][0 ].(map [string ]interface {})["attributes" ].(map [string ]interface {})["ttmlLocalizations" ].(string )
5267 return ttml , nil
5368}
5469
0 commit comments