-
Notifications
You must be signed in to change notification settings - Fork 2
/
dl-gdocs-drive.sh
executable file
·66 lines (53 loc) · 1.86 KB
/
dl-gdocs-drive.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
set -e
if [ $# -ne 5 ]; then
echo "Need 5 arguments"
exit 0
fi
PACKETS_DIR="$1" # packets/
GDOCS_FOLDER_ID="$2"
PACKET_FILENAME_TO_SLUG_START="$3"
PACKET_FILENAME_TO_SLUG_LENGTH="$4"
EXTENSION="$5"
# TODO figure out how to make packets folder not automatically appear in root dir of oligodendrocytes
if [ -z "$GDOCS_FOLDER_ID" ]; then
echo "Need Google Drive folder ID."
echo "(As of 2022-09-01, folder ID is needed instead of name.)"
exit 0
fi
DATE=`date "+%F"`
DOCS_DIR="$PACKETS_DIR"docs/ # packets/docs/
TEMP_DOCS_DIR="$DOCS_DIR"tmp/ # packets/docs/tmp/
DATE_DOCS_DIR="$DOCS_DIR"$DATE/ # packets/docs/yyyy-mm-dd/
mkdir -p $TEMP_DOCS_DIR
mkdir -p $DATE_DOCS_DIR
if [ 1 ]; then
echo "clearing old docs in $PACKETS_DIR, $TEMP_DOCS_DIR, $DATE_DOCS_DIR"
rm -rf $PACKETS_DIR*.$EXTENSION $TEMP_DOCS_DIR* $DATE_DOCS_DIR*.$EXTENSION
echo "fetching docs into $TEMP_DOCS_DIR"
drive pull -export "$EXTENSION" -exports-dir "$TEMP_DOCS_DIR" -explicitly-export -same-exports-dir -files -verbose -id "$GDOCS_FOLDER_ID"
fi
echo
echo "changing filenames and copying into $DATE_DOCS_DIR"
for DOC in $TEMP_DOCS_DIR*; do
OLDNAME=${DOC##*\/}
OLDNAME=${OLDNAME%.$EXTENSION}
# commented out 2020-03-05
#if [ "$PACKET_FILENAME_TO_SLUG_START" -eq "0" ]; then
# NEWNAME=$OLDNAME
#else
if [ "$PACKET_FILENAME_TO_SLUG_LENGTH" -eq "0" ]; then
NEWNAME=${OLDNAME:$PACKET_FILENAME_TO_SLUG_START}
else
NEWNAME=${OLDNAME:$PACKET_FILENAME_TO_SLUG_START:$PACKET_FILENAME_TO_SLUG_LENGTH}
fi
#fi
NEWNAME=${NEWNAME// /_}
if [ ! -z "$NEWNAME" ]; then
NEWPATH=$DATE_DOCS_DIR$NEWNAME.$EXTENSION
printf "copying %-42s → \"$NEWNAME\"\n" "\"$OLDNAME\""
cp "$DOC" "$NEWPATH" || true
fi
done
echo
echo "copying into $PACKETS_DIR"
cp $DATE_DOCS_DIR* $PACKETS_DIR