-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (202 loc) · 5.17 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
"github.com/cenkalti/backoff"
)
var (
// Version (set by compiler) is the version of program
Version = "undefined"
// BuildTime (set by compiler) is the program build time in '+%Y-%m-%dT%H:%M:%SZ' format
BuildTime = "undefined"
// GitHash (set by compiler) is the git commit hash of source tree
GitHash = "undefined"
)
func init() {
// otherwise on some files get an error "stream error: stream ID 1; PROTOCOL_ERROR"
_ = os.Setenv("GODEBUG", "http2client=0")
}
func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
var (
dirPath string
skipFilesCount uint64
)
flag.StringVar(&dirPath, "dir", ".", "directory name where to save the files")
flag.Uint64Var(&skipFilesCount, "skip", 0, "number of files to skip")
defaultUsage := flag.Usage
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Version: %s\tBuildTime: %v\tGitHash: %s\n", Version, BuildTime, GitHash)
defaultUsage()
}
flag.Parse()
args := flag.Args()
if len(args) == 0 {
return errors.New("provide an url from where you need to download files")
}
uri := args[0]
tracks, err := getTracks(uri)
if err != nil {
return err
}
fileNameFormat := fmt.Sprintf("%%0%dd-%%s%%s", len(strconv.Itoa(len(tracks))))
idx := uint64(0)
for _, tr := range tracks {
idx++
uri := tr.Src
trackCaption := tr.Caption
if trackCaption == "" {
trackCaption = tr.Title
}
caption, err := htmlText(trackCaption)
if err != nil {
return err
}
uriParsed, err := url.Parse(uri)
if err != nil {
return err
}
ext := filepath.Ext(path.Base(uriParsed.Path))
fileName := limitFileName(fmt.Sprintf(fileNameFormat, idx, caption, ext), 255)
filePath := path.Join(dirPath, fileName)
if idx > skipFilesCount {
fmt.Printf("Downloading %s to %s...\n", uri, filePath)
if err := downloadFile(filePath, uri); err != nil {
return err
}
} else {
fmt.Printf("Skipping %s to %s...\n", uri, filePath)
}
}
fmt.Println("Done.")
return nil
}
func limitFileName(fileName string, limit int) string {
if utf8.RuneCountInString(fileName) <= limit {
return fileName
}
runes := []rune(fileName)
idx := limit / 2
runes[idx] = rune('…')
copy(runes[idx+1:], runes[len(runes)+idx+1-limit:])
return string(runes[:limit])
}
func getTracks(uri string) ([]track, error) {
doc, err := newDocumentFromURL(uri)
if err != nil {
return nil, err
}
sel := doc.Find("script.wp-playlist-script")
if len(sel.Nodes) == 0 {
return nil, errors.New("playlist is not found on the page")
}
var sc wpPlaylistScript
if err := json.Unmarshal([]byte(sel.Text()), &sc); err != nil {
return nil, fmt.Errorf("JSON parsing error: %v", err)
}
return sc.Tracks, nil
}
type track struct {
Src string `json:"src"`
Title string `json:"title"`
Caption string `json:"caption"`
}
type wpPlaylistScript struct {
Type string `json:"type"`
Tracklist bool `json:"tracklist"`
Tracknumbers bool `json:"tracknumbers"`
Images bool `json:"images"`
Artists bool `json:"artists"`
Tracks []track `json:"tracks"`
}
func newDocumentFromURL(url string) (*goquery.Document, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http.Get: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("goquery.NewDocumentFromReader: %v", err)
}
return doc, nil
}
var errHTTPStatusServiceUnavailable = errors.New("503 Service Temporarily Unavailable")
func downloadFile(filepath string, url string) error {
const permanentOn = 10
i := 0
return backoff.Retry(func() error {
i++
if i > permanentOn {
return backoff.Permanent(fmt.Errorf("failed to download file in %d tries", permanentOn))
}
err := downloadFileAux(filepath, url)
if err == nil || err == errHTTPStatusServiceUnavailable {
return err
}
return backoff.Permanent(err)
}, backoff.NewExponentialBackOff())
}
func downloadFileAux(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer func() {
_ = out.Close()
}()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
switch resp.StatusCode {
case http.StatusOK:
case http.StatusServiceUnavailable:
return errHTTPStatusServiceUnavailable
default:
return fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func htmlText(html string) (string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return "", fmt.Errorf("htmlText: %v", err)
}
return doc.Text(), nil
}