forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinboardwaybackmachine
More file actions
executable file
·99 lines (84 loc) · 2.73 KB
/
Copy pathpinboardwaybackmachine
File metadata and controls
executable file
·99 lines (84 loc) · 2.73 KB
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
#!/bin/bash
readonly program="$(basename "${0}")"
function syntax_error {
echo -e "${program}: ${1}\nTry \`${program} --help\` for more information." >&2
exit 1
}
# Instructions
function usage {
echo "
Usage:
${program} [options]
'token' options are compatible between pinboardbackup, pinboardlinkcheck, pinboardurlupdate, pinboardwaybackmachine
Options:
-i, --include-unread Also save unread bookmarks.
-t, --token <token> Your Pinboard API token.
-a, --ask-for-token Ask for your Pinboard API token on start.
-s, --save-token Save Pinboard API token to Keychain and exit. Use with '--token' or '--ask-for-token' (macOS-only).
-h, --help Show this message.
" | sed -E 's/^ {4}//'
}
# Set options
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-i | --include-unread)
keep_unread='true'
;;
-t | --token)
token="${2}"
shift
;;
-a | --ask-for-token)
ask_for_token='true'
;;
-s | --save-token)
set_keychain_token='true'
;;
-*)
syntax_error "unrecognized option: ${1}"
;;
esac
shift
done
# Ask for api token, if option is set
if [[ -n "${ask_for_token}" ]]; then
echo 'Please insert your api token (will not be echoed)'
echo 'You can get it at https://pinboard.in/settings/password'
read -rsp '> ' token
echo
fi
# If no token given, look for it in the Keychain
[[ -z "${token}" ]] && token="$(security find-generic-password -s 'Pinboard API Token' -w<Paste>)"
# Exit if no token was set
if [[ -z "${token}" ]]; then
echo 'Cannot continue without a Pinboard token.'
usage
exit 1
fi
# Check if token is correct
if ! curl --fail --silent "https://api.pinboard.in/v1/user/api_token/?auth_token=${token}" > /dev/null; then
echo "The Pinboard API token appears to be incorrect. Alternatively, Pinboard’s servers might be down." >&2
exit 1
fi
# Save token to Keychain
if [[ -n "${set_keychain_token}" ]]; then
security add-generic-password -a "${token%:*}" -s 'Pinboard API Token' -w "${token}"
exit 0
fi
# Get all your pinboard links
if [[ "${keep_unread}" ]]; then
links="$(curl --silent "https://api.pinboard.in/v1/posts/all?auth_token=${token}" | grep '^<post' | cut -d '"' -f 2 | sed '1d')"
else
links="$(curl --silent "https://api.pinboard.in/v1/posts/all?auth_token=${token}" | grep '^<post' | grep --invert-match 'toread="yes"' | cut -d '"' -f 2 | sed '1d')"
fi
link_countdown="$(wc -l <<< "${links}" | tr -d ' ')"
# Check each link individually
for page in ${links} ; do
echo "[${link_countdown}] Archiving ${page}…"
((link_countdown--))
curl --location --silent --output /dev/null "https://web.archive.org/save/${page}"
done