-
Notifications
You must be signed in to change notification settings - Fork 2
/
export.go
236 lines (213 loc) · 5.02 KB
/
export.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
package control
import (
"fmt"
"path/filepath"
"strings"
"github.com/insomnimus/libman/control/store"
"github.com/insomnimus/libman/handler"
"github.com/zmb3/spotify"
)
func defaultImportHandlers() handler.Set {
const (
Help = iota
Create
Replace
Play
)
hand := func(c uint8, name, help, usage string, aliases ...string) handler.Handler {
return handler.Handler{
Cmd: c,
Name: name,
Usage: usage,
Help: help,
About: help,
Aliases: aliases,
Complete: completeNothing,
Run: func(string) error { panic("internal error: .Run() called on a dummy handler") },
}
}
set := handler.Set{
hand(
Create,
"create",
"Create a new playlist, with the contents of the imported one.",
"create [name]",
"save",
),
hand(
Replace,
"replace",
"Replace an existing playlist with the imported one.",
"replace [name]",
"repl",
),
hand(
Play,
"play",
"Play tracks from the imported playlist.",
"play",
"pl",
),
hand(
Help,
"help",
"List available commands.",
"help [command]",
),
}
set.Find(Help).Complete = set.CompleteHelp
set.Find(Help).Run = set.RunHelp
updateCache()
set.Find(Replace).Complete = newWordCompleter(cache.ownedNames()...)
return set
}
func handleExportPlaylist(arg string) error {
p := choosePlaylist(arg)
if p == nil {
return nil
}
filename := readString("Enter the full path where the file will be saved (.json extension will be appended): ")
if filename == "" {
fmt.Println("cancelled")
return nil
}
if !strings.HasSuffix(filename, ".json") {
filename += ".json"
}
if !filepath.IsAbs(filename) {
filename = filepath.Join(DataHome, filename)
}
if err := p.makeFull(); err != nil {
return err
}
err := store.ExportTo(p.FullPlaylist, filename)
if err == nil {
fmt.Printf("Exported %s to %s.\n", p.Name, filename)
}
return err
}
func handleImportPlaylist(arg string) error {
const (
Help = iota
Create
Replace
Play
)
if arg == "" {
rl.SetCompleter(suggestPath)
arg = readString("Enter the path of a previously exported playlist: ")
if arg == "" {
fmt.Println("cancelled")
return nil
}
}
if DataHome != "" && !filepath.IsAbs(arg) {
arg = filepath.Join(DataHome, arg)
}
p, err := store.ImportFrom(arg)
if err != nil {
return err
}
fmt.Printf("Imported %s.\n", p.Name)
if len(p.Tracks) == 0 {
fmt.Printf("%s has no tracks in it, no action can be taken.\n", p.Name)
return nil
}
fmt.Println("Type `help` for a list of available actions.")
set := defaultImportHandlers()
set.Find(Play).Run = func(string) error {
uris := make([]spotify.URI, len(p.Tracks))
for i, t := range p.Tracks {
uris[i] = t.URI
}
err := client.PlayOpt(&spotify.PlayOptions{
URIs: uris,
})
if err == nil {
fmt.Printf("Playing tracks from the imported playlist %s.\n", p.Name)
isPlaying = true
}
return err
}
set.Find(Replace).Run = func(arg string) error {
if arg == "" {
fmt.Println("Choose a playlist to replace.")
}
pl := choosePlaylist(arg)
if pl == nil {
return nil
}
if readBool("Are you sure you want to replace all tracks in %s with those in %s?", pl.Name, p.Name) {
ids := make([]spotify.ID, len(p.Tracks))
for i, t := range p.Tracks {
ids[i] = t.ID
}
err := client.ReplacePlaylistTracks(pl.ID, ids...)
if err == nil {
fmt.Printf("Replaced every track in %s.\n", pl.Name)
pl.Tracks.Tracks = p.PlaylistTracks()
}
return err
} else {
fmt.Println("cancelled")
return nil
}
}
set.Find(Create).Run = func(arg string) error {
if len(p.Tracks) == 0 {
fmt.Println("The imported playlist has no tracks. If you still want to create a playlist, use the `create` command.")
return nil
}
if arg == "" {
arg = readString("Playlist name (%s): ", p.Name)
if arg == "" {
arg = p.Name
}
}
desc := readString("Playlist description: ")
pub := readBool("Should the playlist be public?")
if !readBool("Create new playlist %s and import tracks?", arg) {
fmt.Println("cancelled")
return nil
}
pl, err := client.CreatePlaylistForUser(user.ID, arg, desc, pub)
if err != nil {
return err
}
fmt.Printf("Created new playlist %q.\n", pl.Name)
ids := make([]spotify.ID, len(p.Tracks))
for i, t := range p.Tracks {
ids[i] = t.ID
}
_, err = client.AddTracksToPlaylist(pl.ID, ids...)
if err != nil {
fmt.Println("Failed to add new tracks to the playlist.")
return err
}
pl.Tracks.Tracks = p.PlaylistTracks()
cache.insertFull(0, *pl)
fmt.Printf("Successfully imported all the tracks to %s.\n", pl.Name)
return nil
}
for {
rl.SetCompleter(set.Complete)
reply, cancelled := readPrompt(false, "imported %s$ ", p.Name)
if cancelled {
fmt.Println("cancelled")
return nil
}
if reply == "" {
continue
}
command, arg := splitCmd(reply)
h := set.Match(command)
if h == nil {
fmt.Printf("%s is not a known command or alias.\nRun `help` for a list of available commands.\n", command)
continue
}
if h.Cmd == Help {
continue
}
return h.Run(arg)
}
}