Last active
March 29, 2021 06:42
-
-
Save vladimyr/d7bb314b14a59ca5fbefda3498fac81a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
UA="tldr-pages-bad-url-checker (+https://github.com/tldr-pages/tldr; implemented by @sbrl)" | |
pushd "$HOME/.tldr/cache/pages" >/dev/null 2>&1 | |
records=$(rg --sort-files -o '<(https?:[^>]+)>' -r '$1' | cat) | |
tempdir=$(mktemp -d) | |
echo "tempdir: $tempdir" >&2 | |
for record in $records; do | |
file=$(echo "$record" | cut -d':' -f1) | |
url=$(echo "$record" | cut -d':' -f2-) | |
md5=$(echo "$url" | md5sum | cut -d' ' -f1) | |
status_code= | |
if [[ -f $tempdir/$md5 ]]; then | |
status_code=$(cat "$tempdir/$md5") | |
else | |
status_code=$(curl --user-agent "$UA" -so /dev/null -w "%{http_code}" -I "$url" || echo 0) | |
if [[ $status_code == 405 ]]; then | |
status_code=$(curl --user-agent "$UA" -so /dev/null -w "%{http_code}" "$url" || echo 0) | |
fi | |
echo "$status_code" >"$tempdir/$md5" | |
fi | |
echo "$status_code $url" >&2 | |
if [[ $status_code -lt 200 ]] || [[ $status_code -ge 400 ]]; then | |
echo "$file $url $status_code"; | |
fi | |
done | |
popd >/dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment