Skip to content

Instantly share code, notes, and snippets.

@flexiondotorg
Last active December 16, 2021 07:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flexiondotorg/f6deafa4313c1f7d8a30f9da6c57b760 to your computer and use it in GitHub Desktop.
Save flexiondotorg/f6deafa4313c1f7d8a30f9da6c57b760 to your computer and use it in GitHub Desktop.
Get Twitch channel metrics via DecAPI
#!/usr/bin/env bash
TWITCH_CHANNEL="wimpysworld"
METRICS_PATH="${HOME}/Studio/OBS/twitch-metrics"
mkdir -p "${METRICS_PATH}"
for ENDPOINT in $(wget -q https://decapi.net/twitch/ -O - | jq -r '[.endpoints[] | select(contains("CHANNEL")) | select(contains("USER") | not)] | @tsv'); do
ENDPOINT_NAME=$(echo "${ENDPOINT}" | cut -d'/' -f5)
ENDPOINT_URL="${ENDPOINT//\{CHANNEL\}/${TWITCH_CHANNEL}}"
# Get the 5 most recent followers and skip deprecated endpoints.
if [ "${ENDPOINT_NAME}" == "followers" ]; then
ENDPOINT_URL+="?count=5"
elif [[ "${ENDPOINT_NAME}" == *hosts* ]]; then
echo " - Deprecated; skipping it."
continue
fi
echo " - Getting ${ENDPOINT_URL}"
DATA=$(wget -q "${ENDPOINT_URL}" -O-)
# If the channel is offline, report zero viewers
if [ "${ENDPOINT_NAME}" == "viewercount" ] && [[ "${DATA}" == *offline* ]]; then
DATA="0"
fi
echo -n "${DATA}" > "${METRICS_PATH}/${ENDPOINT_NAME}.txt"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment