Skip to content

Instantly share code, notes, and snippets.

@mekanics
Last active October 21, 2021 12:45
Show Gist options
  • Save mekanics/23512d7c16940dce974e98774e7736ea to your computer and use it in GitHub Desktop.
Save mekanics/23512d7c16940dce974e98774e7736ea to your computer and use it in GitHub Desktop.
Download all the files from your Google Classroom
/**
* HOW TO
*
* 1. go to the classwork (open the classroom > classwork)
* 2. open developer console ( F12 or right click > inspect element > console in top)
* 3. copy and paste this code, assignments will be downloaded in 1 second intervals. A list of the documents outside Google Drive will be available at the end
*/
const listItemClass = '.jWCzBe'
const materialClass = '.vwNuXe'
const downloadMaterial = async () => {
await openAllItems()
const materialLinks = getMaterials()
.map(getMaterialLink)
.filter((e) => !!e)
const groupedMaterialLinks = materialLinks.reduce(
(acc, link) => {
if (isGDriveLink(link)) acc.gdrive.push(link)
else acc.other.push(link)
return acc
},
{ gdrive: [], other: [] }
)
const downloadLinks = groupedMaterialLinks.gdrive
.map(extractId)
.filter((e) => !!e)
.map(createDownloadLink)
for (const link of downloadLinks) {
console.log(`⬇️ downloading ${link}`)
window.open(link)
await sleep(1000)
}
console.log('\n\n')
console.log('🔗 List of the links/files outside Google Drive')
console.log('===============================================')
console.log(groupedMaterialLinks.other.join('\n'))
console.log('===============================================')
}
const openAllItems = async () => {
const listItems = Array.from(document.querySelectorAll(listItemClass))
for (const item of listItems) {
item.click()
await sleep(500)
}
}
const getMaterials = () => Array.from(document.querySelectorAll(materialClass))
const getMaterialLink = (elem) => elem.href
const extractId = (driveUrl) => {
const rx = /https:\/\/drive.google.com\/file\/d\/(.*)\/view/
const res = rx.exec(driveUrl)
return res?.[1]
}
const isGDriveLink = (url) => url.includes('drive.google.com')
const createDownloadLink = (id) => `https://drive.google.com/u/0/uc?id=${id}&export=download`
const sleep = async (time) => await new Promise((r) => setTimeout(r, time))
downloadMaterial()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment