mirror of
https://github.com/zhaarey/apple-music-downloader.git
synced 2025-10-23 15:11:05 +00:00
test: 重构
This commit is contained in:
49
utils/ampapi/token.go
Normal file
49
utils/ampapi/token.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package ampapi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func GetToken() (string, error) {
|
||||
req, err := http.NewRequest("GET", "https://beta.music.apple.com", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
regex := regexp.MustCompile(`/assets/index-legacy-[^/]+\.js`)
|
||||
indexJsUri := regex.FindString(string(body))
|
||||
|
||||
req, err = http.NewRequest("GET", "https://beta.music.apple.com"+indexJsUri, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
regex = regexp.MustCompile(`eyJh([^"]*)`)
|
||||
token := regex.FindString(string(body))
|
||||
|
||||
return token, nil
|
||||
}
|
||||
Reference in New Issue
Block a user