Skip to content

Instantly share code, notes, and snippets.

@ddemydenko
Last active January 9, 2020 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddemydenko/03a818b5588ae44abf71e4cb45201e10 to your computer and use it in GitHub Desktop.
Save ddemydenko/03a818b5588ae44abf71e4cb45201e10 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const https = require('https');
const rp = require('request-promise-native');
const cheerio = require('cheerio');
const userCreds = {
e_mail: '',
password: ''
};
const headers = {
'Host': 'coursehunter.net',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Origin': ' https://coursehunter.net',
'Connection': ' keep-alive',
'Referer': 'https://coursehunter.net/',
'Upgrade-Insecure-Requests': '1',
};
const loginUrl = 'https://coursehunter.net/sign-in';
const courseUrl = 'https://coursehunter.net/course/node-js-prodvinutye-temy';
function login() {
const options = {
url:loginUrl,
headers: { ...headers, 'Content-Type': 'application/x-www-form-urlencoded' },
form: userCreds,
resolveWithFullResponse: true,
simple: false
};
return rp.post(options);
}
function getPage(cookie) {
const options = {
url:courseUrl,
headers: { ...headers, 'Cookie': cookie },
};
return rp.get(options);
}
function setCookies(res){
const cookie = ['locale=ru'].concat(res.headers['set-cookie']);
return cookie.join('; ');
}
async function saveVideo(link){
const fileName = link.split('/').pop();
const file = fs.createWriteStream(fileName);
return new Promise((resolve, reject) => {
https.get(link, function(response) {
response.pipe(file);
resolve();
}).on('error', (e) => {
reject(e);
});
});
}
async function main() {
const res = await login();
const cookie = setCookies(res);
const page = await getPage(cookie);
const $ = cheerio.load(page);
const links = $('link[itemprop=contentUrl]');
for (const i in links){
await saveVideo(links[i].attribs.href);
}
console.log(links);
}
main().then(console.log).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment