Hund

My Bash-script for printing online channels using Twitchy

February 15, 2019

I couldn’t find an actively maintained TUI or interactive CLI-client. And I didn’t want to manually check who’s online on Twitch. I then decided to make a Bash-script that prints who’s online every N minutes with some fancy colours to make it look good. This is the result:

The script

This is my second version of it. I’m not a programmer in any way, but I like tinkering with Bash. I tried to make it easy to change the colours yourself by adding variables. I also made it so it will cut the title and end with the character if it’s longer than the width of the terminal.

If you want to change the order of what it prints out, it shouldn’t be that difficult to change that either.

#!/bin/bash
# This requires Twitchy to work:
# https://github.com/BasioMeusPuga/twitchy

online=$HOME/.cache/twitchy.txt
width=$(expr $(tput cols) - 13)
twitchy --non-interactive | sort > $online

#------------------------------#
# Colours
#------------------------------#

default="\e[39m"
black="\e[30m"
red="\e[32m"
yellow="\e[33m"
blue="\e[34m"
magenta="\e[35m"
cyan="\e[36m"
lightGray="\e[37m"
darkGray="\e[90m"
lightRed="\e[91m"
lightGreen="\e[92m"
lightYellow="\e[93m"
lightBlue="\e[94m"
lightMagenta="\e[95m"
lightCyan="\e[96m"
white="\e[97m"

cName=$yellow
cTitle=$magenta
cGame=$blue
cUptime=$default
cDash=$default
cTwitchLogo=$magenta
cTwitchText=$default

#------------------------------#

if [[ -z $online ]]; then
	echo -e "\n$cTwitchLogo  _______       _ _       _
 |__   __|     (_) |     | |    
    | |_      ___| |_ ___| |__  
    | \ \ /\ / / | __/ __| '_ \ 
    | |\ V  V /| | || (__| | | |
    |_| \_/\_/ |_|\__\___|_| |_|

	$cTwitchText one is online. :("
else
    while read line
    do
        name=$(echo "$line" | cut -d "," -f1)
        title=$(echo "$line" | cut -d "," -f3- | rev | cut -d "," -f2- | rev)
        titleLenght=$(echo "$title" | wc -c)
        if [[ $titleLenght -lt "$width" ]]; then
            title=$(echo "$line" | cut -d "," -f3- | rev | cut -d "," -f2- | rev)
        else
            title=$(echo "$line" | cut -d "," -f3- | rev | cut -d "," -f2- | rev | cut -c1-$width | sed '0,/ /s/.$/img/' | sed '${s/$/\…/}')
        fi
        game=$(echo "$line" | cut -d "," -f2)
        uptime=$(echo "$line" | cut -d "," -f3- | rev | cut -d "," -f1 | rev)
    
        echo -e "\n $cName$name $cDash- $cTitle$title\n $cGame$game $cUptime($uptime)"
    done < "$online"
fi 

Usage

I use the script with the tool watch so I can make it automatically update every 5 minutes:

$ watch -c -t -n 300 twitchyBash.sh

The flags used is:

-c --color
-t --no-title
-n --interval

And to get a fancy title for the terminal:

$ printf "\33]2;%s\007" "twitchyBash" && watch -c -t -n 300 twitchyBash.sh

Example alias

To make life easier I added an alias called live for it:

alias live='printf "\33]2;%s\007" "twitchyBash" && watch -c -t -n 300 twitchyBash.sh'

Meta

No Comments

Use the e-mail form, if you wish to leave feedback for this post. Markdown is supported. [Terms of service]