-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
76 lines (61 loc) · 1.57 KB
/
main.go
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
package main
import (
"context"
"fmt"
"github.com/BharatKalluri/spotifydl/src"
"os"
"strings"
"github.com/spf13/cobra"
)
var (
ctx context.Context
)
func main() {
var trackID string
var playlistID string
var albumID string
var spotifyURL string
var rootCmd = &cobra.Command{
Use: "spotifydl",
Short: "spotifydl is a awesome music downloader!",
Long: `spotifydl lets you download albums and playlists and tags them for you.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Help()
}
spotifyURL = args[0]
if len(spotifyURL) == 0 {
fmt.Println("=> Spotify URL required.")
_ = cmd.Help()
return
}
splitURL := strings.Split(spotifyURL, "/")
if len(splitURL) < 2 {
fmt.Println("=> Please enter the url copied from the spotify client.")
os.Exit(1)
}
spotifyID := splitURL[len(splitURL)-1]
if strings.Contains(spotifyID, "?") {
spotifyID = strings.Split(spotifyID, "?")[0]
}
if strings.Contains(spotifyURL, "album") {
albumID = spotifyID
spotifydl.DownloadAlbum(ctx, albumID)
} else if strings.Contains(spotifyURL, "playlist") {
playlistID = spotifyID
spotifydl.DownloadPlaylist(ctx, playlistID)
} else if strings.Contains(spotifyURL, "track") {
trackID = spotifyID
spotifydl.DownloadSong(ctx, trackID)
} else {
fmt.Println("=> Only Spotify Album/Playlist/Track URL's are supported.")
_ = cmd.Help()
}
},
}
rootCmd.SetUsageTemplate(`spotifydl [spotify_url]`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}