-
Notifications
You must be signed in to change notification settings - Fork 10
/
spotify.coffee
161 lines (147 loc) · 6.57 KB
/
spotify.coffee
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
##############################
# Control Spotify on OSX #
##############################
sh = require('sh')
spotify = require('spotify')
module.exports = (robot) ->
# show commands
options = {}
options.commands = [
"play <1|2|3|query> - Play/pause, play a song that you searched (1,2,3) or play a track by title",
"pause - Pause the current song",
"toggle - Play/pause the current song",
"stop - Stop current song",
"next - Play the next song from the playlist",
"previous - Play the previous song from the playlist",
"current song - Shows what song I'm currently playing",
"volume <0..9|up|down> - Change volume using a specific number between 0 (mute) and 9 or by up and down",
"mute - Mute/unmute the sound",
"search <track|album|artist> <query> - Search for a track on Spotify and play it"
]
# starting volume in spotify
options.volume = 100
robot.respond /spotify$/i, (msg) ->
msg.send options.commands.join("\n")
# controls
robot.respond /toggle$/i, (msg) ->
msg.send "Okay, toggling play/pause in Spotify"
sh('osascript -e \'tell app "Spotify" to playpause\'')
robot.respond /play$/i, (msg) ->
msg.send "Playing the current song in Spotify"
sh('osascript -e \'tell app "Spotify" to playpause\'')
robot.respond /(pause|stop)$/i, (msg) ->
msg.send "Pausing the current song in Spotify"
sh('osascript -e \'tell app "Spotify" to playpause\'')
robot.respond /(next|play next|play the next song)$/i, (msg) ->
sh('osascript -e \'tell app "Spotify" to next track\'')
song = sh('osascript src/scripts/current_song.scpt')
song.result (obj) ->
msg.send "And now I'm playing "+ obj
robot.respond /(previous|prev|play previous|play the previous song)$/i, (msg) ->
sh('osascript -e \'tell app "Spotify" to previous track\'')
song = sh('osascript src/scripts/current_song.scpt')
song.result (obj) ->
msg.send "Playing this song again: "+ obj
robot.respond /volume ((\d{1,2})|up|down)$/i, (msg) ->
volume = msg.match[1]
switch volume
when "up"
if options.volume < 100
options.volume+=10
msg.send "Louder, louder!"
when "down"
if options.volume > 0
options.volume-=10
msg.send "Ah, I was trying to say it, but nobody could hear me. Oh wait, I don't have a voice"
else
if volume < (options.volume/10)
msg.send "Yes, this is too loud for me"
else
msg.send "Turning up the volume! w00t"
options.volume = Math.round(volume)*10 if volume <= 100
sh('osascript -e \'tell application "Spotify" to set sound volume to '+options.volume+'\'')
robot.respond /mute$/i, (msg) ->
if options.muted
sh('osascript -e \'tell application "Spotify" to set sound volume to '+options.volume+'\'')
msg.send "That was a quiet moment"
else
sh('osascript -e \'tell application "Spotify" to set sound volume to 0\'')
msg.send "Silence"
options.muted = !options.muted
robot.respond /unmute$/i, (msg) ->
if options.muted
sh('osascript -e \'tell application "Spotify" to set sound volume to '+options.volume+'\'')
msg.send "That was a quiet moment"
# show what song I'm currently playing
robot.respond /(current|song|track|current song|current track)$/i, (msg) ->
song = sh('osascript src/scripts/current_song.scpt')
song.result (obj) ->
msg.send "The current song I'm playing is "+ obj
# search through Spotify
robot.respond /search ?(track|song|album|artist)? (.*)$/i, (msg) ->
query = msg.match[2]
if msg.match[1]?
switch msg.match[1]
when "track" or "song" then type = "track"
when "album" then type = "album"
when "artist" then type = "artist"
else
type = "track"
spotify.search
type: type
query: query
(err, data) ->
unless err
switch type
when "track"
if data.tracks.length is 1
song = data.tracks[0]
msg.send "Found it.. use Hubot play 1 for “"+song.name+"” by "+song.artists[0].name
options.result = {first: data.tracks[0].href}
else if data.tracks.length is 2
result = [
"Use Hubot play 1 for “"+data.tracks[0].name+"” by "+data.tracks[0].artists[0].name,
"or use Hubot play 2 for “"+data.tracks[1].name+"” by "+data.tracks[1].artists[0].name
]
msg.send result.join("\n")
options.result = {first: data.tracks[0].href, second: data.tracks[1].href}
else if data.tracks.length > 2
result = [
"Use Hubot play 1 for “"+data.tracks[0].name+"” by "+data.tracks[0].artists[0].name,
"or use Hubot play 2 for “"+data.tracks[1].name+"” by "+data.tracks[1].artists[0].name,
"or play 3 for “"+data.tracks[2].name+"” by "+data.tracks[2].artists[0].name
]
msg.send result.join("\n")
options.result = {first: data.tracks[0].href, second: data.tracks[1].href, third: data.tracks[2].href}
# can't play a song by album or artist at this moment... maybe in the feature
when "album"
album = data.albums[0]
if album
msg.send "Found a album by the name of "+album.name+". Now, to continue this quiz... Can you name a song?"
when "artist"
artist = data.artists[0]
if artist
msg.send "Got it. I found "+artist.name+". Now, to continue this quiz... Can you name a song?"
# play a track or add a searched song to the play queue and play it
robot.respond /play (.*)$/i, (msg) ->
query = msg.match[1]
switch query
when "1"
msg.send "Okay, sure why not"
sh('open '+options.result.first) if options.result? and options.result.first?
when "2"
msg.send "Hah, this is my favorite song"
sh('open '+options.result.second) if options.result? and options.result.second?
when "3"
msg.send "Are you really sure? Okay, I'll play it anyway"
sh('open '+options.result.third) if options.third? and options.result.third?
else
spotify.search
type: "track"
query: msg.match[1]
(err, data) ->
unless err
song = data.tracks[0]
if song
sh('open '+song.href)
msg.send "Found it, playing: “"+song.name+"” by "+song.artists[0].name+" from "+song.album.name