This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
spotify.go
111 lines (87 loc) 路 3.33 KB
/
spotify.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
package modules
import (
"fmt"
"strings"
"github.com/olivia-ai/olivia/util"
"github.com/olivia-ai/olivia/user"
"github.com/olivia-ai/olivia/language"
"github.com/zmb3/spotify"
spotifyModule "github.com/olivia-ai/olivia/modules/spotify"
)
var (
// SpotifySetterTag is the intent tag for its module
SpotifySetterTag = "spotify setter"
// SpotifyPlayerTag is the intent tag for its module
SpotifyPlayerTag = "spotify player"
)
// SpotifySetterReplacer gets the tokens in the user entry and save them into the client's information.
// See modules/modules.go#Module.Replacer() for more details.
func SpotifySetterReplacer(locale, entry, _, token string) (string, string) {
spotifyTokens := language.SearchTokens(entry)
// Returns if the token is empty
if len(spotifyTokens) != 2 {
return SpotifySetterTag, util.GetMessage(locale, "spotify tokens")
}
// Save the tokens in the user's information
user.ChangeUserInformation(token, func(information user.Information) user.Information {
information.SpotifyID = spotifyTokens[0]
information.SpotifySecret = spotifyTokens[1]
return information
})
return SpotifySetterTag, spotifyModule.LoginSpotify(locale, token)
}
// SpotifyPlayerReplacer plays a specified music on the user's spotify
// See modules/modules.go#Module.Replacer() for more details.
func SpotifyPlayerReplacer(locale, entry, response, token string) (string, string) {
// Return if the tokens are not set
if spotifyModule.CheckTokensPresence(token) {
return SpotifySetterTag, util.GetMessage(locale, "spotify credentials")
}
// Renew the spotify token and get the client
client := spotifyModule.RenewSpotifyToken(token)
// Search for the track
music, artist := language.SearchMusic(locale, entry)
track, err := SearchTrack(client, music+" "+artist)
if err != nil {
return SpotifySetterTag, spotifyModule.LoginSpotify(locale, token)
}
// Search if there is a device name in the entry
device := SearchDevice(client, entry)
options := &spotify.PlayOptions{
URIs: []spotify.URI{track.URI},
}
// Add the device ID if a device is contained
if device != (spotify.PlayerDevice{}) {
options.DeviceID = &device.ID
}
// Play the found track
client.PlayOpt(options)
client.Play()
return SpotifyPlayerTag, fmt.Sprintf(response, track.Name, track.Artists[0].Name)
}
// SearchTrack searches for a given track name and returns the found track and the error
func SearchTrack(client spotify.Client, content string) (spotify.FullTrack, error) {
// Get the results from a track search with the given content
results, err := client.Search(content, spotify.SearchTypeTrack)
if err != nil {
return spotify.FullTrack{}, err
}
// Returns an empty track and empty error if no track was found with this content
if len(results.Tracks.Tracks) == 0 {
return spotify.FullTrack{}, nil
}
// Return the found
return results.Tracks.Tracks[0], nil
}
// SearchDevice searches for a device name inside the given sentence and returns it
func SearchDevice(client spotify.Client, content string) spotify.PlayerDevice {
devices, _ := client.PlayerDevices()
// Iterate through the devices to check if the content contains a device name
for _, device := range devices {
if strings.Contains(content, strings.ToLower(device.Name)) ||
strings.Contains(content, strings.ToLower(device.Type)) {
return device
}
}
return spotify.PlayerDevice{}
}