-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
212 lines (196 loc) · 3.18 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/yanatan16/golang-soundcloud/soundcloud"
)
var api = &soundcloud.Api{
ClientId: "LvWovRaJZlWCHql0bISuum8Bd2KX79mb",
}
var (
log = logger{}
wg = sync.WaitGroup{}
)
var (
pluri = flag.String(
"p",
"",
"The playlist url you want to download.",
)
turi = flag.String(
"t",
"",
"The track url you want to download.",
)
conc = flag.Bool(
"c",
true,
"Enables concurrent downloading when downloading a playlist.",
)
verbose = flag.Bool(
"v",
true,
"Enables verbose logging.",
)
)
func init() {
flag.Parse()
log.verbose = *verbose
}
func main() {
var uri string
if *pluri != "" {
uri = *pluri
}
if *turi != "" {
uri = *turi
}
if uri == "" {
log.Fatal("Please provide a valid url. For usage please use --help flag!")
}
res, err := api.Resolve(uri)
if err != nil {
log.Fatal(err)
}
val := strings.Replace(
filepath.Base(res.Path),
".json",
"",
-1,
)
if *pluri != "" {
PlayListDl(val)
}
if *turi != "" {
TrackDl(val)
}
wg.Wait()
log.Println("Saving all files...")
time.Sleep(30 * time.Second)
}
// PlayListDl downloads a playlist
func PlayListDl(val string) {
plid, err := strconv.Atoi(val)
if err != nil {
log.Fatal(err)
}
pl := api.Playlist(uint64(plid))
p, err := pl.Get(url.Values{})
if err != nil {
log.Fatal(err)
}
for _, v := range p.Tracks {
if *conc {
go downloadTrack(v)
} else {
downloadTrack(v)
}
}
}
// TrackDl downloads a track
func TrackDl(val string) {
tid, err := strconv.Atoi(val)
if err != nil {
log.Fatal(err)
}
tr := api.Track(uint64(tid))
t, err := tr.Get(url.Values{})
if err != nil {
log.Fatal(err)
}
downloadTrack(t)
}
// downloadTrack manages the perfect way to download a track
func downloadTrack(t *soundcloud.Track) {
wg.Add(1)
var n int64
defer func() {
log.Println("[SAVED]", n/1024, "kb", t.Title)
wg.Done()
}()
if t.Downloadable {
log.Println("[DOWNLOADING]", t.Title)
n = saveTrack(t.Title, t.DownloadUrl)
return
}
if t.Streamable {
log.Println("[DOWNLOADING](BYPASS)", t.Title)
n = saveTrack(t.Title, t.StreamUrl)
return
}
log.Println("[FAIL] Can't download", t.Title)
}
// saveTrack saves the track to a file
func saveTrack(name, uri string) int64 {
out, err := os.Create(
prepareName(name) + ".mp3")
if err != nil {
log.Println(err)
return 0
}
defer out.Close()
resp, err := http.Get(uri + "?client_id=" + api.ClientId)
if err != nil {
log.Println(err)
return 0
}
defer resp.Body.Close()
n, err := io.Copy(out, resp.Body)
if err != nil {
log.Println(err)
return 0
}
return n
}
type logger struct {
verbose bool
}
func (l *logger) Println(p ...interface{}) {
if l.verbose {
fmt.Println(p...)
}
}
func (l *logger) Fatal(p ...interface{}) {
if l.verbose {
fmt.Println(p...)
os.Exit(1)
}
fmt.Println("An error occurred!")
os.Exit(1)
}
func prepareName(name string) string {
v1 := strings.Replace(
name,
" ",
"_",
-1,
)
v2 := strings.Replace(
v1,
"/",
"-",
-1,
)
v3 := strings.Replace(
v2,
`\`,
"-",
-1,
)
v4 := strings.Replace(
v3,
"*",
"-",
-1,
)
return v4
}