-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_url
executable file
·192 lines (170 loc) · 4.75 KB
/
archive_url
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /bin/sh
# archive_url - save not yet archived URLs in the Wayback Machine
# Copyright (C) 2021-2024 Erik Auerswald
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -u
set -e
PROG=archive_url
VERSION='2024-09-22-01'
FORCE=0
DRYRUN=0
QUIET=0
VERBOSE=0
WAIT=''
WAITTIME=5
JITTERWAIT=0
WGETVERB='-nv'
CHECK='https://archive.org/wayback/available?url='
ARCHIVE='https://web.archive.org/save/'
EXIT_CODE=0
print_copyright()
{
echo 'Copyright (C) 2021-2024 Erik Auerswald'
}
print_version()
{
printf -- '%s version %s\n' "${PROG}" "${VERSION}"
}
print_usage()
{
printf -- '%s { -h | -V | -L }\n' "$PROG"
printf -- '%s [-f] [-n] [-q] [-v] [-w TIME] [-r] [URL...]\n' "$PROG"
}
print_license()
{
cat <<EOL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
EOL
}
print_help()
{
print_version
print_copyright
echo
print_license
cat <<EOD
Check if the given URLs are already archived in the Wayback Machine
(web.archive.org), and attempt to archive those that are not.
URLs can be provided as arguments.
Without arguments, URLs are read from standard input.
EOD
print_usage
cat <<EOH
Options:
-h print this help and exit
-V print version information and exit
-L print license and exit
-f save URLs without checking
-n check URLs only, but do not archive (dry run)
-q suppress error messages and wget output
-v print URL that is processed next
-w TIME wait at least TIME seconds after each URL (default $WAITTIME)
-r randomize wait time between URLs
EOH
}
verb_no_nl() {
test "$VERBOSE" -eq 1 || return 0
printf -- '%s' "$*"
}
verb_nl() {
test "$VERBOSE" -eq 1 || return 0
printf -- '%s\n' "$*"
}
err() {
EXIT_CODE=1
test "$QUIET" -eq 0 || return 0
printf -- '%s: ERROR: %s\n' "$PROG" "$*" 1>&2
}
add_jitter() {
if command -v 'shuf' >/dev/null; then
WAITINT=${WAITTIME%%.*}
WAITMAX=$((WAITINT / 5))
JITTERINT=$(shuf -i"0-${WAITMAX}" -n1)
JITTERFRAC=$(shuf -i"0-999" -n1)
echo "$((WAITINT + JITTERINT)).${JITTERFRAC}"
else
echo "$WAITTIME"
fi
}
do_wait() {
REALWAIT="$WAITTIME"
test "$JITTERWAIT" -eq 1 && REALWAIT=$(add_jitter)
verb_no_nl 'waiting' "$REALWAIT" 'seconds ...'
sleep -- "$REALWAIT"
verb_nl ' done'
}
while getopts ':hVLfnqvw:r' OPT; do
case "$OPT" in
'h') print_help; exit 0;;
'V') print_version; print_copyright; exit 0;;
'L') print_version; print_copyright; echo; print_license; exit 0;;
'f') FORCE=1;;
'n') DRYRUN=1;;
'q') QUIET=1 WGETVERB='-q';;
'v') VERBOSE=1;;
'w') WAITTIME="$OPTARG";;
'r') JITTERWAIT=1;;
'?') echo "$PROG: error: unknown option '-$OPTARG'"; print_usage; exit 1;;
':') echo "$PROG: error: argument required for option '-$OPTARG'";
print_usage; exit 1;;
*) echo "$PROG: error: getopts() failure"; exit 1;;
esac
done
shift $((OPTIND - 1))
check_and_archive_url()
{
URL="$1"
if test "$FORCE" -eq 0; then
verb_no_nl "Checking ${URL} ..."
STATUS="$(wget -q -O- -- "${CHECK}${URL}" | jq '.archived_snapshots != {}')"
else
verb_no_nl "Forcing ${URL} ..."
STATUS='false'
fi
if test "$STATUS" = 'true'; then
verb_nl ' archived'
else
if test "$DRYRUN" -eq 0; then
verb_nl ' triggering archiving ...'
wget "$WGETVERB" -O/dev/null -- "${ARCHIVE}${URL}" ||
err 'error archiving URL'
else
verb_nl ' not archived (dry run)'
fi
fi
}
if test "$#" -eq 0; then
while read -r URL; do
test -n "$WAIT" && do_wait
check_and_archive_url "$URL"
WAIT='true'
done
else
for URL in "$@"; do
test -n "$WAIT" && do_wait
check_and_archive_url "$URL"
WAIT='true'
done
fi
exit "$EXIT_CODE"