Skip to content

Instantly share code, notes, and snippets.

@johnallen3d
Created June 9, 2018 22:17
Show Gist options
  • Save johnallen3d/e9f175012c52892ddb46a08f4158ee8f to your computer and use it in GitHub Desktop.
Save johnallen3d/e9f175012c52892ddb46a08f4158ee8f to your computer and use it in GitHub Desktop.
Take the formatted output of `mpc status` and convert it into JSON...
#! /usr/bin/env bash
STATUS="$(/usr/local/bin/mpc status)"
if [ $(echo "$STATUS" | wc -l | tr -d ' ') == "1" ]; then
status="[empty]"
else
artist=$(echo "$STATUS" | awk -F" - " 'NR==1{print $1}')
song=$(echo "$STATUS" | awk -F" - " 'NR==1{print $2}')
status=$(echo "$STATUS" | awk 'NR==2{print $1}')
track_data=$(echo "$STATUS" | awk 'NR==2{print $2}')
current_track_number=$(echo "$track_data" | awk -F"/" '{print $1}')
total_track_count=$(echo "$track_data" | awk -F"/" '{print $2}')
elapsed=$(echo "$STATUS" | awk 'NR==2{print $3}')
current_time=$(echo "${elapsed}" | awk -F"/" '{print $1}')
track_length=$(echo "${elapsed}" | awk -F"/" '{print $2}')
volume=$(
echo "$STATUS" \
| awk -F":" 'NR==3{print $2}' \
| awk '{print $1}'
)
fi
echo "{
\"artist\": \"${artist}\",
\"song\": \"${song}\",
\"elapsed\": \"${elapsed}\",
\"currentTime\": \"${current_time}\",
\"trackLength\": \"${track_length}\",
\"currentTrackNumber\": \"${current_track_number/\#/}\",
\"totalTrackCount\": \"${total_track_count}\",
\"status\": \"${status}\",
\"volume\": \"${volume}\"
}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment