fetch_favicon.sh (1591B)
1 #!/usr/bin/env sh
2
3 # fetch input config file, fall back to "general"
4 if [ -z $1 ]; then
5 CONFIG="centre.yaml"
6 else
7 CONFIG="$1"
8 fi
9
10 # read yaml file
11 URLS=`grep -E "^\s*-*\ web:" "$CONFIG" | sed -e 's/^.*web:\ //g'`
12
13 # get domains
14 DOMAINS=`
15 echo "$URLS" | sed -E -e 's/https:\/\/([^\/]*).*$/\2/g' | sort | uniq
16 `
17
18 # Ensure directory exists
19 mkdir -p ./assets/site-icons/
20
21 # loop through websites
22 for DOMAIN in $DOMAINS; do
23
24 # User output
25 printf "Fetching favicon for ${DOMAIN}...\n"
26
27 # Only refresh favicons every 24hours
28 if [ -f "./assets/site-icons/${DOMAIN}" ]; then
29 NOW=`date +%s`
30 TST=`date -r "assets/site-icons/${DOMAIN}" +%s`
31 DIF=`echo "$NOW - $TST" | bc`
32 if [ $DIF -lt 86400 ]; then
33 printf "Skipping!\n"
34 continue; # skip for-loop item
35 fi
36 fi
37
38 # read HTML of webpage
39 HTML=`curl --silent --location "$DOMAIN"`
40
41 # Fetch Favicon reference from HTML
42 ICN_URI=`
43 echo "$HTML" | \
44 grep "rel=\"[^\"]*icon[^\"]*\"" | \
45 grep -v ".svg" | \
46 tail -n 1 | \
47 sed -E -e 's/^.*href=\"([^\"]*)\".*$/\1/'
48 `
49
50 # Post-Process favicon url in order to prepare the download
51 ICN_URL=`
52 echo "$ICN_URI" | \
53 sed -E \
54 -e "s/^\//https:\/\/$DOMAIN\//" \
55 -e "s/^(\.+)/https:\/\/$DOMAIN\/\1/"
56 `
57
58 # Download Favicon to assets folder
59 # If no favicon URL could be found, link a fallback icon
60 if [ ! -z $ICN_URL ]; then
61 curl --silent --location \
62 --output "./assets/site-icons/${DOMAIN}" \
63 "$ICN_URL"
64 else
65 cp ./assets/fallback-site-icon.png ./assets/site-icons/${DOMAIN}
66 fi
67
68 done
69