-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_subcollections.js
More file actions
138 lines (111 loc) · 3.51 KB
/
move_subcollections.js
File metadata and controls
138 lines (111 loc) · 3.51 KB
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
/*
Testing record creation.
*/
const Zotero = require('zotero-lib');
const fs = require('fs');
const zotero = new Zotero({ 'group-id': 2405685 });
const fetch = require('node-fetch'); // Add this at the top of your file if using Node.js
function mapCollectionData(collection) {
return {
name: collection.data.name,
key: collection.data.key,
version: collection.data.version
};
}
function mapItemData(item) {
return {
key: item.data.key,
version: item.data.version
};
}
async function moveSubcollection(subcollection, extractedKey) {
console.log('Moving subcollection:', subcollection.name);
const groupId = "2405685";
// Prepare the update data
try {
// Update the subcollection
const result = await patch(groupId, subcollection.key, extractedKey, subcollection.version);
console.log('result=' + JSON.stringify(result, null, 2));
console.log(`Moved subcollection ${subcollection.name} to new parent ${extractedKey}`);
// process.exit(0);
return { success: true };
} catch (error) {
console.error(`Failed to move subcollection ${subcollection.name}:`, error);
}
}
async function patch(groupId, collectionKey, newParent, lastModifiedVersion) {
const updateData = {
parentCollection: newParent,
version: lastModifiedVersion
};
const apiKey = "Qf9wIhYEWuqpfKqqOWLxUxVT";
// Step 2: Include the version in the PATCH request
try {
const patchResponse = await fetch(`https://api.zotero.org/groups/${groupId}/collections/${collectionKey}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Zotero-API-Version': '3',
'Zotero-API-Key': apiKey
// 'If-Unmodified-Since-Version': lastModifiedVersion, // Adding the precondition header
},
body: JSON.stringify(updateData),
});
if (!patchResponse.ok) {
throw new Error(`Request failed with status ${patchResponse.status}: ${patchResponse.statusText}`);
return { error: "Error" };
}
// return await patchResponse.json();
return { success: true };
} catch (error) {
console.error('Error:', error);
return { error: error.message };
}
};
async function move(c) {
// console.log('c=' + JSON.stringify(c, null, 2));
const extractedKey = c.name.split('/').pop();
const result = await zotero.collections({
key: c.key
});
console.log('Extracted key:', extractedKey);
// console.log('result=' + JSON.stringify(result, null, 2));
const subcollections = result.map(mapCollectionData);
for (const subcollection of subcollections) {
await moveSubcollection(subcollection, extractedKey)
// process.exit(0);
}
// the collections in r now need to be moved to extractedKey
// process.exit(0);
return { success: true };
}
async function moveItems(c) {
const result = await zotero.items({
collection: c.key,
top: true
});
console.log(c.name);
// console.log('result=' + JSON.stringify(result.map(mapItemData), null, 2));
const extractedKey = c.name.split('/').pop();
console.log('Extracted key:', extractedKey);
for (const item of result.map(mapItemData)) {
await zotero.item({
key: item.key,
addToCollection: extractedKey
})
}
}
async function main() {
const result = await zotero.collections({
key: 'MP53N7NC',
});
const r = result.map(mapCollectionData);
// console.log('r=' + JSON.stringify(r), null, 2));
for (const c of r) {
if (c.name.includes('zotero://')) {
await move(c);
await moveItems(c);
}
}
};
main();