Last active
September 6, 2020 23:18
-
-
Save BowenYin/da3c858b9ec80873283f7017b72c26c2 to your computer and use it in GitHub Desktop.
Schoology course enrollment lookup script
This file contains hidden or 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
// Get your API credentials at https://app.schoology.com/api and insert them below. | |
// Go to https://api.schoology.com/404 to avoid CORS problems. | |
// Paste the script in console, wait for "Ready," then call the function searchSection(sectionId). | |
const CONSUMER_KEY = ">>>YOUR_API_KEY<<<"; | |
const CONSUMER_SECRET = ">>>YOUR_API_SECRET<<<"; | |
var users = [], userCourses = [], matches = []; | |
async function fetchUsers() { | |
users = []; | |
let numUsers = 1000; | |
for (let i = 0; i < numUsers; i += 200) { | |
let reqUrl = `https://api.schoology.com/v1/users?start=${i}&limit=200`; | |
let nonce = Math.floor(Math.random()*1000000000); | |
let timestamp = Math.floor(new Date().getTime()/1000); | |
let response = await fetch(reqUrl, { | |
"method": "GET", | |
"headers": { | |
"authorization": 'OAuth oauth_consumer_key="'+CONSUMER_KEY+'", oauth_nonce="'+ nonce + '", oauth_signature="' + oauthSignature.generate("GET", reqUrl, { | |
oauth_consumer_key: CONSUMER_KEY, | |
oauth_nonce: nonce, | |
oauth_timestamp: timestamp, | |
oauth_signature_method: "HMAC-SHA1", | |
oauth_version: "1.0", | |
start: i, | |
limit: 200, | |
}, CONSUMER_SECRET) + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + timestamp + '", oauth_version="1.0"' | |
} | |
}); | |
if (!response.ok) return console.error(await response.text()); | |
const data = await response.json(); | |
numUsers = data.total; | |
users = users.concat(data.user); | |
} | |
console.log("Ready."); | |
} | |
async function searchSection(id, requestInterval=105, minCacheRatio=0.99) { | |
matches = []; | |
if (userCourses.length/users.length < minCacheRatio) { | |
let errors = [], completed = 0; | |
userCourses = []; | |
users.forEach(async (user, index) => { | |
setTimeout(async () => { | |
let reqUrl = `https://api.schoology.com/v1/users/${user.id}/sections?include_past=1`; | |
let nonce = Math.floor(Math.random()*1000000000); | |
let timestamp = Math.floor(new Date().getTime()/1000); | |
let response = await fetch(reqUrl, { | |
"method": "GET", | |
"headers": { | |
"authorization": 'OAuth oauth_consumer_key="'+CONSUMER_KEY+'", oauth_nonce="'+ nonce + '", oauth_signature="' + oauthSignature.generate("GET", reqUrl, { | |
oauth_consumer_key: CONSUMER_KEY, | |
oauth_nonce: nonce, | |
oauth_timestamp: timestamp, | |
oauth_signature_method: "HMAC-SHA1", | |
oauth_version: "1.0", | |
include_past: 1, | |
}, CONSUMER_SECRET) + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + timestamp + '", oauth_version="1.0"' | |
} | |
}); | |
if (!response.ok) return errors.push({error: await response.text(), status: response.status, user, index}); | |
const sections = await response.json(); | |
userCourses.push({user, sections: sections.section}); | |
for (const section of sections.section) | |
if (section.id == id) | |
matches.push({ | |
id: user.id, | |
name: user.name_display || user.name_first+" "+user.name_last, | |
user}); | |
completed++; | |
console.clear(); | |
console.log(completed+"/"+users.length); | |
console.log(errors.length+" failed."); | |
if (completed == users.length-errors.length) { | |
if (errors.length != 0) console.error(errors); | |
console.log("Done."); | |
console.log(matches); | |
} | |
}, index*requestInterval); | |
}); | |
} else { | |
for (const item of userCourses) | |
for (const section of item.sections) | |
if (section.id == id) | |
matches.push({ | |
id: item.user.id, | |
name: item.user.name_display || item.user.name_first+" "+item.user.name_last, | |
user: item.user}); | |
console.log("Done."); | |
console.log(matches); | |
} | |
} | |
var script = document.createElement("script"); | |
script.onload = () => fetchUsers(); | |
script.src = "https://cdn.jsdelivr.net/npm/oauth-signature/dist/oauth-signature.min.js"; | |
document.head.appendChild(script); | |
console.log("Loading..."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment