dotfiles/hm/scripts/lip

33 lines
553 B
Plaintext
Raw Normal View History

#!/usr/bin/env nix-shell
#! nix-shell -i bash --pure
#! nix-shell -p bash jq curl findutils coreutils
2023-07-23 13:28:39 +02:00
set -euo pipefail
url="https://ip.frogeye.fr/json"
cachedir="$HOME/.cache/lip"
ip="${1:-}"
2023-07-25 13:33:22 +02:00
jq_sel="del(.user_agent)"
if [ $# -gt 1 ]
2023-07-23 13:28:39 +02:00
then
shift
jq_sel="$@"
fi
2023-07-25 13:33:22 +02:00
2023-07-23 13:28:39 +02:00
if [ -n "$ip" ]
then
cachefile="$cachedir/$ip"
if ! find "$cachefile" -mtime -7 &> /dev/null
then
mkdir -p "$cachedir"
2023-07-25 13:23:56 +02:00
curl --silent "$url?ip=$ip" > "$cachefile"
2023-07-23 13:28:39 +02:00
fi
cat "$cachefile" | jq $jq_sel
else
curl --silent "$url" | jq $jq_sel
fi