Created
June 9, 2019 07:18
-
-
Save yunpengn/c4e6e60ec2046a6fa1311dd6d6c553d3 to your computer and use it in GitHub Desktop.
A useful script to download online course videos
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
// Imports the required libraries. | |
const https = require('https'); | |
const fs = require('fs'); | |
// Defines some constants. | |
const COURSE_HOMEPAGE = '<CHANGE_IT_HERE>'; | |
// Parses the given JSON file regarding URLs. | |
const url_json = require('./restricted_files.json'); | |
const url_list = url_json.data.getRestrictedFiles.urls; | |
// Filters according to the URL prefix. | |
const required_format = new RegExp(`${COURSE_HOMEPAGE}/video/.+/1080p/`);; | |
const filter_list = url_list.filter(url => url.match(required_format)); | |
console.log(`We found ${filter_list.length} URLs which meet the required format.`); | |
// Parses the given JSON file regarding course content. | |
const course_json = require('./course_content.json'); | |
const section_list = course_json.data.getCourse.sections; | |
// Iterates through each section of the course. | |
for (let i = 0; i < section_list.length; i++) { | |
const current_section = section_list[i]; | |
// Creates a folder for the current section if not exists. | |
const section_folder_name = `${current_section.sequence} ${current_section.title}`; | |
if (!fs.existsSync(section_folder_name)) { | |
fs.mkdirSync(section_folder_name); | |
} | |
for (let j = 0; j < current_section.components.length; j++) { | |
// Finds the video with the matched URL. | |
const current_component = current_section.components[j]; | |
const url_prefix = `${COURSE_HOMEPAGE}/video/${current_section.sectionIdentifier}_${current_component.componentIdentifier}/1080p/`; | |
const found_url = filter_list.find(url => url.startsWith(url_prefix)); | |
// Downloads the video if found. | |
if (found_url === undefined) { | |
console.log(`Unable to find any URL with a prefix of ${url_prefix} with section_title=${current_section.title} component_title=${current_component.title}`); | |
continue; | |
} | |
const output_file_name = `${current_section.sequence} ${current_section.title}/${current_component.sequence} ${current_component.title}.mp4`; | |
const output_file = fs.createWriteStream(output_file_name); | |
console.log(`Begins to download video from ${found_url} to ${output_file_name}:`); | |
https.get(found_url, resp => { | |
resp.pipe(output_file); | |
output_file.on('finish', () => output_file.end()); | |
}).on('error', err => console.log(err)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment