-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
117 lines (108 loc) · 2.92 KB
/
search.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
package owntone
import (
"fmt"
"strings"
)
type SearchAndPlayAction struct {
name string
c *Client
}
func (a SearchAndPlayAction) Run(query string) (string, error) {
//TODO resultTypeを複数選択できるようにする or 引数から割り出す or actionを別にする
msg := []string{"Search Results..."}
types := []SearchType{track, album, artist}
result, err := a.c.Search(query, types)
if err != nil {
fmt.Println("error in SearchAndDisplayAction")
return "Something wrong...", err
}
var uris []string
if result.Artists.Total > 0 {
msg = append(msg, "# Artists")
for _, item := range result.Artists.Items {
msg = append(msg, fmt.Sprintf(" %v", item.Name))
uris = append(uris, item.Uri)
}
}
if result.Albums.Total > 0 {
msg = append(msg, "# Albums")
for _, item := range result.Albums.Items {
msg = append(msg, fmt.Sprintf(" %v / %v", item.Name, item.Artist))
uris = append(uris, item.Uri)
}
}
if result.Tracks.Total > 0 {
msg = append(msg, "# Tracks")
for _, item := range result.Tracks.Items {
msg = append(msg, fmt.Sprintf(" %v / %v ", item.Title, item.Artist))
uris = append(uris, item.Uri)
}
}
if len(uris) > 0 {
err := a.c.ClearQueue()
if err != nil {
fmt.Println("error in ClearQueue")
return "", err
}
err = a.c.AddItem2Queue(strings.Join(uris, ","))
if err != nil {
fmt.Println("error calling AddItem2Queue")
return "", err
}
err = a.c.Play()
if err != nil {
fmt.Println("error in Play")
return "", err
}
}
if len(msg) > 1 {
msg = append(msg, "And play these items")
} else {
msg = append(msg, "And no play items...")
}
return strings.Join(msg, "\n"), nil
}
func NewSearchAndPlayAction(client *Client) SearchAndPlayAction {
return SearchAndPlayAction{
name: "Search and Play music on Owntone by keyword",
c: client,
}
}
type SearchAndDisplayAction struct {
name string
c *Client
}
func (a SearchAndDisplayAction) Run(query string) (string, error) {
msg := []string{"Search Results..."}
types := []SearchType{track, album, artist}
result, err := a.c.Search(query, types)
if err != nil {
fmt.Println("error in SearchAndDisplayAction")
return "Something wrong...", err
}
if result.Artists.Total > 0 {
msg = append(msg, "# Artists")
for _, item := range result.Artists.Items {
msg = append(msg, fmt.Sprintf(" %v", item.Name))
}
}
if result.Albums.Total > 0 {
msg = append(msg, "# Albums")
for _, item := range result.Albums.Items {
msg = append(msg, fmt.Sprintf(" %v / %v", item.Name, item.Artist))
}
}
if result.Tracks.Total > 0 {
msg = append(msg, "# Tracks")
for _, item := range result.Tracks.Items {
msg = append(msg, fmt.Sprintf(" %v / %v ", item.Title, item.Artist))
}
}
return strings.Join(msg, "\n"), nil
}
func NewSearchAndDisplayAction(client *Client) SearchAndDisplayAction {
return SearchAndDisplayAction{
name: "Search music by keyword on Owntone and display results",
c: client,
}
}