-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.sh
executable file
·55 lines (43 loc) · 1.3 KB
/
main.sh
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
#!/bin/bash
# Spotify legacy token
LEGACY_TOKEN="YOUR_LEGACY_TOKEN"
trap onexit INT
# Helper for https://api.slack.com/methods/users.profile.set Slack API Method
function setProfile() {
PAYLOAD=$1
echo "Calling https://api.slack.com/methods/users.profile.set with $PAYLOAD"
curl -X POST -H "Authorization: Bearer $LEGACY_TOKEN" -H "Content-Type: application/json; charset=UTF-8" -s -d "$PAYLOAD" "https://slack.com/api/users.profile.set" > /dev/null
}
function reset() {
echo 'Resetting status'
PAYLOAD="{ \"profile\": { \"status_text\" : \"\", \"status_emoji\" : \"\" } }"n
setProfile "$PAYLOAD"
}
function onexit() {
echo 'Exiting'
reset
exit
}
function getSong() {
SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
echo $SONG
}
pgrep Spotify > /dev/null
if [ $? -ne 0 ]; then
echo 'Spotify is not running, exiting'
reset
exit 0
fi
state=$(osascript -e 'tell application "Spotify" to player state')
echo "$(date) Spotify state: $state"
if [[ "$state" != "playing" ]]; then
echo 'Spotify is not playing any song, exiting'
reset
else
SONG=$(getSong)
echo "Detected Spotify track: $SONG"
PAYLOAD="{ \"profile\": { \"status_text\": \"$SONG\", \"status_emoji\": \":headphones:\" } }"
setProfile "$PAYLOAD"
fi
echo "Done"
exit 0