-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverleaf-upload.sh
executable file
·67 lines (58 loc) · 2.39 KB
/
overleaf-upload.sh
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
#!/bin/sh
die() { EXITCODE=$1; shift; printf '%s\n' "$*"; exit $EXITCODE; }
# Source the passed file.
[ -e "$1" ] && . "$1"
# Perform sanity checks
[ -z "$TITLE" ] && die 1 Undefined / empty TITLE
[ -z "$AUTHOR" ] && die 2 Undefined / empty AUTHOR
[ -z "$DESCRIPTION" ] && die 3 Undefined / empty DESCRIPTION
[ -z "$LICENSE" ] && die 4 Undefined / empty LICENSE
[ -z "$SHOW_SOURCE" ] && die 5 Undefined / empty SHOW_SOURCE
[ -z "$COOKIE_JAR" ] && die 6 Undefined / empty COOKIE_JAR
[ -e "$COOKIE_JAR" ] || die 7 $COOKIE_JAR does not exist
[ -z "$DOCUMENT_ID" ] && die 8 Undefined / empty DOCUMENT_ID
curl_() {
curl -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0' -s --retry 5 --location -b "$COOKIE_JAR" "$@"
}
RESPONSE=`mktemp`
trap 'rm $RESPONSE' EXIT
echo 'Retrieving CSRF token' >&2
curl_ https://www.overleaf.com/project/$DOCUMENT_ID >$RESPONSE
if grep --silent csrfToken <$RESPONSE
then
CSRF_TOKEN="$(sed -nr '/window\.csrfToken/s/.*window\.csrfToken = "([^"]*)".*/\1/p' <$RESPONSE | head -n 1)"
else
die 9 Cannot retrieve CSRF token from the project page: "`cat $RESPONSE`"
fi
echo 'Publishing document' >&2
curl_ https://www.overleaf.com/project/$DOCUMENT_ID/export/96 \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H 'X-CSRF-Token: '"$CSRF_TOKEN" \
-H 'X-Requested-With: XMLHttpRequest' \
--data-urlencode title="$TITLE" \
--data-urlencode author="$AUTHOR" \
--data-urlencode description="$DESCRIPTION" \
--data-urlencode license="$LICENSE" \
--data-urlencode showSource="$SHOW_SOURCE" \
>$RESPONSE
if ! grep --silent 'export_v1_id' <$RESPONSE
then
die 10 Publishing failed '(post)': "`cat $RESPONSE`"
else
EXPORT_ID="$(sed -r 's/.*"export_v1_id":(.*)[},].*/\1/' <$RESPONSE)"
fi
echo '"status_summary":"pending","status_detail":"Starting up"' >$RESPONSE
while grep --silent -F '"status_summary":"pending"' <$RESPONSE
do
STATUS_SUMMARY="$(sed -r 's/.*"status_summary":"([^"]*)".*/\1/' <$RESPONSE)"
STATUS_DETAIL="$(sed -r 's/.*"status_detail":"([^"]*)".*/\1/' <$RESPONSE)"
printf 'Waiting for document to be published (%s: %s)\n' "$STATUS_SUMMARY" "$STATUS_DETAIL" >&2
curl_ https://www.overleaf.com/project/5c381f90819e564c3d6152cc/export/$EXPORT_ID \
>$RESPONSE
sleep 5s
done
if ! grep --silent -F '"status_summary":"succeeded"' <$RESPONSE
then
die 11 Publishing failed '(status)': "`cat $RESPONSE`"
fi
echo Done! >&2