Last active
April 26, 2018 18:44
-
-
Save phette23/b416553b9940ff5d8d6d79f63dbe0112 to your computer and use it in GitHub Desktop.
a utility script for CCA to check for duplicate syllabi in VAULT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// a utility script for CCA to check for duplicate syllabi in VAULT | |
let defaults = { | |
uuid: '9ec74523-e018-4e01-ab4e-be4dd06cdd68', | |
// 50 is max length | |
length: 50, | |
term: 'Spring 2018', | |
} | |
// .equellarc file with credentials for API use | |
let options = require('rc')('equella', defaults) | |
let request = require('request') | |
// construction API URL | |
let url = `https://vault.cca.edu/api/search/?q=&collections=${options.uuid}&where=%2Fxml%2Fmods%2FtitleInfo%2Ftitle%20LIKE%20%27${encodeURIComponent(options.term)}%25%27&length=${options.length}` | |
// each syllabus is an object like { name, uuid, ... } | |
let syllabi = [] | |
let dupes = [] | |
let counts = { | |
dupes: 0, | |
total: Infinity, | |
retrieved: 0 | |
} | |
request.get( | |
url, | |
{ | |
'headers': { 'X-Authorization': 'access_token=' + options.token }, | |
'json': true | |
}, | |
(err, response, data) => { | |
counts.total = data.available | |
counts.retrieved = options.length | |
syllabi.concat(data.results) | |
console.log(`${counts.total} total syllabi`) | |
while (counts.retrieved < counts.total) { | |
console.log(`Retrieved ${counts.retrieved} of ${counts.total} syllabi`) | |
counts.retrieved += options.length | |
request.get( | |
url + `&start=${counts.retrieved}`, | |
{ | |
'headers': { 'X-Authorization': 'access_token=' + options.token }, | |
'json': true | |
}, | |
(err, response, data) => { | |
syllabi.push(...data.results) | |
} | |
) | |
} | |
} | |
) | |
// recursive check for dupes fn | |
function check_for_dupes(arr) { | |
let list = arr.slice().sort((a, b) => { | |
if ([a.name, b.name].sort()[0] === a.name) { | |
return -1 | |
} else if ([a.name, b.name].sort()[0] === b.name) { | |
return 1 | |
} | |
}) | |
list.forEach((item, index) => { | |
let next = list[index + 1] | |
if (next && item.name === next.name) { | |
dupes.push([item, next]) | |
counts.dupes++ | |
} | |
}) | |
} | |
function main() { | |
check_for_dupes(syllabi) | |
// print results | |
console.log(`${counts.dupes} dupes out of ${counts.total} syllabi for ${options.term}`) | |
if (counts.dupes) { | |
console.log('List of possible dupes:\n') | |
for (let dupe of dupes) { | |
console.log(`${dupe[0].name}\thttps://vault.cca.edu/items/${dupe[0].uuid}/0/`) | |
console.log(`${dupe[1].name}\thttps://vault.cca.edu/items/${dupe[1].uuid}/0/\n`) | |
} | |
} | |
} | |
// lol async coding is hard | |
setTimeout(main, 4000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment