forked from gotbletu/shownotes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
404urlcleaner
executable file
·92 lines (81 loc) · 2.69 KB
/
404urlcleaner
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
90
#!/usr/bin/env bash
### _ _ _ _
### __ _ ___ | |_| |__ | | ___| |_ _ _
### / _` |/ _ \| __| '_ \| |/ _ \ __| | | |
###| (_| | (_) | |_| |_) | | __/ |_| |_| |
### \__, |\___/ \__|_.__/|_|\___|\__|\__,_|
### |___/
### https://www.youtube.com/user/gotbletu
### https://twitter.com/gotbletu
### https://github.com/gotbletu
###
### Name : 404urlcleaner
### Version : 0.3
### Date : 20190508
### Description : Remove all dead urls with HTTP status code 404 (mainly use with newsboat/newsbeuter)
### Depends On : bash curl coreutils grep moreutils
### Video Demo : https://youtu.be/dTpEuQZRRDM
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
display_usage() {
echo "Remove all dead urls with HTTP status code 404 (mainly use with newsboat/newsbeuter)"
echo -e "\n Usage:\n $0 [file]"
echo -e "\n Example:\n $0 ~/.newsboat/urls"
}
# if less than one argument supplied, display usage
if [ $# -lt 1 ]
then
display_usage
exit 1
fi
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $@ == "--help") || $@ == "-h" ]]
then
display_usage
exit 0
fi
# all url status code
TMPFILE1=/tmp/urlstatus.txt
# only 404 status code
TMPFILE2=/tmp/urlstatus2nd.txt
# create backup file
echo -e "${Green}>>>Creating Backup File${Color_Off}"
cp -aiv "$1" "$1_`date +'%F_%Hh%Ms%S'`"
# check all links and print http code
echo -e "${Green}>>>Checking HTTP Status Code${Color_Off}"
grep '^http' -i "$1" | awk '{print $1}' | while read url
do
urlstatus=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "$url")
echo "$url C0DE-$urlstatus" | tee -a "$TMPFILE1"
done
# double check 404 links again (sometime the first check is not acurrate)
echo -e "${Blue}>>>Double Checking ONLY DEAD HTTP Status Code${Color_Off}"
grep 'C0DE-404' -i "$TMPFILE1" | awk '{print $1}' | while read url
do
urlstatus=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "$url")
echo "$url C0DE-$urlstatus" | tee -a "$TMPFILE2"
done
# remove all 404
grep 'C0DE-404' -s -i "$TMPFILE2" | awk '{print $1}' | while read url
do
grep -F -v "$url" "$1" | sponge "$1"
echo -e "${Red}>>>REMOVING $url ${Color_Off}"
done
# remove temp files if it exist
if [ -f "$TMPFILE1" ] ; then
rm "$TMPFILE1"
fi
if [ -f "$TMPFILE2" ] ; then
rm "$TMPFILE2"
fi
echo -e "${Green}>>>Scan Completed${Color_Off}"