-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
157 lines (133 loc) · 3.38 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/schollz/getsong"
log "github.com/schollz/logger"
"github.com/schollz/spotifydownload/getplaylist"
)
type Result struct {
err error
track getplaylist.Track
}
var debug, verbose bool
func init() {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}
func main() {
var playlistURL string
flag.StringVar(&playlistURL, "playlist", "", "The Spotify playlist URL link")
flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.Parse()
if debug {
log.SetLevel("debug")
} else {
log.SetLevel("warn")
}
if playlistURL == "" {
fmt.Print(`
Enter your Spotify Playlist link.
To get the Spotify URL link you can right click on the playlist.
If you are using the Desktop client, then you'll see a button
"Shared > 🔗 Copy Playlist Link", or in the Web browser
you'll see "Copy Playlist Link".
Enter the Spotify Playlist link here:
`)
reader := bufio.NewReader(os.Stdin)
playlistURL, _ = reader.ReadString('\n')
playlistURL = strings.TrimSpace(playlistURL)
}
err := run(playlistURL)
if err != nil {
log.Infof("\nProblem with your bearer key or your playlist ID: %s\n", err.Error())
}
}
func run(playlistURL string) (err error) {
playlistName, tracks, err := getplaylist.GetTracks(playlistURL)
if err != nil {
return
}
if len(tracks) == 0 {
err = fmt.Errorf("found no tracks")
return
}
if _, err = os.Stat(playlistName); os.IsNotExist(err) {
err = os.Mkdir(playlistName, 0755)
if err != nil {
return
}
}
cwd, err := os.Getwd()
if err != nil {
return
}
cwd, err = filepath.Abs(cwd)
if err != nil {
return
}
err = os.Chdir(playlistName)
defer os.Chdir(cwd)
if err != nil {
return
}
bJSON, _ := json.MarshalIndent(tracks, "", " ")
ioutil.WriteFile(time.Now().Format("2006-01-02")+".json", bJSON, 0644)
runtime.GOMAXPROCS(4 * runtime.NumCPU())
workers := runtime.NumCPU()
tracksToDownload := make([]getplaylist.Track, len(tracks))
i := 0
for _, track := range tracks {
fname := fmt.Sprintf("%s - %s.m4a", track.Artist, track.Title)
if _, err = os.Stat(fname); os.IsNotExist(err) {
tracksToDownload[i] = track
i++
}
}
tracksToDownload = tracksToDownload[:i]
if len(tracksToDownload) == len(tracks) {
log.Infof("Downloading %d tracks to '%s' folder\n", len(tracks), playlistName)
} else {
log.Infof("Downloading remaining %d of %d tracks to '%s' folder\n", len(tracksToDownload), len(tracks), playlistName)
}
jobs := make(chan getplaylist.Track, len(tracksToDownload))
results := make(chan Result, len(tracksToDownload))
for w := 0; w < workers; w++ {
go func(jobs <-chan getplaylist.Track, results chan<- Result) {
for j := range jobs {
_, err := getsong.GetSong(j.Title, j.Artist, getsong.Options{
ShowProgress: false, //debug,
Debug: debug,
Filename: fmt.Sprintf("%s - %s", j.Artist, j.Title),
// DoNotDownload: true,
})
if err == nil {
log.Infof("'%s' by '%s' downloaded.", j.Title, j.Artist)
} else {
log.Warnf("'%s' by '%s' not downloaded: %s.", j.Title, j.Artist, err.Error())
}
results <- Result{
track: j,
err: err,
}
}
}(jobs, results)
}
for _, track := range tracksToDownload {
jobs <- track
}
close(jobs)
for i := 0; i < len(tracksToDownload); i++ {
<-results
}
err = nil
return
}