-
Notifications
You must be signed in to change notification settings - Fork 2
/
zotzen.js
709 lines (671 loc) · 21 KB
/
zotzen.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
const ArgumentParser = require('argparse').ArgumentParser;
const childProcess = require('child_process');
const fs = require('fs');
const opn = require('opn');
const path = require('path');
const prompt = require('prompt');
const getPrompt = require('util').promisify(prompt.get).bind(prompt);
const parser = new ArgumentParser({
version: '1.0.0',
addHelp: true,
description: 'ZotZen utility. Main modes are --new or provide a Zotero item.',
});
parser.addArgument('--new', {
action: 'storeTrue',
help: 'Create a new pair of Zotero/Zenodo entries.',
});
parser.addArgument('--title', {
help: 'Title of the new entries (for --new).',
});
parser.addArgument('--json', {
help: 'A Zotero json file to be used for the Zotero entry (for --new).',
});
parser.addArgument('--group', {
help: 'Group ID for which the new item Zotero is to be created (for --new).',
});
parser.addArgument('zot', {
help: 'Zotero id of the item group_id:item_key or item_key',
nargs: '*',
});
parser.addArgument('--show', {
action: 'storeTrue',
help: 'Show the zotero, zenodo item information (for both --new and --zot).',
});
parser.addArgument('--open', {
action: 'storeTrue',
help:
'Open the zotero and zenodo link after creation (for both --new and --zot).',
});
parser.addArgument('--getdoi', {
action: 'storeTrue',
help: 'Generate a DOI for an existing Zotero item.',
});
parser.addArgument('--template', {
help: 'Path of the template to be used for creating Zenodo record.',
});
parser.addArgument('--zen', {
help: 'Zenodo record id of the item to be linked.',
});
parser.addArgument('--sync', {
action: 'storeTrue',
help: 'Sync metadata from zotero to zenodo.',
});
parser.addArgument('--push', {
action: 'storeTrue',
help: 'Push Zotero attachments to Zenodo.',
});
parser.addArgument('--type', {
action: 'store',
help: 'Type of the attachments to be pushed.',
defaultValue: 'all',
});
parser.addArgument('--publish', {
action: 'storeTrue',
help: 'Publish zenodo record.',
});
parser.addArgument('--install', {
action: 'storeTrue',
help: 'Install the config for Zotero and Zenodo.',
});
parser.addArgument('--debug', {
action: 'storeTrue',
help: 'Enable debug logging',
});
parser.addArgument('--link', {
action: 'storeTrue',
help: 'Link the zotero item to the zenodo record in the DOI.',
});
const args = parser.parseArgs();
const zoteroPrefix = 'node bin/zotero-cli.js';
const zenodoPrefix = 'python zenodo-cli.py';
const zoteroSelectPrefix = 'zotero://select';
const zoteroApiPrefix = 'https://api.zotero.org';
const zoteroTmpFile = 'zotero-cli/tmp';
const zenodoTmpFile = 'zenodo-cli/tmp';
const zenodoCreateRecordTemplatePath = 'zenodo-cli/template.json';
function runCommandWithJsonFileInput(command, json, zotero = true) {
if (args.debug) {
console.log('DEBUG: runCommandWithJsonFileInput');
}
fs.writeFileSync(
zotero ? zoteroTmpFile : zenodoTmpFile,
JSON.stringify(json)
);
const response = runCommand(`${command} tmp`, zotero);
fs.unlinkSync(zotero ? zoteroTmpFile : zenodoTmpFile);
return response;
}
function runCommand(command, zotero = true) {
if (args.debug) {
console.log('DEBUG: runCommand; ' + command);
}
try {
return childProcess
.execSync(`${zotero ? zoteroPrefix : zenodoPrefix} ${command}`, {
cwd: `${zotero ? 'zotero' : 'zenodo'}-cli`,
stdio: [],
})
.toString();
} catch (ex) {
try {
return JSON.parse(ex.stderr.toString());
} catch (_) {}
throw new Error(`${zotero ? 'Zotero' : 'Zenodo'}: ${ex.output.toString()}`);
}
}
function parseFromZenodoResponse(content, key) {
if (args.debug) {
console.log('DEBUG: parseFromZenodoResponse');
}
return content
.substr(content.indexOf(`${key}:`))
.split('\n')[0]
.split(':')
.slice(1)
.join(':')
.trim();
}
function zoteroCreate(title, group, jsonFile = null) {
if (args.debug) {
console.log('DEBUG: zoteroCreate');
}
if (jsonFile) {
return JSON.parse(
runCommand(
`${group ? '--group-id ' + group : ''} create-item ${path.join(
__dirname,
jsonFile
)}`,
true
)
);
}
const zoteroCreateItemTemplate = runCommand(
'create-item --template report',
true
);
const templateJson = JSON.parse(zoteroCreateItemTemplate);
templateJson.title = title;
return JSON.parse(
runCommandWithJsonFileInput(
`${group ? '--group-id ' + group : ''} create-item`,
templateJson,
true
)
);
}
function zenodoCreate(title, creators, zoteroSelectLink, template) {
if (args.debug) {
console.log('DEBUG: zenodoCreate');
}
template = template || zenodoCreateRecordTemplatePath;
const zenodoTemplate = JSON.parse(fs.readFileSync(template).toString());
zenodoTemplate.related_identifiers[0].identifier = zoteroSelectLink;
if (!zenodoTemplate.title) zenodoTemplate.title = title;
if (!zenodoTemplate.description) zenodoTemplate.description = title;
if (creators) zenodoTemplate.creators = creators;
return runCommandWithJsonFileInput('create --show', zenodoTemplate, false);
}
function linkZotZen(zoteroKey, zenodoDoi, group, zoteroLink = null) {
if (args.debug) {
console.log('DEBUG: linkZotZen');
}
runCommandWithJsonFileInput(
`${group ? '--group-id ' + group : ''} update-item --key ${zoteroKey}`,
{
extra: `DOI: ${zenodoDoi}`,
}
);
if (zoteroLink) {
runCommand(`update ${zenodoDoi} --zotero-link ${zoteroLink}`, false);
}
}
function zotzenCreate(args) {
if (args.debug) {
console.log('DEBUG: zotzenCreate');
}
const zoteroRecord = zoteroCreate(args.title, args.group, args.json);
const zoteroSelectLink = zoteroRecord.successful[0].links.self.href.replace(
zoteroApiPrefix,
zoteroSelectPrefix
);
const zenodoRecord = zenodoCreate(
zoteroRecord.successful[0].data.title,
zoteroRecord.successful[0].data.creators,
zoteroSelectLink
);
const doi = parseFromZenodoResponse(zenodoRecord, 'DOI');
const zenodoDepositUrl = parseFromZenodoResponse(zenodoRecord, 'URL');
linkZotZen(zoteroRecord.successful[0].key, doi, args.group);
console.log('Item successfully created: ');
console.log(
`Zotero ID: ${zoteroRecord.successful[0].library.id}:${zoteroRecord.successful[0].key}`
);
console.log(`Zotero link: ${zoteroRecord.successful[0].links.self.href}`);
console.log(`Zotero select link: ${zoteroSelectLink}`);
console.log(
`Zenodo RecordId: ${parseFromZenodoResponse(zenodoRecord, 'RecordId')}`
);
console.log(`Zenodo DOI: ${doi}`);
console.log(`Zenodo deposit link: ${zenodoDepositUrl}`);
if (args.open) {
opn(zoteroSelectLink);
opn(zenodoDepositUrl);
}
}
function zoteroGet(groupId, userId, itemKey) {
if (args.debug) {
console.log('DEBUG: zoteroGet');
}
return JSON.parse(
runCommand(
`${groupId ? '--group-id ' + groupId : ''} ${
userId ? '--user-id ' + userId : ''
} item --key ${itemKey}`,
true
)
);
}
function zenodoGet(doi) {
if (args.debug) {
console.log('DEBUG: zenodoGet');
}
const zenodoResponse = runCommand(`get ${doi} --show`, false);
return {
title: parseFromZenodoResponse(zenodoResponse, 'Title'),
status: parseFromZenodoResponse(zenodoResponse, 'State'),
writable:
parseFromZenodoResponse(zenodoResponse, 'Published') == 'yes'
? 'not'
: '',
url: parseFromZenodoResponse(zenodoResponse, 'URL'),
doi: parseFromZenodoResponse(zenodoResponse, 'DOI'),
};
}
function zenodoGetRaw(doi) {
if (args.debug) {
console.log('DEBUG: zenodoGetRaw');
}
runCommand(`get ${doi}`, false);
const fileName = doi.split('.').pop();
return JSON.parse(fs.readFileSync(`zenodo-cli/${fileName}.json`).toString());
}
function getZoteroSelectlink(id, key, group = false) {
if (args.debug) {
console.log('DEBUG: getZoteroSelectlink');
}
return `zotero://select/${group ? 'groups' : 'users'}/${id}/items/${key}`;
}
function syncErrors(doi, zenodoRawItem, zoteroSelectLink) {
if (args.debug) {
console.log('DEBUG: syncErrors');
}
let error = false;
if (!doi) {
console.log(
'This item has no Zenodo DOI. You need to generate or link one first with --getdoi.'
);
error = true;
} else if (!zenodoRawItem) {
console.log(`Zenodo item with id ${doi} does not exist.`);
error = true;
} else if (
zenodoRawItem.related_identifiers &&
zenodoRawItem.related_identifiers.length >= 1 &&
zenodoRawItem.related_identifiers[0].identifier !== zoteroSelectLink
) {
console.log(zoteroSelectLink);
console.log(
`The Zenodo item exists, but is not linked. You need to link the items with --zen ${doi} first.`
);
error = true;
}
return error;
}
function pushAttachment(itemKey, key, fileName, doi, groupId, userId) {
if (args.debug) {
console.log('DEBUG: pushAttachment');
}
console.log(`Pushing from Zotero to Zenodo: ${fileName}`);
runCommand(
`${
groupId ? '--group-id ' + groupId : ''
} attachment --key ${key} --save "../${fileName}"`
);
// TODO: What is the above command fails?
// TODO: Also, I've inserted "..." in case the filename contains spaces. However, really the filename should be made shell-proof.
// In perl, you would say:
// use String::ShellQuote; $safefilename = shell_quote($filename);
// There's no built-in for escaping. We can only escape special characters. We can do that if needed.
// All the command failures will throw an exception which will be caught on the top-level and a message will be printed.
const pushResult = runCommand(`upload ${doi} "../${fileName}"`, false);
if (pushResult.status === 403) {
console.log(pushResult.message);
console.log('Creating new version.');
const newVersionResponse = runCommand(`newversion ${doi}`, false);
doi = doi.replace(
/zenodo.*/,
`zenodo.${
parseFromZenodoResponse(newVersionResponse, 'latest_draft')
.split('/')
.slice(-1)[0]
}`
);
linkZotZen(
itemKey,
doi,
groupId,
getZoteroSelectlink(userId || groupId, itemKey, !!groupId)
);
runCommand(`upload ${doi} "../${fileName}"`, false);
}
fs.unlinkSync(fileName);
// TODO: How does the user know this was successful?
console.log('Upload successfull.'); //This shoukd be good enough. User can always use --show or --open to see/open the record.
return doi;
}
function linked(zenodoItem, zoteroLink) {
if (args.debug) {
console.log('DEBUG: linked');
}
return (
zenodoItem.related_identifiers &&
zenodoItem.related_identifiers.length >= 1 &&
zenodoItem.related_identifiers[0].identifier === zoteroLink
);
}
async function zotzenGet(args) {
if (args.debug) {
console.log('DEBUG: zotzenGet');
}
await Promise.all(
args.zot.map(async (zot) => {
let groupId = null;
let itemKey = null;
let userId = null;
if (zot.includes('zotero')) {
const selectLink = zot.split('/');
if (selectLink.length < 7) {
throw new Error('Invalid zotero select link specified');
}
if (selectLink[3] == 'users') {
userId = selectLink[4];
} else {
groupId = selectLink[4];
}
itemKey = selectLink[6];
} else if (zot.includes(':')) {
groupId = zot.split(':')[0];
itemKey = zot.split(':')[1];
} else {
itemKey = zot;
}
const zoteroItem = zoteroGet(groupId, userId, itemKey);
let doi = null;
if (zoteroItem.data.DOI) {
doi = zoteroItem.data.DOI;
} else {
const doiRegex = new RegExp(/10\.5281\/zenodo\.[0-9]+/);
if (zoteroItem.data.extra) {
const match = zoteroItem.data.extra.match(doiRegex);
if (match) {
doi = match[0];
}
}
}
const zoteroSelectLink = getZoteroSelectlink(
groupId || userId,
itemKey,
!!groupId
);
let zenodoRawItem = doi && zenodoGetRaw(doi);
if (args.getdoi) {
if (args.debug) {
console.log('DEBUG: zotzenGet, getdoi');
}
if (doi) {
console.log(`Item has DOI already: ${doi}`);
console.log(
`Linked zotero record: `,
zenodoRawItem.related_identifiers[0].identifier
);
} else {
const zenodoRecord = zenodoCreate(
zoteroItem.data.title,
zoteroItem.data.creators &&
zoteroItem.data.creators.map((c) => {
return {
name: `${c.name ? c.name : c.lastName + ', ' + c.firstName}`,
};
}),
zoteroSelectLink,
args.template
);
doi = parseFromZenodoResponse(zenodoRecord, 'DOI');
linkZotZen(itemKey, doi, groupId);
console.log(`DOI allocated: ${doi}`);
}
} else if (args.zen) {
if (args.debug) {
console.log('DEBUG: zotzenGet, zen');
}
try {
zenodoZenItem = zenodoGetRaw(args.zen);
} catch (ex) {
if (args.debug) {
console.log('DEBUG: zotzenGet, exception zenodoGetRaw');
}
}
if (doi) {
console.log(`Item has DOI already: ${doi}`);
console.log(
`Linked zotero record: `,
zenodoRawItem.related_identifiers[0].identifier
);
} else if (!zenodoZenItem) {
console.log(`Zenodo item with id ${args.zen} does not exist.`);
} else if (!linked(zenodoZenItem, zoteroSelectLink)) {
console.log(
'Zenodo item is linked to a different Zotero item: ',
zenodoZenItem.related_identifiers[0].identifier
);
} else {
const zenodoLinked = zenodoGet(args.zen);
doi = zenodoLinked.doi;
linkZotZen(itemKey, doi, groupId, zoteroSelectLink);
console.log(`DOI allocated: ${doi}`);
}
} else if (args.sync || args.push || args.publish || args.link) {
if (!doi) {
console.log('No doi present in the zotero item.');
} else if (linked(zenodoRawItem, zoteroSelectLink)) {
console.log('Item is already linked.');
} else if (
zenodoRawItem.related_identifiers &&
zenodoRawItem.related_identifiers.length >= 1 &&
args.link
) {
linkZotZen(itemKey, doi, groupId, zoteroSelectLink);
} else {
console.log(
`Found doi: ${doi} not linked to zotero. Zotero: ${zoteroItem.data.title} Zenodo: ${zenodoRawItem.title} `
);
const result = await getPrompt({
properties: {
Link: {
message: `Found doi: ${doi} not linked to zotero. Proceed? (y/N)`,
default: 'y',
},
},
});
if (result && (result.Link == 'y' || result.Link == 'Y')) {
console.log('Proceeding to link...');
linkZotZen(itemKey, doi, groupId, zoteroSelectLink);
}
}
}
let zenodoItem = null;
if (doi) {
zenodoItem = zenodoGet(doi);
zenodoRawItem = zenodoGetRaw(doi);
}
if (!zoteroItem.data.title) {
console.log('Zotero item does not have title. Exiting...');
return;
}
// This is useful is you just want the bare abstract.
var abstract = '';
if (
!zoteroItem.data.abstractNote ||
zoteroItem.data.abstractNote.length < 3
) {
//console.log('Zotero item abstract is less than 3 characters. Exiting...');
//return;
console.log(
'Zotero item abstract is less than 3 characters - using "No description available."'
);
abstract = 'No description available.';
} else {
abstract = zoteroItem.data.abstractNote;
}
if (!zoteroItem.data.creators || !zoteroItem.data.creators.length) {
console.log('Zotero item does not have creators. Exiting...');
return;
}
abstract += zoteroItem.data.url
? `\n\nAlso see: ${zoteroItem.data.url}`
: '';
if (args.sync) {
if (!syncErrors(doi, zenodoRawItem, zoteroSelectLink)) {
let updateDoc = {
title: zoteroItem.data.title,
description: abstract,
creators: zoteroItem.data.creators.map((c) => {
return {
name: `${c.name ? c.name : c.lastName + ', ' + c.firstName}`,
};
}),
};
if (zoteroItem.data.date) {
updateDoc.publication_date = zoteroItem.data.date;
}
runCommandWithJsonFileInput(
`update ${doi} --json `,
updateDoc,
false
);
}
}
if (args.push) {
if (!syncErrors(doi, zenodoRawItem, zoteroSelectLink)) {
const children = JSON.parse(
runCommand(
`${
groupId ? '--group-id ' + groupId : ''
} get /items/${itemKey}/children`,
true
)
);
let attachments = children.filter(
(c) =>
c.data.itemType === 'attachment' &&
c.data.linkMode === 'imported_file'
);
const attachmentType = args.type.toLowerCase();
if (attachmentType !== 'all') {
attachments = attachments.filter((a) =>
a.data.filename.endsWith(attachmentType)
);
}
if (!attachments.length) {
console.log('No attachments found.');
} else {
attachments.forEach((attachment) => {
doi = pushAttachment(
itemKey,
attachment.data.key,
attachment.data.filename,
doi,
groupId,
userId
);
});
}
}
}
if (args.publish && doi) {
runCommand(`get ${doi} --publish`, false);
}
if (args.show) {
console.log('Zotero:');
console.log(`- Item key: ${itemKey}`);
zoteroItem.data.creators.forEach((c) => {
console.log(
'-',
`${c.creatorType}:`,
c.name || c.firstName + ' ' + c.lastName
);
});
console.log(`- Date: ${zoteroItem.data.date}`);
console.log(`- Title: ${zoteroItem.data.title}`);
console.log(`- DOI: ${doi}`);
console.log('');
if (doi) {
zenodoRawItem = zenodoGetRaw(doi);
zenodoItem = zenodoGet(doi);
console.log('Zenodo:');
console.log('* Item available.');
console.log(`* Item status: ${zenodoItem.status}`);
console.log(`* Item is ${zenodoItem.writable} writable`);
console.log(`- Title: ${zenodoRawItem.title}`);
zenodoRawItem.creators &&
zenodoRawItem.creators.forEach((c) => {
console.log(`- Author: ${c.name}`);
});
console.log(`- Publication date: ${zenodoRawItem.publication_date}`);
console.log('');
}
}
if (args.open) {
opn(zoteroSelectLink);
if (zenodoItem) {
opn(zenodoItem.url);
}
}
})
);
}
try {
if (args.new) {
if (args.debug) {
console.log('DEBUG: args.new');
}
zotzenCreate(args);
} else if (args.install) {
if (args.debug) {
console.log('DEBUG: args.install');
}
const schema = {
properties: {
'Zenodo API Key': {
message: 'Please enter you Zenodo API Key. (Enter to ignore)',
},
'Zotero API Key': {
message: 'Please enter your Zotero API Key. (Enter to ignore)',
},
'Zotero User ID': {
message: 'Please enter your Zotero User ID. (Enter to ignore)',
},
'Zotero Group ID': {
message: 'Please enter your Zotero Group ID. (Enter to ignore)',
},
},
};
prompt.start();
prompt.get(schema, (err, result) => {
if (err) {
console.err('Invalid input received');
} else {
const zenKey = result['Zenodo API Key'];
if (zenKey) {
fs.writeFileSync(
'zenodo-cli/config.json',
JSON.stringify({
accessToken: zenKey,
})
);
console.log(
'Zenodo config wrote successfully to zenodo-cli/config.json.'
);
}
const zotKey = result['Zotero API Key'];
const zotUid = result['Zotero User ID'];
const zotGid = result['Zotero Group ID'];
if (zotKey || zotUid || zotGid) {
fs.writeFileSync(
'zotero-cli/zotero-cli.toml',
`${zotKey ? 'api-key="' + zotKey + '"\n' : ''}` +
`${zotUid ? 'user-id="' + zotUid + '"\n' : ''}` +
`${zotGid ? 'group-id="' + zotGid + '"\n' : ''}`
);
console.log(
'Zotero config wrote successfully to zotero-cli/zotero-cli.toml'
);
}
}
});
} else {
zotzenGet(args).catch((ex) => {
if (args.debug) {
console.log(ex);
}
});
}
} catch (ex) {
if (args.debug) {
console.log('DEBUG: ERROR');
}
if (args.debug) {
console.log(ex);
}
}