Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Last active December 9, 2018 23:31
Show Gist options
  • Save micalevisk/b2bbf84335e1108553964146fcae3e4e to your computer and use it in GitHub Desktop.
Save micalevisk/b2bbf84335e1108553964146fcae3e4e to your computer and use it in GitHub Desktop.
Simple Shell Script to list all channels in which a Twitch TV (API v5) user is on chat
#!/bin/bash
#
# List all channels in which a Twitch TV user is on chat.
#
# USAGE:
# ./list_channels.sh <user_id> <username>
#
# Created by Micael Levi on 22/01/2018
# Copyright (c) 2018 <[email protected]> All rights reserved
#
# using: mapfile, curl, jq, printf, rm, sleep
#
[ $# -ne 2 ] && { printf "%s <user_id> <username>\\n" "$0"; exit 1; }
declare -r CLIENT_ID="<YOUR-CLIENT-ID>"
declare -r LOADING_MARKER="(...)"
declare -i amount=0
declare -r user_id="$1" username="$2"
## @see https://dev.twitch.tv/docs/v5/reference/users#get-user-follows
mapfile -t follows <<< "$(curl -s -H 'Accept: application/vnd.twitchtv.v5+json' -X GET "https://api.twitch.tv/kraken/users/${user_id,,}/follows/channels?client_id=${CLIENT_ID}" | jq --raw-output '.follows[].channel.name')"
[ -z "${follows[0]}" ] && { rm -f jq*.stackdump; exit 2; }
printf "'%s'\\x1B[s is online on channels:\\n" "$username"
for channel in "${follows[@]}"
do
printf "%s%s" "$channel" "$LOADING_MARKER"
isOnChat=$(curl -s "http://tmi.twitch.tv/group/user/${channel,,}/chatters" | jq --raw-output ".chatters | flatten | contains([\"${username,,}\"])")
[ "$isOnChat" == "true" ] && { printf "\\x1B[%dD\\x1B[K\\n" "${#LOADING_MARKER}" ; ((amount++)); } || printf "\\x1B[G\\x1B[K"
sleep 0.1
done
[ "$amount" -eq 0 ] && printf "\\x1B[u isn't online on any channel (that his follows) \\x1B[K\\n"
@micalevisk
Copy link
Author

micalevisk commented Jan 21, 2018

download the script

wget https://gist.githubusercontent.com/micalevisk/b2bbf84335e1108553964146fcae3e4e/raw/4904d1acc214b9e31531689eb09e738e23575b17/list_channels.sh \
&& chmod +x list_channels.sh 

ways to get user_id

  1. Go to user channel page (https://www.twitch.tv/<username>). Inspect any image on his panels and on src attribute get the number between panel- and -image. Like https://static-cdn.jtvnw.net/jtv_user_pictures/panel-40932410-image-...
  2. or use the Twitch API v5 (c) https://dev.twitch.tv/docs/v5#translating-from-user-names-to-user-ids like:
curl -H 'Accept: application/vnd.twitchtv.v5+json' -H "Client-ID: ${CLIENT_ID}" \
-X GET https://api.twitch.tv/kraken/users?login=jeffreyeski,3n3j0ta1o

demo

demo

asciicast

@micalevisk
Copy link
Author

micalevisk commented Dec 9, 2018

update 2018-12-09 ~ version with auto-fetch user_id:

#!/bin/bash
[ $# -ne 1 ] && { printf "%s <username>\\n" "$0"; exit 1; }

declare -r CLIENT_ID="<YOUR-CLIENT-ID>" ## <= put yours
declare -r API_URL="https://api.twitch.tv"
declare -r LOADING_MARKER="(...)"
declare -i amount=0
declare -r username="${1,,}"

local user_id="$(curl -s -H 'Accept: application/vnd.twitchtv.v5+json' -H "Client-ID: ${CLIENT_ID}" \
               -X GET "${API_URL}/kraken/users?login=${username,,}" | jq --raw-output '.users[]?._id')"

[ -z "$user_id" ] && { rm -f jq*.stackdump; return 2; }

mapfile -t follows <<< "$(curl -s -H 'Accept: application/vnd.twitchtv.v5+json' \
    -X GET "${API_URL}/kraken/users/${user_id,,}/follows/channels?client_id=${CLIENT_ID}" | jq --raw-output '.follows[].channel.name')"

[ -z "${follows[0]}" ] && { rm -f jq*.stackdump; exit 2; }

printf "'%s'\\x1B[s is online on channels:\\n" "$username"

for channel in "${follows[@]}"
do
	printf "%s%s" "$channel" "$LOADING_MARKER"
	isOnChat=$(curl -s "http://tmi.twitch.tv/group/user/${channel,,}/chatters" | jq --raw-output ".chatters | flatten | contains([\"${username,,}\"])")
	[ "$isOnChat" == "true" ] && { printf "\\x1B[%dD\\x1B[K\\n" "${#LOADING_MARKER}" ; ((amount++)); } || printf "\\x1B[G\\x1B[K"
	sleep 0.1
done

[ "$amount" -eq 0 ] && printf "\\x1B[u isn't online on any channel (that his follows) \\x1B[K\\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment