-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy paththost-uploader
executable file
·78 lines (63 loc) · 1.91 KB
/
thost-uploader
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
#!/bin/bash
set -euo pipefail
#
# Bash script showing how to use curl to save to a Tiddlyhost site.
#
# Example usage:
# ./thost-uploader yoursite newcontent.html [email protected] yourpassword
#
# To avoid including your password in scripts, you could do something like
# this for example:
# echo yourpassword > ~/.thostpass
# chmod 600 ~/.thostpass
#
# Then:
# ./thost-uploader yoursite newcontent.html [email protected] $(cat ~/.thostpass)
#
show_usage() {
echo Usage:
echo " $0 <sitename> <tiddlywikifile> <email> <password>"
exit 1
}
SITE=$1
FILE=$2
EMAIL=$3
PASSWD=$4
DOWNLOAD_DIR=${DOWNLOAD_DIR:-./downloads}
mkdir -p $DOWNLOAD_DIR
[[ -z "$SITE" ]] && show_usage
[[ -z "$FILE" ]] && show_usage
[[ -z "$EMAIL" ]] && show_usage
[[ -z "$PASSWD" ]] && show_usage
[[ ! -r "$FILE" ]] && echo "Can't read file $FILE!" && exit 1
TIDDLYHOST=tiddlyhost.com
SIGN_IN=https://$TIDDLYHOST/users/sign_in
COOKIES=$(mktemp)
trap "rm -f $COOKIES" EXIT
CURL="curl -s --cookie-jar $COOKIES --cookie $COOKIES"
# Read authenticity_token
TOKEN=$($CURL $SIGN_IN | grep '"csrf-token"' | cut -d'"' -f4)
# Log in
$CURL -X POST $SIGN_IN \
-d "authenticity_token=$TOKEN" \
-d "user[email]=$EMAIL" \
-d "user[password]=$PASSWD" \
-o /dev/null
# Check we logged in succesfully
OK=$($CURL -I "https://$TIDDLYHOST/sites" | head -1 | grep '200 OK')
[[ -z "$OK" ]] && echo "Login failed." && exit 1
TIMESTAMP=$(date +%Y%m%d%H%M%S)
# Download a copy just in case something goes wrong
$CURL "https://$SITE.$TIDDLYHOST/download" -o $DOWNLOAD_DIR/$SITE.$(date +%Y%m%d%H%M%S).backup.html
# Now upload the new TiddlyWiki file
OK=$($CURL -i -X PUT --data-binary "@$FILE" \
-H "Content-Type: text/html;charset=UTF-8" \
"https://$SITE.$TIDDLYHOST/" | grep '204 No Content')
# 204 means it worked
if [[ -z "$OK" ]]; then
echo "Upload failed." && exit 1
else
echo "Uploaded $FILE to https://$SITE.$TIDDLYHOST/"
fi
# Clean up
rm -f $COOKIES