-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlyrics.go
More file actions
87 lines (82 loc) · 2.59 KB
/
lyrics.go
File metadata and controls
87 lines (82 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
func GetHttpClient() *http.Client {
if PROXY == "" {
return http.DefaultClient
}
proxyUrl, err := url.Parse(PROXY)
if err != nil {
panic("Invalid proxy URL: " + PROXY)
}
transport := &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
return &http.Client{Transport: transport}
}
type LyricResponse struct {
Errors []interface{} `json:"errors"`
Data []struct {
Attributes struct {
TtmlLocalizations string `json:"ttmlLocalizations"`
} `json:"attributes"`
} `json:"data"`
}
func GetLyrics(adamID string, region string, language string, token string, musicToken string) (string, error) {
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)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "Music/5.7 Android/10 model/Pixel6GR1YH build/1234 (dt:66)")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("media-user-token", musicToken)
req.Header.Set("Origin", "https://music.apple.com")
resp, err := GetHttpClient().Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("failed to get lyrics: %d", resp.StatusCode))
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result LyricResponse
if err := json.Unmarshal(respBody, &result); err != nil {
return "", err
}
if len(result.Errors) > 0 {
return "", fmt.Errorf("failed to get lyrics: %v", result.Errors)
}
if len(result.Data) == 0 {
return "", errors.New("no data found")
}
ttml := result.Data[0].Attributes.TtmlLocalizations
if ttml == "" {
return "", errors.New("no ttml found")
}
return ttml, nil
}
func HasLyrics(adamID string, region string, language string, token string, musicToken string) bool {
req, err := http.NewRequest("HEAD", 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)
if err != nil {
return false
}
req.Header.Set("User-Agent", "Music/5.7 Android/10 model/Pixel6GR1YH build/1234 (dt:66)")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("media-user-token", musicToken)
req.Header.Set("Origin", "https://music.apple.com")
resp, err := GetHttpClient().Do(req)
if err != nil {
return false
}
if resp.StatusCode != 200 {
return false
}
return true
}