diff --git a/config.yaml b/config.yaml index a2253e1..8fc2985 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,5 @@ -media-user-token: "your-media-user-token" +media-user-token: "your-media-user-token" #If you need to obtain the lyrics, need to change it +authorization-token: "your-authorization-token" #You don't need to change it; it can automatically obtain token embed-lrc: true save-lrc-file: false lrc-format: "lyrics" #lyrics syllable-lyrics @@ -10,7 +11,7 @@ cover-size: 5000x5000 cover-format: jpg #jpg png or original alac-save-folder: AM-DL downloads atmos-save-folder: AM-DL-Atmos downloads -check: "" +check: "" # API or .txt force-api: false decrypt-m3u8-port: "127.0.0.1:10020" get-m3u8-port: "127.0.0.1:20020" diff --git a/go.mod b/go.mod index 954c241..e8c0bba 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,11 @@ go 1.17 require ( github.com/abema/go-mp4 v0.7.2 github.com/grafov/m3u8 v0.11.1 + github.com/spf13/pflag v1.0.5 ) require ( - github.com/beevik/etree v1.3.0 // indirect + github.com/beevik/etree v1.3.0 github.com/google/uuid v1.1.2 // indirect gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index 2d15e06..cc5584c 100644 --- a/go.sum +++ b/go.sum @@ -10,14 +10,18 @@ github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grafov/m3u8 v0.11.1 h1:igZ7EBIB2IAsPPazKwRKdbhxcoBKO3lO1UY57PZDeNA= github.com/grafov/m3u8 v0.11.1/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e h1:s2RNOM/IGdY0Y6qfTeUKhDawdHDpK9RGBdx80qN4Ttw= github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e/go.mod h1:nBdnFKj15wFbf94Rwfq4m30eAcyY9V/IyKAGQFtqkW0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -26,6 +30,7 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg= gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98= diff --git a/main.go b/main.go index a92d912..5538af4 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,8 @@ import ( "strings" "time" + "github.com/spf13/pflag" + "gopkg.in/yaml.v2" "github.com/abema/go-mp4" @@ -37,12 +39,17 @@ const ( var ( forbiddenNames = regexp.MustCompile(`[/\\<>:"|?*]`) ) -var dl_atmos = false -var dl_select = false -var artist_select = false +var ( + dl_atmos bool + dl_select bool + artist_select bool + alac_max *int + atmos_max *int +) type Config struct { MediaUserToken string `yaml:"media-user-token"` + AuthorizationToken string `yaml:"authorization-token"` SaveLrcFile bool `yaml:"save-lrc-file"` LrcFormat string `yaml:"lrc-format"` SaveAnimatedArtwork bool `yaml:"save-animated-artwork"` @@ -1770,32 +1777,53 @@ func main() { } token, err := getToken() if err != nil { - fmt.Println("Failed to get token.") - return - } - var dlArgs []string - for _, arg := range os.Args { - if strings.Contains(arg, "--atmos") { - dl_atmos = true - } else if strings.Contains(arg, "--select") { - dl_select = true - } else if strings.Contains(arg, "--all-album") { - artist_select = true + if config.AuthorizationToken != "" && config.AuthorizationToken != "your-authorization-token" { + token = strings.Replace(config.AuthorizationToken, "Bearer ", "", -1) } else { - dlArgs = append(dlArgs, arg) + fmt.Println("Failed to get token.") + return } } - os.Args = dlArgs - if strings.Contains(os.Args[1], "/artist/") { - newArgs, err := checkArtist(os.Args[1], token) + // Define command-line flags + pflag.BoolVar(&dl_atmos, "atmos", false, "Enable atmos download mode") + pflag.BoolVar(&dl_select, "select", false, "Enable selective download") + pflag.BoolVar(&artist_select, "all-album", false, "Download all artist albums") + alac_max = pflag.Int("alac-max", -1, "Specify the max quality for download alac") + atmos_max = pflag.Int("atmos-max", -1, "Specify the max quality for download atmos") + + // Custom usage message for help + pflag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s [options] url1 url2 ...\n", "[main | main.exe | go run main.go]") + fmt.Println("Options:") + pflag.PrintDefaults() + } + + // Parse the flag arguments + pflag.Parse() + + if *alac_max != -1 { + config.AlacMax = *alac_max + } + if *atmos_max != -1 { + config.AtmosMax = *atmos_max + } + args := pflag.Args() + if len(args) == 0 { + fmt.Println("No URLs provided. Please provide at least one URL.") + pflag.Usage() + return + } + os.Args = args + if strings.Contains(os.Args[0], "/artist/") { + newArgs, err := checkArtist(os.Args[0], token) if err != nil { fmt.Println("Failed to get artist.") return } - os.Args = append([]string{os.Args[0]}, newArgs...) + os.Args = newArgs } - albumTotal := len(os.Args[1:]) - for albumNum, url := range os.Args[1:] { + albumTotal := len(os.Args) + for albumNum, url := range os.Args { fmt.Printf("Album %d of %d:\n", albumNum+1, albumTotal) var storefront, albumId string if strings.Contains(url, ".txt") {