Created
September 30, 2020 06:03
-
-
Save chiragzq/3cf217f135000c40fe8e6f1fe1e1495e to your computer and use it in GitHub Desktop.
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 = "9894f356bc999e01b0e346aa94e55ec205dda14ec"; | |
const CONSUMER_SECRET = "5f4921f7cf268127dd2a61f137787e14"; | |
let output; | |
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."); | |
output = 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); | |
output = 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