Skip to content

Instantly share code, notes, and snippets.

@Lassi-Koykka
Last active May 13, 2023 03:17
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lassi-Koykka/1d754890f9e6fa92103f0b52dbc860eb to your computer and use it in GitHub Desktop.
Save Lassi-Koykka/1d754890f9e6fa92103f0b52dbc860eb to your computer and use it in GitHub Desktop.
A small zsh script for getting and displaying Dilbert comics in kitty terminal
# DILBERT
# Add this to your .bashrc or if you are using oh-my-zsh add a functions.zsh file under your oh-my-zsh/custom folder.
# Requirements:
# - Kitty terminal
# - ripgrep
# - ImageMagick
function dilbert () {
officialUrl=https://dilbert.com
url=""
data=""
comicNum=0
pageNum=1
# SPECIFIC COMIC
if [[ "$1" =~ '[0-9-]{10}' ]] ; then
url=$(curl -s $officialUrl/strip/$1 | rg -oP 'src="\Khttps://assets.amuniversal.com[^"]+')
if [[ -z $url ]] ; then
echo "Comic not found"
return
fi
echo "\nDilbert -- $1\n"
kitty +kitten icat --align left $url
return
fi
# QUERY
if [[ "$1" == "-q" ]] ; then
data=$(curl -s "$officialUrl/search_results?sort=date_desc&terms=$2")
pages=($(echo $data | rg -oP 'page=\K[^&]*'))
lastPage=${pages[-2]}
if ! [[ -z "$lastPage" ]] && [[ "$3" != "newest" ]] ; then
pageNum=$((1 + $RANDOM % lastPage))
fi
data=$(curl -s "$officialUrl/search_results?sort=date_desc&terms=$2&page=$pageNum")
# RANDOM
else
if [[ "$1" != "newest" ]] ; then
pageNum=$((1 + $RANDOM % 100))
fi
data=$(curl -s "$officialUrl/search_results?sort=date_desc&terms=+&page=$pageNum")
fi
#Arrays of image urls and dates
srcs=($(echo $data | rg -oP 'src="\Khttps://assets.amuniversal.com[^"]+'))
dates=($(echo $data | rg -oP 'comic-title-link" href="https://dilbert.com/strip/\K[^"]+'))
len=${#srcs[@]}
if [[ len -lt 1 ]] ; then
echo "No comics found"
return
fi
# Random comic number
comicNum=$((1 + $RANDOM % $len))
#Select first comic
if [[ comicNumb -eq 0 ]] || [[ len -eq 1 ]] || [[ "$1" == "newest" ]] || [[ "$3" == "newest" ]] ; then
date=${dates[@]:0:1}
url=${srcs[@]:0:1}
else
# Select random comic from page
date=${dates[$comicNum]}
url=${srcs[$comicNum]}
fi
# Display the image using icat or alternatively print the alt-text and transcript of the comic
echo "\nDilbert -- $date\n"
kitty +kitten icat --align left $url
echo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment