-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadCourses.js
More file actions
115 lines (107 loc) · 3.09 KB
/
loadCourses.js
File metadata and controls
115 lines (107 loc) · 3.09 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
const fs = require('fs');
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
const fn = process.argv[2];
const del = process.argv[3];
delFlag = (del && del === 'DELETE') ? true : false;
let endpoints = [
'departments',
'terms',
'courselistings',
'courses',
'instructors',
'reserves',
];
let cl = console.log;
const logSeen = {}
let lpath;
console.log = (msg) => {
if (lpath) {
if (!logSeen[lpath] && fs.existsSync(lpath)) fs.unlinkSync(lpath);
fs.writeFileSync(lpath, msg + '\n', { flag: 'a'});
logSeen[lpath] = 1;
} else {
cl(msg);
}
}
(async () => {
let added = {};
let errors = {};
let deleted = {};
try {
if (!fn) {
throw new Error('Usage: node loadCourses.js <file> [DELETE]');
}
const config = await getAuthToken(superagent);
let logDir = config.logpath;
if (logDir) lpath = logDir + '/loadCourses.log';
let collStr = fs.readFileSync(fn, 'utf8');
let coll = JSON.parse(collStr);
const base = `${config.okapi}/coursereserves`;
if (delFlag) {
endpoints = endpoints.reverse();
}
for (x = 0; x < endpoints.length; x++) {
let p = endpoints[x];
if (coll[p] && coll[p][0]) {
let endpoint = p;
let data = coll[p];
added[p] = 0;
deleted[p] = 0;
errors[p] = 0;
for (let d = 0; d < data.length; d++) {
if (endpoint === 'instructors' || endpoint === 'reserves') {
url = `${base}/courselistings/${data[d].courseListingId}/${endpoint}`;
} else {
url = `${base}/${endpoint}`;
url = url.toLocaleLowerCase();
}
try {
if (delFlag) {
url += `/${data[d].id}`;
console.log(`[${deleted[p]}] DELETE ${url}`);
await superagent
.delete(url)
.set('User-Agent', config.agent)
.timeout({ response: 5000 })
.set('accept', 'text/plain')
.set('x-okapi-token', config.token)
deleted[p]++;
} else {
console.log(`[${added[p]}] POST ${url}`);
let res = await superagent
.post(url)
.timeout({ response: 5000 })
.set('User-Agent', config.agent)
.set('accept', 'application/json', 'text/plain')
.set('x-okapi-token', config.token)
.set('content-type', 'application/json')
.send(data[d]);
added[p]++;
}
} catch (e) {
console.log(`${e}`);
errors[p]++;
}
}
}
}
} catch (e) {
console.error(e.message);
}
console.log('------ Added ---------');
for (let k in added) {
let l = k.padEnd(16, ' ')
console.log(`${l}${added[k]}`);
}
console.log('------ Deleted -------');
for (let k in deleted) {
let l = k.padEnd(16, ' ')
console.log(`${l}${deleted[k]}`);
}
console.log('------ Errors --------');
for (let k in errors) {
let l = k.padEnd(16, ' ')
console.log(`${l}${errors[k]}`);
}
})();