This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
musly.go
88 lines (63 loc) · 2.28 KB
/
musly.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
// Ensures that collections are exists has been initialized
func EnsureCollection(collection *Collection) (err error) {
if _, err = os.Stat(collection.Pathname); err == nil {
return
}
dir := filepath.Dir(collection.Pathname)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0777)
}
ch := make(chan CommandResult)
RunCommand(fmt.Sprintf("musly -n timbre -c \"%s\"", collection.Pathname), ch)
cr := <- ch
if cr.Error != nil {
err = errors.New(fmt.Sprintf("An error occurred when initializing collection: %s.", cr.Stderr))
}
return
}
// Adds track to collection
func AddTrackToCollection(track *Track, collection *Collection) (err error) {
ch := make(chan CommandResult)
RunCommand(fmt.Sprintf("musly -a \"%s\" -c \"%s\"", track.Pathname, collection.Pathname), ch)
cr := <- ch
if cr.Error != nil {
err = errors.New(fmt.Sprintf("An error occurred when adding track to collection: %s.", cr.Stderr))
return
}
if strings.Contains(cr.Stdout, "[FAILED]") {
err = errors.New("An error occurred when adding track to collection: failed.")
return
}
return
}
// Returns similar tracks
func GetSimilarTracks(track *Track, collection *Collection) (similarTracks SimilarTracks, err error) {
ch := make(chan CommandResult)
RunCommand(fmt.Sprintf("musly -p \"%s\" -k 200 -o long -c \"%s\" -j \"%s\"", track.Pathname, collection.Pathname, collection.JukeboxPathname), ch)
cr := <- ch
similarTracks = SimilarTracks{}
scanner := bufio.NewScanner(strings.NewReader(cr.Stdout))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "track-id") == false {
continue
}
pathname := strings.SplitAfter(line, "track-origin: ")[1]
distance, _ := strconv.ParseFloat(strings.Split(strings.SplitAfter(line, "track-distance: ")[1], ", ")[0], 64)
similarity, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", 100 - (distance * 100)), 64)
similarTracks = append(similarTracks, SimilarTrack{NewTrack(pathname), similarity})
}
sort.Sort(similarTracks)
return
}