-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (112 loc) · 2.47 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
// "github.com/rapito/go-spotify/spotify"
"spotify-filler/auth"
"spotify-filler/playlist"
"github.com/zmb3/spotify"
)
var supportedExtensions = []string{
".3gp",
".mp3",
".aa",
".aac",
".aiff",
".au",
".flac",
".m4a",
".ogg",
".oga",
".wav",
".aax",
".mp4",
}
var currentPlaylist playlist.Playlist
func main() {
var root string
if len(os.Args) == 1 {
log.Fatal("No path given, Please specify path.")
return
}
if root = os.Args[1]; root == "" {
log.Fatal("No path given, Please specify path.")
return
}
// filepath.Walk
files, err := IOReadDir(root)
if err != nil {
panic(err)
}
if len(files) == 0 {
log.Fatalf("Directory '%s' does not contain files with extensions %s", root, strings.Join(supportedExtensions, ", "))
}
client := auth.GetClient()
currentPlaylist, err = playlist.ChoosePlaylistPrompt(client)
if err != nil {
log.Fatal(err)
}
for _, title := range files {
Search(client, title)
}
}
func Search(client *spotify.Client, title string) {
// search for playlists and albums containing "holiday"
results, err := client.Search(title, spotify.SearchTypeTrack)
if err != nil {
log.Fatal(err)
}
saved := false
// handle album results
if results.Tracks != nil {
for _, item := range results.Tracks.Tracks {
if strings.ContainsAny(title, item.Name) {
if playlist.HasItem(item.ID) {
// fmt.Printf("[Exists] Track '%s' on playlist '%s'\n", title, currentPlaylist.Name)
saved = true
break
}
err := playlist.SaveToPlaylist(client, item.ID)
if err != nil {
log.Fatalf("[Fail] Track '%s' to playlist '%s': %s\n", title, currentPlaylist.Name, err)
}
fmt.Printf("[Saved] Track '%s' to playlist %s\n", item.Name, currentPlaylist.Name)
saved = true
break
}
}
}
if saved == false {
fmt.Printf("[Not Found] Track '%s'\n", title)
}
}
func IOReadDir(root string) ([]string, error) {
var files []string
fileInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
var extension string
for _, file := range fileInfo {
if file.IsDir() {
files, _ = IOReadDir(root + "/" + file.Name())
}
extension = filepath.Ext(file.Name())
if Contains(supportedExtensions, extension) {
files = append(files, strings.TrimSuffix(file.Name(), extension))
}
}
return files, nil
}
func Contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}