-
Notifications
You must be signed in to change notification settings - Fork 15
/
playlist.go
830 lines (641 loc) · 18 KB
/
playlist.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//go:build !darwin
// +build !darwin
// Copyright (C) 2020 Raziman
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
spin "github.com/tj/go-spin"
"github.com/tramhao/id3v2"
"github.com/ztrue/tracerr"
"github.com/issadarkthing/gomu/lyric"
"github.com/issadarkthing/gomu/player"
)
// Playlist struct represents playlist panel
// that shows the tree of the music directory
type Playlist struct {
*tview.TreeView
prevNode *tview.TreeNode
defaultTitle string
// number of downloads
download int
done chan struct{}
yankFile *player.AudioFile
}
func (p *Playlist) help() []string {
return []string{
"j down",
"k up",
"h close node",
"a create a playlist",
"l add song to queue",
"L add playlist to queue",
"d delete file from filesystem",
"D delete playlist from filesystem",
"Y download audio from url",
"r refresh",
"R rename",
"y/p yank/paste file",
"/ find in playlist",
"s search audio from youtube",
"t edit mp3 tags",
"1/2 find lyric if available",
}
}
// newPlaylist returns new instance of playlist and runs populate function
// on root music directory.
func newPlaylist(args Args) *Playlist {
anko := gomu.anko
m := anko.GetString("General.music_dir")
rootDir, err := filepath.Abs(expandTilde(m))
if err != nil {
err = tracerr.Errorf("unable to find music directory: %e", err)
die(err)
}
// if not default value was given
if *args.music != "~/music" {
rootDir = expandFilePath(*args.music)
}
var rootTextView string
useEmoji := anko.GetBool("General.use_emoji")
if useEmoji {
emojiPlaylist := anko.GetString("Emoji.playlist")
rootTextView = fmt.Sprintf("%s %s", emojiPlaylist, path.Base(rootDir))
} else {
rootTextView = path.Base(rootDir)
}
root := tview.NewTreeNode(rootTextView).
SetColor(gomu.colors.playlistDir)
tree := tview.NewTreeView().SetRoot(root)
tree.SetBackgroundColor(gomu.colors.background)
playlist := &Playlist{
TreeView: tree,
defaultTitle: "─ Playlist ──┤ 0 downloads ├",
done: make(chan struct{}),
}
rootAudioFile := new(player.AudioFile)
rootAudioFile.SetName(path.Base(rootDir))
rootAudioFile.SetNode(root)
rootAudioFile.SetPath(rootDir)
root.SetReference(rootAudioFile)
root.SetColor(gomu.colors.playlistDir)
playlist.
SetTitle(playlist.defaultTitle).
SetBorder(true).
SetTitleAlign(tview.AlignLeft).
SetBorderPadding(0, 0, 1, 1)
populate(root, rootDir, gomu.anko.GetBool("General.sort_by_mtime"))
var firstChild *tview.TreeNode
if len(root.GetChildren()) == 0 {
firstChild = root
} else {
firstChild = root.GetChildren()[0]
}
playlist.setHighlight(firstChild)
playlist.SetChangedFunc(func(node *tview.TreeNode) {
playlist.setHighlight(node)
})
playlist.SetSelectedFunc(func(node *tview.TreeNode) {
node.SetExpanded(!node.IsExpanded())
})
cmds := map[rune]string{
'a': "create_playlist",
'D': "delete_playlist",
'd': "delete_file",
'Y': "download_audio",
's': "youtube_search",
'l': "add_queue",
'L': "bulk_add",
'h': "close_node",
'r': "refresh",
'R': "rename",
'y': "yank",
'p': "paste",
'/': "playlist_search",
't': "edit_tags",
'1': "fetch_lyric",
'2': "fetch_lyric_cn2",
}
for key, cmdName := range cmds {
src := fmt.Sprintf(`Keybinds.def_p("%c", %s)`, key, cmdName)
anko.Execute(src)
}
playlist.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if gomu.anko.KeybindExists("playlist", e) {
err := gomu.anko.ExecKeybind("playlist", e)
if err != nil {
errorPopup(err)
}
return nil
}
// disable default key handler for space
if e.Rune() == ' ' {
return nil
}
return e
})
return playlist
}
// Returns the current file highlighted in the playlist
func (p Playlist) getCurrentFile() *player.AudioFile {
node := p.GetCurrentNode()
if node == nil {
return nil
}
return node.GetReference().(*player.AudioFile)
}
// Deletes song from filesystem
func (p *Playlist) deleteSong(audioFile *player.AudioFile) {
confirmationPopup(
"Are you sure to delete this audio file?", func(_ int, buttonName string) {
if buttonName == "no" || buttonName == "" {
return
}
// hehe we need to move focus to next node before delete it
p.InputHandler()(tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone), nil)
err := os.Remove(audioFile.Path())
if err != nil {
errorPopup(err)
return
}
defaultTimedPopup(" Success ",
audioFile.Name()+"\nhas been deleted successfully")
go gomu.app.QueueUpdateDraw(func() {
p.refresh()
// Here we remove the song from queue
gomu.queue.updateQueuePath()
gomu.queue.updateCurrentSongDelete(audioFile)
})
})
}
// Deletes playlist/dir from filesystem
func (p *Playlist) deletePlaylist(audioFile *player.AudioFile) (err error) {
// here we close the node and then move to next folder before delete
p.InputHandler()(tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone), nil)
p.InputHandler()(tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone), nil)
err = os.RemoveAll(audioFile.Path())
if err != nil {
return tracerr.Wrap(err)
}
defaultTimedPopup(
" Success ",
audioFile.Name()+"\nhas been deleted successfully")
go gomu.app.QueueUpdateDraw(func() {
p.refresh()
// Here we remove the song from queue
gomu.queue.updateQueuePath()
gomu.queue.updateCurrentSongDelete(audioFile)
})
return nil
}
// Bulk add a playlist to queue
func (p *Playlist) addAllToQueue(root *tview.TreeNode) {
var childrens []*tview.TreeNode
childrens = root.GetChildren()
// gets the parent if the highlighted item is a file
if root.GetReference().(*player.AudioFile).IsAudioFile() {
childrens = root.GetReference().(*player.AudioFile).ParentNode().GetChildren()
}
for _, v := range childrens {
currNode := v.GetReference().(*player.AudioFile)
if currNode.IsAudioFile() {
currSong := gomu.player.GetCurrentSong()
if currSong == nil || (currNode.Name() != currSong.Name()) {
gomu.queue.enqueue(currNode)
}
}
}
}
// Refreshes the playlist and read the whole root music dir
func (p *Playlist) refresh() {
root := gomu.playlist.GetRoot()
prevNode := gomu.playlist.GetCurrentNode()
prevFilepath := prevNode.GetReference().(*player.AudioFile).Path()
root.ClearChildren()
node := root.GetReference().(*player.AudioFile)
populate(root, node.Path(), gomu.anko.GetBool("General.sort_by_mtime"))
root.Walk(func(node, _ *tview.TreeNode) bool {
// to preserve previously highlighted node
if node.GetReference().(*player.AudioFile).Path() == prevFilepath {
p.setHighlight(node)
return false
}
return true
})
}
// Adds child while setting reference to audio file
func (p *Playlist) addSongToPlaylist(
audioPath string, selPlaylist *tview.TreeNode,
) error {
f, err := os.Open(audioPath)
if err != nil {
return tracerr.Wrap(err)
}
defer f.Close()
songName := getName(audioPath)
node := tview.NewTreeNode(songName)
// populateAudioLength(selPlaylist)
audioLength, err := getTagLength(audioPath)
if err != nil {
return tracerr.Wrap(err)
}
audioFile := new(player.AudioFile)
audioFile.SetName(songName)
audioFile.SetPath(audioPath)
audioFile.SetIsAudioFile(true)
audioFile.SetLen(audioLength)
audioFile.SetNode(node)
audioFile.SetParentNode(selPlaylist)
displayText := setDisplayText(audioFile)
node.SetReference(audioFile)
node.SetText(displayText)
selPlaylist.AddChild(node)
return nil
}
// Gets all audio files walks from music root directory
func (p *Playlist) getAudioFiles() []*player.AudioFile {
root := p.GetRoot()
audioFiles := []*player.AudioFile{}
root.Walk(func(node, _ *tview.TreeNode) bool {
audioFile := node.GetReference().(*player.AudioFile)
audioFiles = append(audioFiles, audioFile)
return true
})
return audioFiles
}
// Creates a directory under selected node, returns error if playlist exists
func (p *Playlist) createPlaylist(name string) error {
selectedNode := p.GetCurrentNode()
parentNode := selectedNode.GetReference().(*player.AudioFile).ParentNode()
// if the current node is the root
// sets the parent to itself
if parentNode == nil {
parentNode = selectedNode
}
audioFile := parentNode.GetReference().(*player.AudioFile)
err := os.Mkdir(path.Join(audioFile.Path(), name), 0744)
if err != nil {
return tracerr.Wrap(err)
}
p.refresh()
return nil
}
// This is used to replace default behaviour of SetCurrentNode which
// adds color highlight attributes
func (p *Playlist) setHighlight(currNode *tview.TreeNode) {
if p.prevNode != nil {
if p.prevNode.GetReference().(*player.AudioFile).IsAudioFile() {
p.prevNode.SetColor(gomu.colors.foreground)
} else {
p.prevNode.SetColor(gomu.colors.playlistDir)
}
}
currNode.SetColor(gomu.colors.playlistHi)
p.SetCurrentNode(currNode)
p.prevNode = currNode
}
// Traverses the playlist and finds the AudioFile struct
// audioName must be hashed with sha1 first
func (p *Playlist) findAudioFile(audioName string) (*player.AudioFile, error) {
root := p.GetRoot()
var selNode *player.AudioFile
root.Walk(func(node, _ *tview.TreeNode) bool {
audioFile := node.GetReference().(*player.AudioFile)
hashed := sha1Hex(getName(audioFile.Name()))
if hashed == audioName {
selNode = audioFile
return false
}
return true
})
if selNode == nil {
return nil, tracerr.New("no matching audio name")
}
return selNode, nil
}
func (p *Playlist) rename(newName string) error {
currentNode := p.GetCurrentNode()
audio := currentNode.GetReference().(*player.AudioFile)
pathToFile, _ := filepath.Split(audio.Path())
var newPath string
if audio.IsAudioFile() {
newPath = pathToFile + newName + ".mp3"
} else {
newPath = pathToFile + newName
}
err := os.Rename(audio.Path(), newPath)
if err != nil {
return tracerr.Wrap(err)
}
return nil
}
// updateTitle creates a spinning motion on the title
// of the playlist panel when downloading.
func (p *Playlist) updateTitle() {
if p.download == 0 {
p.SetTitle(p.defaultTitle)
return
}
// only one call can be made in one time
if p.download > 1 {
return
}
s := spin.New()
Download:
for {
select {
case <-p.done:
p.download--
if p.download == 0 {
p.SetTitle(p.defaultTitle)
break Download
}
case <-time.After(time.Millisecond * 100):
r, g, b := gomu.colors.accent.RGB()
hexColor := padHex(r, g, b)
title := fmt.Sprintf("─ Playlist ──┤ %d downloads [green]%s[#%s] ├",
p.download, s.Next(), hexColor)
p.SetTitle(title)
gomu.app.Draw()
}
}
}
// Download audio from youtube audio and adds the song to the selected playlist
func ytdl(url string, selPlaylist *tview.TreeNode) error {
// lookup if youtube-dl exists
_, err := exec.LookPath("youtube-dl")
if err != nil {
defaultTimedPopup(" Error ", "youtube-dl is not in your $PATH")
return tracerr.Wrap(err)
}
selAudioFile := selPlaylist.GetReference().(*player.AudioFile)
dir := selAudioFile.Path()
defaultTimedPopup(" Ytdl ", "Downloading")
// specify the output path for ytdl
outputDir := fmt.Sprintf(
"%s/%%(title)s.%%(ext)s",
dir)
metaData := fmt.Sprintf("%%(artist)s - %%(title)s")
args := []string{
"--extract-audio",
"--audio-format",
"mp3",
"--output",
outputDir,
"--add-metadata",
"--embed-thumbnail",
"--metadata-from-title",
metaData,
"--write-sub",
"--all-subs",
"--convert-subs",
"lrc",
url,
}
cmd := exec.Command("youtube-dl", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
gomu.playlist.download++
go gomu.playlist.updateTitle()
// blocking
err = cmd.Run()
gomu.playlist.done <- struct{}{}
if err != nil {
return tracerr.Wrap(err)
}
playlistPath := dir
audioPath := extractFilePath(stdout.Bytes(), playlistPath)
historyPath := gomu.anko.GetString("General.history_path")
err = appendFile(expandTilde(historyPath), url+"\n")
if err != nil {
return tracerr.Wrap(err)
}
err = gomu.playlist.addSongToPlaylist(audioPath, selPlaylist)
if err != nil {
return tracerr.Wrap(err)
}
// Embed lyric to mp3 as uslt
var tag *id3v2.Tag
tag, err = id3v2.Open(audioPath, id3v2.Options{Parse: true})
if err != nil {
return tracerr.Wrap(err)
}
defer tag.Close()
pathToFile, _ := filepath.Split(audioPath)
files, err := ioutil.ReadDir(pathToFile)
if err != nil {
logError(err)
}
var lyricWritten int = 0
for _, file := range files {
fileName := file.Name()
fileExt := filepath.Ext(fileName)
lyricFileName := filepath.Join(pathToFile, fileName)
if fileExt == ".lrc" {
// Embed all lyrics and use langExt as content descriptor of uslt
fileNameWithoutExt := strings.TrimSuffix(fileName, fileExt)
langExt := strings.TrimPrefix(filepath.Ext(fileNameWithoutExt), ".")
// Read entire file content, giving us little control but
// making it very simple. No need to close the file.
byteContent, err := ioutil.ReadFile(lyricFileName)
if err != nil {
return tracerr.Wrap(err)
}
lyricContent := string(byteContent)
var lyric lyric.Lyric
err = lyric.NewFromLRC(lyricContent)
if err != nil {
return tracerr.Wrap(err)
}
lyric.LangExt = langExt
err = embedLyric(audioPath, &lyric, false)
if err != nil {
return tracerr.Wrap(err)
}
err = os.Remove(lyricFileName)
if err != nil {
return tracerr.Wrap(err)
}
lyricWritten++
}
}
downloadFinishedMessage := fmt.Sprintf("Finished downloading\n%s\n%v lyrics embeded", getName(audioPath), lyricWritten)
defaultTimedPopup(" Ytdl ", downloadFinishedMessage)
gomu.app.Draw()
return nil
}
// Add songs and their directories in Playlist panel.
func populate(root *tview.TreeNode, rootPath string, sortMtime bool) error {
files, err := ioutil.ReadDir(rootPath)
if err != nil {
return tracerr.Wrap(err)
}
if sortMtime {
sort.Slice(files, func(i, j int) bool {
stat1 := files[i].Sys().(*syscall.Stat_t)
stat2 := files[j].Sys().(*syscall.Stat_t)
time1 := time.Unix(stat1.Mtim.Unix())
time2 := time.Unix(stat2.Mtim.Unix())
return time1.After(time2)
})
}
for _, file := range files {
path, err := filepath.EvalSymlinks(filepath.Join(rootPath, file.Name()))
if err != nil {
continue
}
songName := getName(file.Name())
child := tview.NewTreeNode(songName)
if file.Mode().IsRegular() {
f, err := os.Open(path)
if err != nil {
continue
}
defer f.Close()
filetype, err := getFileContentType(f)
if err != nil {
continue
}
// skip if not mp3 file
if filetype != "mpeg" {
continue
}
audioFile := new(player.AudioFile)
audioFile.SetName(songName)
audioFile.SetPath(path)
audioFile.SetIsAudioFile(true)
audioFile.SetNode(child)
audioFile.SetParentNode(root)
audioLength, err := getTagLength(audioFile.Path())
if err != nil {
logError(err)
}
audioFile.SetLen(audioLength)
displayText := setDisplayText(audioFile)
child.SetReference(audioFile)
child.SetText(displayText)
root.AddChild(child)
}
if file.IsDir() || file.Mode()&os.ModeSymlink != 0 {
audioFile := new(player.AudioFile)
audioFile.SetName(songName)
audioFile.SetPath(path)
audioFile.SetIsAudioFile(false)
audioFile.SetNode(child)
audioFile.SetParentNode(root)
displayText := setDisplayText(audioFile)
child.SetReference(audioFile)
child.SetColor(gomu.colors.playlistDir)
child.SetText(displayText)
root.AddChild(child)
populate(child, path, sortMtime)
}
}
return nil
}
func (p *Playlist) yank() error {
p.yankFile = p.getCurrentFile()
if p.yankFile == nil {
return errors.New("no file has been yanked")
}
if p.yankFile.Node() == p.GetRoot() {
return errors.New("please don't yank the root directory")
}
defaultTimedPopup(" Success ", p.yankFile.Name()+"\n has been yanked successfully.")
return nil
}
func (p *Playlist) paste() error {
if p.yankFile == nil {
return errors.New("no file has been yanked")
}
oldAudio := p.yankFile
oldPathDir, oldPathFileName := filepath.Split(p.yankFile.Path())
pasteFile := p.getCurrentFile()
var newPathDir string
if pasteFile.IsAudioFile() {
newPathDir, _ = filepath.Split(pasteFile.Path())
} else {
newPathDir = pasteFile.Path()
}
if oldPathDir == newPathDir {
return nil
}
newPathFull := filepath.Join(newPathDir, oldPathFileName)
err := os.Rename(p.yankFile.Path(), newPathFull)
if err != nil {
return tracerr.Wrap(err)
}
defaultTimedPopup(" Success ", p.yankFile.Name()+"\n has been pasted to\n"+newPathDir)
// keep queue references updated
newAudio := oldAudio
newAudio.SetPath(newPathFull)
p.refresh()
gomu.queue.updateQueuePath()
if p.yankFile.IsAudioFile() {
err = gomu.queue.updateCurrentSongName(oldAudio, newAudio)
if err != nil {
return tracerr.Wrap(err)
}
} else {
err = gomu.queue.updateCurrentSongPath(oldAudio, newAudio)
if err != nil {
return tracerr.Wrap(err)
}
}
p.yankFile = nil
return nil
}
func setDisplayText(audioFile *player.AudioFile) string {
useEmoji := gomu.anko.GetBool("General.use_emoji")
if !useEmoji {
return audioFile.Name()
}
if audioFile.IsAudioFile() {
emojiFile := gomu.anko.GetString("Emoji.file")
return fmt.Sprintf(" %s %s", emojiFile, audioFile.Name())
}
emojiDir := gomu.anko.GetString("Emoji.playlist")
return fmt.Sprintf(" %s %s", emojiDir, audioFile.Name())
}
// refreshByNode is called after rename of file or folder, to refresh queue info
func (p *Playlist) refreshAfterRename(node *player.AudioFile, newName string) error {
root := p.GetRoot()
root.Walk(func(node, _ *tview.TreeNode) bool {
if strings.Contains(node.GetText(), newName) {
p.setHighlight(node)
}
return true
})
// update queue
newNode := p.getCurrentFile()
if node.IsAudioFile() {
err := gomu.queue.renameItem(node, newNode)
if err != nil {
return tracerr.Wrap(err)
}
err = gomu.queue.updateCurrentSongName(node, newNode)
if err != nil {
return tracerr.Wrap(err)
}
} else {
gomu.queue.updateQueuePath()
err := gomu.queue.updateCurrentSongPath(node, newNode)
if err != nil {
return tracerr.Wrap(err)
}
}
return nil
}