-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zenodo.js
executable file
·86 lines (77 loc) · 2.87 KB
/
Zenodo.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
const ZENODO_API_URL = 'https://zenodo.org/api';
function createZenodoDeposition_() {
var props = PropertiesService.getUserProperties();
var enableZenodo = props.getProperty('enableZenodo');
var token = props.getProperty('zenodoToken');
if (enableZenodo === 'true' && token) {
var docProps = PropertiesService.getDocumentProperties();
var zenodoId = docProps.getProperty('zenodoId');
var eml = JSON.parse(docProps.getProperty('eml'));
var data = {
metadata: {
upload_type: 'dataset',
publication_date: (new Date()).toISOString(),
title: eml['title'],
creators: eml['creators'].map(function(creator) { return {
name: creator['surname'] + ", " + creator['givenName']
}}),
description: eml['abstract'],
access_right: 'open',
license: 'cc-zero',
prereserve_doi: true
}
};
var url = ZENODO_API_URL + "/deposit/depositions";
if (zenodoId) {
url += "/" + zenodoId;
}
var options = {
'method' : zenodoId ? 'put' : 'post',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + token
},
payload: JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 201 || response.getResponseCode() === 200) {
return JSON.parse(response.getContentText());
}
return null;
}
return false;
}
function uploadToZenodo_(result, file) {
var props = PropertiesService.getUserProperties();
var enableZenodo = props.getProperty('enableZenodo');
var token = props.getProperty('zenodoToken');
if (enableZenodo === 'true' && token) {
var formData = {
name: file.getName(),
file: DriveApp.getFileById(file.getId()).getBlob()
};
var options = {
method: 'post',
//'contentType': 'multipart/form-data',
muteHttpExceptions : true,
headers: {
'Authorization': 'Bearer ' + token
},
payload : formData
};
var response = UrlFetchApp.fetch(ZENODO_API_URL + "/deposit/depositions/" + result.id + "/files", options);
if (response.getResponseCode() === 201) {
var published = UrlFetchApp.fetch(result.links.publish, {
method: 'post',
headers: {
'Authorization': 'Bearer ' + token
},
});
if (published.getResponseCode() !== 202) {
Logger.log('DwC-Archive not published to Zenodo');
}
return JSON.parse(published.getContentText());
}
}
return null;
}