-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (69 loc) 路 2.14 KB
/
Copy pathmain.go
File metadata and controls
78 lines (69 loc) 路 2.14 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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
)
const tokenEnvVar = "SCDL_TOKEN"
var (
flagToken string
flagOutput string
flagProxy string
)
var rootCmd = &cobra.Command{
Use: "scdl <url>",
Short: "Download a song or playlist from a given SoundCloud URL",
Long: `Download a song or playlist from SoundCloud and save it as .mp3
files with embedded cover art.
For Go+ high-quality downloads and private tracks, supply a SoundCloud OAuth
token via --token or the ` + tokenEnvVar + ` environment variable. Grab it from
your browser's DevTools: open the network tab on soundcloud.com, find any
request to api-v2.soundcloud.com, and copy the value after "OAuth " in the
Authorization request header.`,
Args: cobra.ArbitraryArgs,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
token := flagToken
if token == "" {
token = os.Getenv(tokenEnvVar)
}
sc, err := NewClient(Options{
Token: token,
OutputDir: flagOutput,
ProxyURL: flagProxy,
})
if err != nil {
return err
}
return sc.Download(cmd.Context(), args[0])
},
}
func main() {
// SIGINT (Ctrl-C) / SIGTERM cancel the root context, which all in-flight
// HTTP requests inherit via NewRequestWithContext. The errgroup wrapping
// HLS segment fetches stops promptly, and the binary exits with 130 (the
// conventional SIGINT exit code).
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
rootCmd.Flags().StringVar(&flagToken, "token", "", "SoundCloud OAuth token (overrides $"+tokenEnvVar+")")
rootCmd.Flags().StringVarP(&flagOutput, "output", "o", "", "output directory (default: current directory)")
rootCmd.Flags().StringVar(&flagProxy, "proxy", "", "proxy URL, e.g. http://user:pass@host:port (defaults to $HTTPS_PROXY/$HTTP_PROXY)")
err := rootCmd.ExecuteContext(ctx)
if err == nil {
return
}
if errors.Is(err, context.Canceled) {
fmt.Fprintln(os.Stderr, "scdl: interrupted")
os.Exit(130)
}
fmt.Fprintln(os.Stderr, "scdl:", err)
os.Exit(1)
}