-
Notifications
You must be signed in to change notification settings - Fork 1
/
title.sh
executable file
·39 lines (33 loc) · 1.1 KB
/
title.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
title=
dir="$1"
link="$2"
localTitle="$(awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$dir/index.html")"
if [[ "$localTitle" == "Redirecting to"* ]] || [ "$localTitle" == "redirect-title" ] || [ -z "$localTitle" ]; then
webTitle="$(wget --timeout=5 -qO- "$link" | awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}')"
# Trim whitespace and limit to one line to tame crazy pages (looking at you, RedBubble)
webTitle=$(echo "$webTitle" | awk '{$1=$1};1' | sed -n '1,1 p')
useWebTitle=
if [ -z "$webTitle" ]
then
useWebTitle="n"
else
read -rp "Do you want to use \"$webTitle\" as the title for \"$link\"? [Y/n] " -e useWebTitle </dev/tty
# Default value
useWebTitle=${useWebTitle:-Y}
# Trim whitespace
useWebTitle=$(echo "$useWebTitle" | awk '{$1=$1};1')
fi
if [[ "$useWebTitle" == "Y" ]]; then
title="$webTitle"
else
while [ -z "$title" ]; do
read -rp "What title do you want to use for \"$link\"? " -e title </dev/tty
# Trim whitespace
title=$(echo "$title" | awk '{$1=$1};1')
done
fi
else
title="$localTitle"
fi
echo "$title"