Created
July 19, 2024 19:52
-
-
Save nicksherron/e89ff7cb89e64ec56c53b1b725d318fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Wakeup Script | |
# This script sets the volume to 100% and plays a song from a Spotify playlist. | |
# It is intended to be run by a cron job. | |
PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:$HOME/.cargo/bin" | |
#SPT="$HOME/.cargo/bin/spt" | |
TARGET_DEVICE="mac" # The device to play the song on | |
# Function to restart the Spotify daemon | |
restart_spotifyd() { | |
brew services restart spotifyd | |
} | |
# Function to get the active Spotify device | |
get_device() { | |
device=$(spt list --devices -f '%d' | grep $TARGET_DEVICE | head -n 1) | |
if [ -z "$device" ]; then | |
restart_spotifyd &> /tmp/spotify_wakeup_tmp.log | |
if [ $? -ne 0 ]; then | |
log "Failed to restart spotifyd: $(cat /tmp/spotify_wakeup_tmp.log)" | |
rm /tmp/spotify_wakeup_tmp.log &> /dev/null | |
exit 1 | |
fi | |
device=$(spt list --devices -f '%d' | grep $TARGET_DEVICE | head -n 1) | |
fi | |
echo "$device" | |
} | |
log() { | |
echo "$(date) - $1" >> /tmp/spotify_wakeup.log | |
} | |
# Function to play a song and set the volume | |
play() { | |
device=$(get_device) | |
if [ -z "$device" ]; then | |
echo "No devices found" | |
exit 1 | |
fi | |
sleep 5 | |
failed=0 | |
# Attempt to play the song | |
spt play -p -n 'trip' -r --device "$device" &> /tmp/spotify_wakeup_tmp.log | |
if [ $? -ne 0 ]; then | |
log "Failed to play song: $(cat /tmp/spotify_wakeup_tmp.log)" | |
failed=1 | |
fi | |
# Set the volume to 100% | |
spt playback --device "$device" --volume 100 &> /tmp/spotify_wakeup_tmp.log | |
if [ $? -ne 0 ]; then | |
log "Failed to set volume: $(cat /tmp/spotify_wakeup_tmp.log)" | |
failed=1 | |
fi | |
rm /tmp/spotify_wakeup_tmp.log &> /dev/null | |
if [ $failed -eq 1 ]; then | |
exit 1 | |
fi | |
} | |
if ! pgrep -x "spotifyd" > /dev/null; then | |
restart_spotifyd | |
if [ $? -ne 0 ]; then | |
echo "Failed to start spotifyd" | |
exit 1 | |
fi | |
fi | |
# Check if Spotify is running | |
if ! pgrep -x "Spotify" > /dev/null; then | |
open -a Spotify | |
sleep 5 | |
# Wait for Spotify to start and get a device | |
n=0 | |
until `[ -n "$(get_device)" ]`; do | |
if [ $n -eq 5 ]; then | |
echo "Failed to start Spotify" | |
exit 1 | |
fi | |
sleep 1 | |
n=$((n+1)) | |
done | |
fi | |
# Set the output volume to 100% and play the song | |
osascript -e 'set volume output volume 100' | |
play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment