This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
forked from Tokyo-Metro-Gov/covid19
-
Notifications
You must be signed in to change notification settings - Fork 10
/
link_check.sh
executable file
·89 lines (81 loc) · 1.79 KB
/
link_check.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
source .env
function url_extract(){
#grep -Eo "http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" $1
grep -Eo "http(s?)://(\w|:|%|#|\$|&|\?|\(|\)|~|\.|=|\+|\-|/)+" $1
}
function url_extract_all(){
for file in $(find $1)
do
if ! file --mime $file |grep -e "binary" 1>/dev/null;then
if [ -f $file ]; then
url_extract $file
fi
fi
done|sort|uniq
}
function status_code(){
curl --head $1 2>/dev/null| head -n 1 |sed -r "s/.* ([0-9]*) .*/\1/"
}
function link_check(){
status=$(status_code $1)
if [ "$status" == "" ]; then
echo "broken link"
return 1;
else
if [ "$status" -eq 404 ]; then
echo "broken link"
return 1;
fi
fi
echo "normal link"
return 0;
}
function link_check_all(){
result=0
for url in $(cat /dev/stdin)
do
if ! link_check $url 1>/dev/null; then
echo "$url : broken" >&2
echo $url
result=1
else
echo "$url : living" >&2
fi
done
return $result
}
function broken_link_test(){
DOMAIN_NAME=$1
wget -r -np --random-wait -x https://$DOMAIN_NAME --directory-prefix=downloaded_pages
url_extract_all ./downloaded_pages/$DOMAIN_NAME | link_check_all
result=$?
rm -rf downloaded_pages
return $result
}
function send_message(){
MESSAGE=$(cat /dev/stdin | sed -e 's/"/\\\"/g')
HOOK_URL=$1
curl \
-X POST\
-H 'Content-type: application/json'\
--data "{\"text\":\"$MESSAGE\"}"\
$HOOK_URL
}
#====MAIN======
if [ "$HOOK_URL" == "" ]; then
echo "ERROR: HOOK_URL is empty."
echo "Please create slack incoming webhook and assign its url to HOOK_URL in setting.sh."
exit 1;
fi
if ! broken_links=$(broken_link_test $TARGET_DOMAIN); then
send_message $HOOK_URL << EOS
WARNING: Broken link detected!!
~~~~~Broken link list~~~~~~~~~
$broken_links
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EOS
else
send_message $HOOK_URL << EOS
SUCCESS: Broken link not detected!!
EOS
fi