-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.iced
executable file
·90 lines (72 loc) · 2.07 KB
/
index.iced
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
#!./node_modules/.bin/iced
sonos = require('sonos')
request = require("request")
randomColor = require("randomcolor")
fs = require('fs')
request = require('request')
imgur = require('imgur')
download = (uri, filename, callback) ->
f = (err, res, body) ->
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback)
request.head(uri, f)
config = require('./config.json')
lastArtist = null
lastTitle = null
isBroke = false
mySonos = null
if config.sonosIpAddress == "detect"
await(sonos.search(defer(mySonos)))
else
mySonos = new sonos.Sonos(config.sonosIpAddress)
postToChat = (message) ->
await(request.post({
uri: config.slackIncomingWebHookUrl
body: JSON.stringify(message)
}, defer(err)))
if err?
console.error("Error posting to chat: #{err}")
checkSong = ->
await(mySonos.currentTrack(defer(err, track)))
if err? or not track?
if not isBroke
isBroke = true
console.error("Error connecting to Sonos: #{err}")
postToChat(text: "Error: #{err}")
else if track.artist != lastArtist or track.title != lastTitle
postSong(track)
postSong = (track) ->
isBroke = false
albumArtURL = track.albumArtURL
lastArtist = track.artist
lastTitle = track.title
oneLiner = "#{track.artist} - #{track.title}"
console.log(oneLiner)
await(download(albumArtURL, "./art.png", defer()))
await(imgur.uploadFile("./art.png").then(defer(imgurData)))
postOptions =
link_names: 1
attachments: [
{
fallback: oneLiner
color: randomColor()
fields: [
{
title: track.artist
value: track.title
short: true
}
]
image_url: imgurData.data.link
}
]
for key in ['username', 'icon_emoji']
if config[key] then postOptions[key] = config[key]
usersToNotify = []
for user, favorites of config.favorites
if track.artist in favorites
usersToNotify.push(user)
if usersToNotify.length > 0
postOptions.text = "hi " + usersToNotify.join(", ")
postToChat(postOptions)
checkSong()
setInterval(checkSong, 5000)