forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinboardbackup
executable file
·108 lines (92 loc) · 2.99 KB
/
pinboardbackup
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
#!/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:
-j, --json Export as JSON (default is XML).
-o, --output <file> File to export to (defaults to saving in your home).
-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
;;
-j | --json)
export_format='json'
;;
-o | --output)
output_file="${2}"
shift
;;
-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
# Defaults for unset variables
[[ -z "${export_format}" ]] && export_format='xml'
[[ -z "${output_file}" ]] && output_file="${HOME}/pinboardbackup.${export_format}"
# 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)"
# 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
# Backup
if [[ ! -f "${output_file}" ]]; then
mkdir -p "$(dirname "${output_file}")"
touch "${output_file}"
fi
last_update="$(curl --silent "https://api.pinboard.in/v1/posts/update?auth_token=${token}" | grep 'update time' | sed -E 's/.*"(.*)".*/\1/')"
if head -1 "${output_file}" | grep --quiet "${last_update}"; then
echo 'All bookmarks saved (no change since last backup).'
exit 0
fi
echo -e "Last saved on: ${last_update}\n" > "${output_file}"
curl --silent "https://api.pinboard.in/v1/posts/all?auth_token=${token}&format=${export_format}" >> "${output_file}"
echo 'All bookmarks saved.'