mirror of
https://github.com/zhaarey/apple-music-downloader.git
synced 2025-10-23 15:11:05 +00:00
50 lines
941 B
Go
50 lines
941 B
Go
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
|
|
}
|