/
savefile.go
52 lines (48 loc) · 1.24 KB
/
savefile.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
package main
import (
"fmt"
"io/ioutil"
"os"
)
func saveCompositions(c compositions, arr []int) {
resultChan := make(chan string, len(arr))
out := ""
left := 0
for _, num := range arr {
go saveFile(resultChan, c[num-1])
}
fmt.Printf("Скачивается %d %s:\n", len(arr), enditive(len(arr), "файл", "файла", "файлов"))
for i := 0; i < len(arr); i++ {
out = <-resultChan
left = len(arr) - 1 - i
if left != 0 {
fmt.Printf("%s (осталось: %d)\n", out, left)
} else {
fmt.Println(out)
}
}
fmt.Println("Загрузки завершены!")
}
func saveFile(ch chan string, c composition) {
filename := fmt.Sprintf("%s – %s.mp3", c.artist, c.song)
for exists(filename) {
filename = "_" + filename
}
addr := createAddr(p.scheme, p.host, c.url, "", 0)
fileAddr := getFileAddr(addr)
if fileAddr == "" {
ch <- "Ошибка при скачивании файла: " + filename
return
}
body := getSiteBody(fileAddr)
err := ioutil.WriteFile(filename, body, 0644)
if err != nil {
ch <- "Ошибка при сохранении файла: " + filename
return
}
ch <- "'" + filename + "' сохранен..."
}
func exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}