Skip to content

Instantly share code, notes, and snippets.

@olebedev
Forked from jgamblin/slackspotify.sh
Last active April 19, 2020 22:38
Show Gist options
  • Save olebedev/298b456897d04ec85b683b88f11ff820 to your computer and use it in GitHub Desktop.
Save olebedev/298b456897d04ec85b683b88f11ff820 to your computer and use it in GitHub Desktop.

Revisions

  1. olebedev revised this gist Apr 19, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions com.user.slack-spotify.plist
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@
    <key>StartInterval</key>
    <integer>60</integer>
    <!-- <key>StandardOutPath</key>
    <string>/Users/olebedev/slackspotify-output.txt</string>
    <string>/PATH/TO/slackspotify-output.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/olebedev/slackspotify-errors.txt</string> -->
    <string>/PATH/TO//slackspotify-errors.log</string> -->
    </dict>
    </plist>
  2. olebedev revised this gist Apr 19, 2020. 4 changed files with 26 additions and 11 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    .apikey
    4 changes: 4 additions & 0 deletions com.user.slack-spotify.plist
    Original file line number Diff line number Diff line change
    @@ -10,5 +10,9 @@
    <true/>
    <key>StartInterval</key>
    <integer>60</integer>
    <!-- <key>StandardOutPath</key>
    <string>/Users/olebedev/slackspotify-output.txt</string>
    <key>StandardErrorPath</key>
    <string>/Users/olebedev/slackspotify-errors.txt</string> -->
    </dict>
    </plist>
    6 changes: 3 additions & 3 deletions data.json
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "defined": {
    "emoji": "",
    "text": ""
    "text": "",
    "emoji": ":finn:"
    },
    "playing_emoji": ":headphones:"
    "playing_emoji": ":spotify:"
    }
    26 changes: 18 additions & 8 deletions slackspotify.sh
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,11 @@

    set -eu

    APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"
    # Get your api key here https://api.slack.com/custom-integrations/legacy-tokens and puth it into .apikey file.
    SCRIPT_DIR="$(dirname $0)"
    APIKEY=$(cat "${SCRIPT_DIR}"/.apikey)

    DATA="$(dirname $0)"/data.json
    DATA="${SCRIPT_DIR}"/data.json
    JQ_BIN=/usr/local/bin/jq

    function post_status() {
    @@ -13,7 +15,7 @@ function post_status() {
    -H "Content-type: application/json; charset=utf-8" \
    -H "Authorization: Bearer ${APIKEY}" \
    -d "${payload}" \
    https://slack.com/api/users.profile.set > /dev/null
    https://slack.com/api/users.profile.set >/dev/null
    }

    function current_slack_status() {
    @@ -35,21 +37,29 @@ function maybe_save_current_status() {
    local text="$(echo "${status}" | ${JQ_BIN} -r '.text')"

    if [[ "${playing}" != "${emoji}" ]]; then
    local new=$(${JQ_BIN} -r --arg text "${text}" --arg emoji "${emoji}" '.defined.text = $text | .defined.emoji = $emoji' "${DATA}")
    echo new data: "${new}"
    local new=$(${JQ_BIN} -n -r --arg text "${text}" --arg emoji "${emoji}" --arg playing "${playing}" '.defined.text = $text | .defined.emoji = $emoji | .playing_emoji = $playing')
    echo "${new}" > "${DATA}"
    fi
    }

    is_spotify_running() {
    ps aux | grep -v grep | grep -v slackspotify | grep -c spotify >/dev/null
    }

    function main() {
    date
    echo "Spotify is "$(echo_state)". Current song is: '"$(echo_song)"'."
    maybe_save_current_status
    if ! is_spotify_running; then
    echo "Spotify is not running on this machine."
    exit 0
    else
    echo "Spotify is "$(echo_state)"."
    fi

    if [[ "$(echo_state)" != "playing" ]]; then
    post_status "$(${JQ_BIN} -r '.defined.emoji' ${DATA})" "$(${JQ_BIN} -r '.defined.text' ${DATA})"
    post_status "$(${JQ_BIN} -r '.defined.emoji //empty' ${DATA})" "$(${JQ_BIN} -r '.defined.text //empty' ${DATA})"
    else
    post_status "$(${JQ_BIN} -r '.playing_emoji' ${DATA})" "$(echo_song)"
    post_status "$(${JQ_BIN} -r '.playing_emoji //empty' ${DATA})" "$(echo_song)"
    fi
    }

  3. olebedev revised this gist Oct 29, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,6 @@ $ launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist
    4. Start it by either logging out and back in or running
    ```
    $ launchctl start com.user.slack-spotify
    ```
    ```

    6. (Optional) Modify your 'playing' emoji in `./data.json`.
  4. olebedev revised this gist Aug 2, 2019. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions slackspotify.sh
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,10 @@ set -eu
    APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"

    DATA="$(dirname $0)"/data.json
    JQ_BIN=/usr/local/bin/jq

    function post_status() {
    local payload="$(jq -n -r --arg emoji "${1}" --arg text "${2}" '{"profile":{"status_text":$text,"status_emoji":$emoji}}')"
    local payload="$(${JQ_BIN} -n -r --arg emoji "${1}" --arg text "${2}" '{"profile":{"status_text":$text,"status_emoji":$emoji}}')"
    curl -s \
    -H "Content-type: application/json; charset=utf-8" \
    -H "Authorization: Bearer ${APIKEY}" \
    @@ -16,7 +17,7 @@ function post_status() {
    }

    function current_slack_status() {
    curl -s "https://slack.com/api/users.profile.get?token=${APIKEY}" | jq -r '{ text: .profile.status_text, emoji: .profile.status_emoji }'
    curl -s "https://slack.com/api/users.profile.get?token=${APIKEY}" | ${JQ_BIN} -r '{ text: .profile.status_text, emoji: .profile.status_emoji }'
    }

    function echo_state() {
    @@ -28,13 +29,13 @@ function echo_song() {
    }

    function maybe_save_current_status() {
    local playing="$(jq -r '.playing_emoji' ${DATA})"
    local playing="$(${JQ_BIN} -r '.playing_emoji' ${DATA})"
    local status=$(current_slack_status)
    local emoji="$(echo "${status}" | jq -r '.emoji')"
    local text="$(echo "${status}" | jq -r '.text')"
    local emoji="$(echo "${status}" | ${JQ_BIN} -r '.emoji')"
    local text="$(echo "${status}" | ${JQ_BIN} -r '.text')"

    if [[ "${playing}" != "${emoji}" ]]; then
    local new=$(jq -r --arg text "${text}" --arg emoji "${emoji}" '.defined.text = $text | .defined.emoji = $emoji' "${DATA}")
    local new=$(${JQ_BIN} -r --arg text "${text}" --arg emoji "${emoji}" '.defined.text = $text | .defined.emoji = $emoji' "${DATA}")
    echo new data: "${new}"
    echo "${new}" > "${DATA}"
    fi
    @@ -46,9 +47,9 @@ function main() {
    maybe_save_current_status

    if [[ "$(echo_state)" != "playing" ]]; then
    post_status "$(jq -r '.defined.emoji' ${DATA})" "$(jq -r '.defined.text' ${DATA})"
    post_status "$(${JQ_BIN} -r '.defined.emoji' ${DATA})" "$(${JQ_BIN} -r '.defined.text' ${DATA})"
    else
    post_status "$(jq -r '.playing_emoji' ${DATA})" "$(echo_song)"
    post_status "$(${JQ_BIN} -r '.playing_emoji' ${DATA})" "$(echo_song)"
    fi
    }

  5. olebedev revised this gist Aug 2, 2019. 3 changed files with 62 additions and 21 deletions.
    21 changes: 16 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,20 @@
    # Usage

    1. Link the `com.user.slack-spotify.plist` file into `~/Library/LaunchAgents/com.user.slack-spotify.plist`
    0. Get API key [here](https://api.slack.com/custom-integrations/legacy-tokens) and put it into `./slackspotify.sh` as an `APIKEY` value.

    2. Register this daemon with launchd by running
    launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist
    1. Copy the `com.user.slack-spotify.plist` file into `~/Library/LaunchAgents/com.user.slack-spotify.plist`
    ```
    $ cp com.user.slack-spotify.plist ~/Library/LaunchAgents/com.user.slack-spotify.plist
    ```

    3. Start it by either logging out and back in or running
    launchctl start com.user.slack-spotify
    2. Modify `~/Library/LaunchAgents/com.user.slack-spotify.plist` accordinally -- set path to your `./slackspotify.sh` script.

    3. Register this daemon with launchd by running
    ```
    $ launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist
    ```

    4. Start it by either logging out and back in or running
    ```
    $ launchctl start com.user.slack-spotify
    ```
    7 changes: 7 additions & 0 deletions data.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    {
    "defined": {
    "emoji": "",
    "text": ""
    },
    "playing_emoji": ":headphones:"
    }
    55 changes: 39 additions & 16 deletions slackspotify.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,54 @@
    #!/bin/bash
    #!/usr//bin/env bash

    set -eu

    APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"
    EMOJI=$(echo :headphones: | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')

    function reset() {
    echo 'Resetting status'
    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%0A%20%20%20%20%22status_text%22%3A%20%22%22%2C%0A%20%20%20%20%22status_emoji%22%3A%20%22"$EMOJI"%22%0A%7D" > /dev/null
    DATA="$(dirname $0)"/data.json

    function post_status() {
    local payload="$(jq -n -r --arg emoji "${1}" --arg text "${2}" '{"profile":{"status_text":$text,"status_emoji":$emoji}}')"
    curl -s \
    -H "Content-type: application/json; charset=utf-8" \
    -H "Authorization: Bearer ${APIKEY}" \
    -d "${payload}" \
    https://slack.com/api/users.profile.set > /dev/null
    }

    function set_status() {
    SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
    URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
    echo $SONG
    function current_slack_status() {
    curl -s "https://slack.com/api/users.profile.get?token=${APIKEY}" | jq -r '{ text: .profile.status_text, emoji: .profile.status_emoji }'
    }

    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%0A%20%20%20%20%22status_text%22%3A%20%22"$URLSONG"%22%2C%0A%20%20%20%20%22status_emoji%22%3A%20%22"$EMOJI"%22%0A%7D" > /dev/null
    function echo_state() {
    osascript -e 'tell application "Spotify" to player state'
    }

    function main() {
    state=$(osascript -e 'tell application "Spotify" to player state')
    function echo_song() {
    osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track'
    }

    function maybe_save_current_status() {
    local playing="$(jq -r '.playing_emoji' ${DATA})"
    local status=$(current_slack_status)
    local emoji="$(echo "${status}" | jq -r '.emoji')"
    local text="$(echo "${status}" | jq -r '.text')"

    if [[ "${playing}" != "${emoji}" ]]; then
    local new=$(jq -r --arg text "${text}" --arg emoji "${emoji}" '.defined.text = $text | .defined.emoji = $emoji' "${DATA}")
    echo new data: "${new}"
    echo "${new}" > "${DATA}"
    fi
    }

    function main() {
    date
    echo "Spotify: "$state
    echo "Spotify is "$(echo_state)". Current song is: '"$(echo_song)"'."
    maybe_save_current_status

    if [[ "$state" != "playing" ]]; then
    reset
    if [[ "$(echo_state)" != "playing" ]]; then
    post_status "$(jq -r '.defined.emoji' ${DATA})" "$(jq -r '.defined.text' ${DATA})"
    else
    set_status
    post_status "$(jq -r '.playing_emoji' ${DATA})" "$(echo_song)"
    fi
    }

  6. olebedev revised this gist May 29, 2019. 3 changed files with 53 additions and 8 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Usage

    1. Link the `com.user.slack-spotify.plist` file into `~/Library/LaunchAgents/com.user.slack-spotify.plist`

    2. Register this daemon with launchd by running
    launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist

    3. Start it by either logging out and back in or running
    launchctl start com.user.slack-spotify
    14 changes: 14 additions & 0 deletions com.user.slack-spotify.plist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Label</key>
    <string>com.user.slack-spotify</string>
    <key>ProgramArguments</key>
    <array><string>/PATH/TO/SCRIPT.sh</string></array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>60</integer>
    </dict>
    </plist>
    38 changes: 30 additions & 8 deletions slackspotify.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,32 @@
    #!/bin/bash

    APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"
    SONG=$(osascript -e 'tell application "Spotify" to name of current track as string')
    URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')

    while true
    do
    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22"$URLSONG"%22%2C%22status_emoji%22%3A%22%3Amusical_note%3A%22%7D" > /dev/null
    sleep 60
    done
    EMOJI=$(echo :headphones: | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')

    function reset() {
    echo 'Resetting status'
    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%0A%20%20%20%20%22status_text%22%3A%20%22%22%2C%0A%20%20%20%20%22status_emoji%22%3A%20%22"$EMOJI"%22%0A%7D" > /dev/null
    }

    function set_status() {
    SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
    URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
    echo $SONG

    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%0A%20%20%20%20%22status_text%22%3A%20%22"$URLSONG"%22%2C%0A%20%20%20%20%22status_emoji%22%3A%20%22"$EMOJI"%22%0A%7D" > /dev/null
    }

    function main() {
    state=$(osascript -e 'tell application "Spotify" to player state')

    date
    echo "Spotify: "$state

    if [[ "$state" != "playing" ]]; then
    reset
    else
    set_status
    fi
    }

    main
  7. @jgamblin jgamblin created this gist Apr 19, 2017.
    10 changes: 10 additions & 0 deletions slackspotify.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/bash
    APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"
    SONG=$(osascript -e 'tell application "Spotify" to name of current track as string')
    URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')

    while true
    do
    curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22"$URLSONG"%22%2C%22status_emoji%22%3A%22%3Amusical_note%3A%22%7D" > /dev/null
    sleep 60
    done