Created
June 1, 2019 22:25
-
-
Save andreafalzetti/c95ae8a907ec949017333519214560c3 to your computer and use it in GitHub Desktop.
Lingoda Teaching Material Downloader in Node.js
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
/** | |
* Node.js Script to Download Lingoda Teaching Material | |
*/ | |
const fs = require('fs'); | |
const needle = require('needle'); | |
const { promisify } = require('util'); | |
const { stringify } = require('query-string'); | |
const mkdir = require('mkdirp-promise'); | |
const writeFileAsync = promisify(fs.writeFile); | |
const downloadLang = 'spanish'; // change this one | |
const lingodaUrl = 'https://www.lingoda.com'; | |
const apiUrl = `${lingodaUrl}/en/api`; | |
const downloadFolder = './download'; | |
const run = async () => { | |
await mkdir(downloadFolder); | |
const res = await needle(`${apiUrl}/modules`, { json: true }); | |
const modules = res.body.data; | |
modules.forEach(async m => { | |
console.log(`[${m.name}] Downloading Modules`); | |
const params = stringify({ | |
moduleId: m.id, | |
section: downloadLang | |
}); | |
console.log(`[API_CALL] ${apiUrl}/learning-units?${params}`); | |
let resUnits = await needle(`${apiUrl}/learning-units?${params}`, { | |
json: true | |
}); | |
if (!resUnits.body.included || !resUnits.body.included.lessons) { | |
console.log(`Skip Module ${m.name}`); | |
return; | |
} | |
const { lessons } = resUnits.body.included; | |
lessons.forEach(async lesson => { | |
const docUrl = `${lingodaUrl}${lesson.presentation}`; | |
console.log(`[Download] ${docUrl}`); | |
const doc = await needle(docUrl); | |
const titleSafe = lesson.title.replace(/ /g, '-'); | |
const pdfName = `${lesson.label}-${titleSafe}.pdf`; | |
const destFolder = `${downloadFolder}/${m.name}`; | |
await mkdir(destFolder); | |
await writeFileAsync(`${destFolder}/${pdfName}`, doc.body); | |
}); | |
}); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! I've tried the code but it doesn't work. Do you have an updated version?