|
/** |
|
* Copyright 2017 Yulio Aleman Jimenez |
|
* |
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions |
|
* are met: |
|
* |
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|
* |
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in |
|
* the documentation and/or other materials provided with the distribution. |
|
* |
|
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived |
|
* from this software without specific prior written permission. |
|
* |
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
|
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
|
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
*/ |
|
|
|
var fs = require('fs'); |
|
var args = require('system').args; |
|
var casper = require('casper').create({ |
|
verbose: true, |
|
pageSettings: { |
|
webSecurityEnabled: false, |
|
loadImages: false, |
|
loadPlugins: false, |
|
} |
|
}); |
|
|
|
var prefixFile = '_[Vue.JS]'; |
|
var sufixFile = '.mp4'; |
|
const domain = 'https://laracasts.com'; |
|
const initURL = 'https://laracasts.com/series/learn-vue-2-step-by-step/episodes/1'; |
|
|
|
if (args.length < 4) { |
|
casper.echo("Usage: casperjs --ignore-ssl-errors=true script.js"); |
|
casper.exit(0) ; |
|
} |
|
|
|
// wait 7 seconds before giving up |
|
casper.options.waitTimeout = 10000000 |
|
|
|
casper.onResourceTimeout = function(request) { |
|
console.log('Response (#' + request.id + '): ' + JSON.stringify(request)); |
|
}; |
|
|
|
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X); Safari'); |
|
|
|
casper.on('started', function () { |
|
this.page.customHeaders = { 'Accept': '*/*' } |
|
}); |
|
|
|
// casper.on('resource.error',function (request) { |
|
// this.echo('RESOURCE: [' + request.id + '] IN: [' + request.url + '] SAID: [' + request.errorString + ']' ); |
|
// }); |
|
|
|
casper |
|
.start() |
|
.open(initURL) |
|
.then(function () { |
|
|
|
var listOfLinks = this.evaluate(function(){ |
|
|
|
// Getting links to other pages to scrape, this will be |
|
// a primitive array that will be easily returned from page.evaluate |
|
var links = [].map.call(document.querySelectorAll('div.sidebar-section ul.condensed-episode-list li a.episode-title'), function(link) { |
|
return link.href; |
|
}); |
|
return links; |
|
}); |
|
|
|
var listOfTitles = this.evaluate(function(){ |
|
var titles = [].map.call(document.querySelectorAll('div.sidebar-section ul.condensed-episode-list li a.episode-title'), function(title) { |
|
var spliting = title.innerHTML.split('\n'); |
|
return spliting[1].split(' ').join('_').split(':').join('').split('#').join('No'); |
|
}); |
|
return titles; |
|
}); |
|
|
|
var currLinks = 1; |
|
var fileName = ""; |
|
var output = "#!/bin/bash \n"; |
|
|
|
// Now to iterate over that array of links |
|
this.each(listOfLinks, function(self, eachPageHref) { |
|
|
|
self.thenOpen(eachPageHref, function () { |
|
|
|
// Getting video from each page |
|
var video = this.evaluate(function() { |
|
return document.querySelector('source[data-default="true"]').getAttribute('src'); |
|
}); |
|
|
|
fileName = ((currLinks < 10) ? '0' + currLinks : currLinks ) + '_' + listOfTitles[currLinks - 1] + prefixFile + sufixFile; |
|
|
|
// if(currLinks < 40){ |
|
output += 'wget -O "' + fileName + '" "https:' + video + '" \n'; |
|
console.log('ADDED [' + fileName + ']'); |
|
// } else { |
|
// console.log('NOT ADDED [' + fileName + ']'); |
|
// } |
|
currLinks++; |
|
|
|
}); |
|
}); |
|
|
|
this.then(function(){ |
|
// output += 'mv ' + prefixFile + '* upload/ \n'; |
|
fs.write("video.sh", output, 'w'); |
|
}); |
|
}) |
|
|
|
casper.run(); |