generated from numpex/deliverable.template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzotero.sh
144 lines (114 loc) · 4.99 KB
/
zotero.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
GROUP_ID="5582837"
API_KEY=${ZOTERO_API_KEY}
declare -A existing_collections
declare -A collection_keys
fetch_existing_collections() {
local start=0
local limit=100
local total=1
# Get total number of collections
total=$(curl -s -I -X GET "https://api.zotero.org/groups/$GROUP_ID/collections" \
-H "Zotero-API-Version: 3" \
-H "Authorization: Bearer $API_KEY" | grep -i 'Total-Results' | awk '{print $2}' | tr -d '\r')
while [[ $start -lt $total ]]; do
response=$(curl -s -X GET "https://api.zotero.org/groups/$GROUP_ID/collections?limit=$limit&start=$start" \
-H "Zotero-API-Version: 3" \
-H "Authorization: Bearer $API_KEY")
# Parse the response and store collections in an associative array
while read -r collection; do
key=$(echo "$collection" | jq -r '.key')
name=$(echo "$collection" | jq -r '.data.name')
parent=$(echo "$collection" | jq -r '.data.parentCollection')
existing_collections["$name|$parent"]="$key"
echo "Found collection '$name' in '$parent' with key $key."
done < <(echo "$response" | jq -c '.[]')
start=$((start + limit))
done
}
fetch_existing_collections existing_collections
echo "Existing collections: ${!existing_collections[@]}"
# Create top-level collections
collections=("Our Publications" "External Publications")
top_level_keys=()
for collection in "${collections[@]}"; do
# Check if collection exists
parent="false"
collection_key="${existing_collections["$collection|$parent"]}"
echo "Collection key: $collection_key"
if [[ -n "$collection_key" ]]; then
echo "Collection '$collection' already exists with key $collection_key."
else
# Create collection
response=$(curl -s -X POST "https://api.zotero.org/groups/$GROUP_ID/collections" \
-H "Zotero-API-Version: 3" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "[{\"name\": \"$collection\"}]")
echo "Response for creating '$collection': $response"
# Extract the key
collection_key=$(echo "$response" | jq -r '.success."0"')
if [[ -z "$collection_key" || "$collection_key" == "null" ]]; then
echo "Failed to create collection '$collection'. Exiting."
exit 1
fi
# Update the existing collections array
existing_collections["$collection|$parent"]="$collection_key"
echo "Created collection '$collection' with key $collection_key."
fi
# Store the key for use in sub-collections
collection_keys["$collection"]="$collection_key"
done
echo "Top-level collection keys: ${collection_keys[@]}"
# Create WP sub-collections under each top-level collection
wps=("WP1" "WP2" "WP3" "WP4" "WP5" "WP6" "WP7")
sub_collections=("Articles" "Software" "Technical Notes" "Deliverables")
for top_collection in "${collections[@]}"; do
parent_key="${collection_keys["$top_collection"]}"
for wp in "${wps[@]}"; do
# Check if WP collection exists
wp_key="${existing_collections["$wp|$parent_key"]}"
if [[ -n "$wp_key" ]]; then
echo "WP '$wp' under '$top_collection' already exists with key $wp_key."
else
# Create WP collection
response=$(curl -s -X POST "https://api.zotero.org/groups/$GROUP_ID/collections" \
-H "Zotero-API-Version: 3" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "[{\"name\": \"$wp\", \"parentCollection\": \"$parent_key\"}]")
echo "Response for creating WP '$wp' under '$top_collection': $response"
wp_key=$(echo "$response" | jq -r '.success."0"')
if [[ -z "$wp_key" || "$wp_key" == "null" ]]; then
echo "Failed to create WP '$wp' under '$top_collection'. Exiting."
exit 1
fi
# Update existing collections
existing_collections["$wp|$parent_key"]="$wp_key"
echo "Created WP '$wp' under '$top_collection' with key $wp_key."
fi
# Create sub-collections under WP
for sub_collection in "${sub_collections[@]}"; do
sub_key="${existing_collections["$sub_collection|$wp_key"]}"
if [[ -n "$sub_key" ]]; then
echo "Sub-collection '$sub_collection' under '$wp' already exists with key $sub_key."
else
# Create sub-collection
response=$(curl -s -X POST "https://api.zotero.org/groups/$GROUP_ID/collections" \
-H "Zotero-API-Version: 3" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "[{\"name\": \"$sub_collection\", \"parentCollection\": \"$wp_key\"}]")
echo "Response for creating '$sub_collection' under '$wp': $response"
sub_key=$(echo "$response" | jq -r '.success."0"')
if [[ -z "$sub_key" || "$sub_key" == "null" ]]; then
echo "Failed to create sub-collection '$sub_collection' under '$wp'. Exiting."
exit 1
fi
# Update existing collections
existing_collections["$sub_collection|$wp_key"]="$sub_key"
echo "Created sub-collection '$sub_collection' under '$wp' with key $sub_key."
fi
done
done
done