-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (165 loc) · 4.53 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(express.static('public'));
const ZENODO_URL = 'https://zenodo.org/api/deposit/depositions';
const GITHUB_URL = 'https://api.github.com';
app.post('/getDoi', (req, response) => {
console.log('Request /getDoi received');
response.setHeader('Content-Type', 'application/json');
const data = req.body;
if (!data.token || !data.token.trim()) {
response.send(JSON.stringify({
'status': 0,
'message': 'Invalid token!'
}));
return;
}
fetch(ZENODO_URL, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${data.token}`
},
body: JSON.stringify({})
})
.then(res => res.json())
.then(res => {
console.log(res);
response.send(JSON.stringify({
'status': 1,
'message': res.metadata.prereserve_doi.doi
}))
})
.catch(err => {
console.log("Error occurred!", err);
response.send(JSON.stringify({
'status': 0,
'message': 'Error!'
}))
})
})
app.post('/generateCff', (req, response) => {
console.log('Request /generateCff received');
response.setHeader('Content-Type', 'application/json');
const data = req.body;
function getDate () {
let date = new Date();
const offset = date.getTimezoneOffset();
date = new Date(date.getTime() + (offset*60*1000));
return date.toISOString().split('T')[0];
}
function parseGithubUrl (url) {
url = url.toLowerCase();
url = url.slice(url.indexOf('github'));
if (url[url.length-1] == '/')
url = url.slice(0, -1);
url = url.split('/');
return {
owner: url[1],
repo: url[2]
}
}
if (!data.doi || !data.doi.trim()) {
response.send(JSON.stringify({
'status': 0,
'message': 'Invalid doi!'
}));
return;
}
if (!data.githubUrl || !data.githubUrl.trim()) {
response.send(JSON.stringify({
'status': 0,
'message': 'Invalid GitHub URL!'
}));
return;
}
if (!data.version || !data.version.trim()) {
response.send(JSON.stringify({
'status': 0,
'message': 'Invalid version!'
}));
return;
}
let todaysDate = getDate();
let cff = {
"cff-version": "1.0.0",
"doi": data.doi,
"date-released": todaysDate,
"version": data.version
}
githubUrl = parseGithubUrl(data.githubUrl);
fetch(GITHUB_URL+`/repos/${githubUrl.owner}/${githubUrl.repo}`, {
method: 'GET'
})
.then(res => {
if (res.status == 200)
return res.json();
else {
response.send(JSON.stringify({
status: 0,
message: 'Error getting details from GitHub'
}))
}
})
.then(res => {
cff.title = res.name;
cff.message = res.description;
let ymlText = json2yaml(cff);
const randomFolder = `${cff.title.trim()}${Math.floor(Math.random()*100000)}`;
const dataDirName = path.resolve(__dirname, 'public/data');
if (!fs.existsSync(dataDirName)){
fs.mkdirSync(dataDirName);
}
const dirName = path.resolve(dataDirName, randomFolder);
if (!fs.existsSync(dirName)){
fs.mkdirSync(dirName);
}
const yamlFileUrl = path.resolve(dirName, 'CITATION.yml');
fs.writeFileSync(yamlFileUrl, ymlText);
const jsonText = cffToZenodoJson(cff);
const jsonFileUrl = path.resolve(dirName, 'zenodo.json');
fs.writeFileSync(jsonFileUrl, JSON.stringify(jsonText, null, 2));
response.send(JSON.stringify({
status: 1,
message: `/data/${randomFolder}`
}))
})
.catch(err => {
console.log(err);
response.send(JSON.stringify({
status: 0,
message: 'Error getting details from GitHub'
}))
})
})
function json2yaml (json) {
let yaml = "";
for (let key in json) {
if (typeof json[key] == "string")
yaml += `${key}: "${json[key]}"\n`
else {
yaml += `${key}:\n`;
let obj = json[key];
for (let key in obj) {
// if (typeof obj[key] == "string")
yaml += `\t- ${key}: "${obj[key]}"\n`;
}
}
}
return yaml;
}
function cffToZenodoJson (cff) {
let obj = {};
obj.version = cff.version;
obj.title = cff.title;
obj.doi = cff.doi;
obj.created = cff["date-released"];
return obj;
}
app.listen(process.env.port || 8000);
console.log("App hosted on localhost:8000");