-
Notifications
You must be signed in to change notification settings - Fork 0
/
tad.go
98 lines (85 loc) · 1.87 KB
/
tad.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
type Library struct {
Genres map[string]map[string]Song `json:"genres"`
}
type Song struct {
Artists []string `json:"artists"`
Links []string `json:"links"`
}
func main() {
config := flag.String("f", "", "file containing your library")
rootPath := flag.String("p", "", "the path to the folder in which it should downloaded the music")
flag.Parse()
if *rootPath == "" || *config == "" {
fmt.Println("Arguments -f and -p must be provided")
return
}
//Read file
file, err := os.ReadFile(*config)
if err != nil {
panic(err)
}
var library Library
err = json.Unmarshal(file, &library)
if err != nil {
panic(err)
}
//Check directory music
err = createFolderIfNotExist(*rootPath)
if err != nil {
panic(err)
}
for genreName, genre := range library.Genres {
createFolderIfNotExist(*rootPath+ "/" + genreName)
if err != nil {
panic(err)
}
for songName, song := range genre {
artists := strings.Join(song.Artists, ", ")
songName = artists + " - " + songName + ""
path := *rootPath + "/" + genreName + "/" + songName + ".%(ext)s"
// check if song already present
_, err := os.Stat(path)
if err == nil {
fmt.Println("file already present", err)
continue
}
fmt.Println("Downloading", path)
for _, url := range song.Links {
err = exec.Command(
"youtube-dl",
"-x",
"--audio-format",
"m4a",
"--prefer-ffmpeg",
url,
"-o",
path,
).Run()
if err != nil {
continue
}
}
}
}
}
func createFolderIfNotExist(path string) error {
folderInfo, err := os.Stat(path)
if err != nil || !folderInfo.IsDir() {
err = os.Mkdir(path, 0755)
if err != nil {
return err
}
}
return nil
}
// Command to download:
// yoltube-dl -x --audio-format m4a --prefer-ffmpeg <URL> -o "<ARTIST> - <TITLE>.%(ext)s"