Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script para baixar livros do site http://lelivros.li/
/*
* lelivros
* user/repo
*
* Copyright (c) 2014 Sérgio Vilar
* Licensed under the MIT license.
*/
'use strict';
// Carrega os módulos do node
var jsdom = require('jsdom');
var request = require('request');
var fs = require('fs');
var mkdirp = require('mkdirp');
var http = require('http');
var url = require('url');
var clc = require('cli-color');
var jquery = fs.readFileSync("./bower_components/jquery/jquery.min.js").toString();
var BaixaLivros = function(){
console.log('Iniciando...');
this.enderecoSite = 'http://lelivros.li/';
this.init();
};
BaixaLivros.prototype.init = function(){
console.log('init');
var that = this;
try{
that.pegaPagina(1);
}catch(e){
console.log(e.message);
}
};
BaixaLivros.prototype.pegaPagina = function(number){
console.log(clc.blue('Abrindo página '+ number));
var that = this;
request(that.enderecoSite + '/book/page/'+number + '/', function(error, response, body){
if(error){
throw new Error(error);
}
jsdom.env({
html: body,
src: [
jquery
],
done: function(errors, window) {
var $ = window.$;
var body = $('body');
body.find('li.product').each(function(i, item){
var livro = {
url: $(item).find('h3').parent().attr('href'),
titulo: $(item).find('h3').text()
};
that.pegaLivros(livro);
});
if(body.find('li.product').length > 0){
that.pegaPagina(++number);
}
}
});
});
};
BaixaLivros.prototype.pegaLivros = function(livro){
var that = this;
try{
request(livro.url, function(error, response, body){
if(error){
throw new Error(error);
}
jsdom.env({
html: body,
src: [
jquery
],
done: function(errors, window) {
var $ = window.$;
var dirname = $('.woocommerce-breadcrumb').find('a:eq(1)').text();
var path = 'downloads/'+dirname+'/'+livro.titulo + '/';
mkdirp(path, function(err) {
if(err){
throw new Error(err);
}
// EPUB
var epub = $('body').find('img[src="http://lelivros.li/wp-content/themes/xing/images/download/botao%20epub.png"]').parent().attr('href');
that.baixalivro(epub, path);
// MOBI
var mobi = $('body').find('img[src="http://lelivros.li/wp-content/themes/xing/images/download/botao%20mobi.png"]').parent().attr('href');
that.baixalivro(mobi, path);
// PDF
var pdf = $('body').find('img[src="http://lelivros.li/wp-content/themes/xing/images/download/botao%20pdf.png"]').parent().attr('href');
that.baixalivro(pdf, path);
});
}
});
});
}catch(e){
console.log(clc.red('ERRO: ')+ e.message);
}
};
BaixaLivros.prototype.baixalivro = function(file_url, path){
try{
if(typeof file_url === 'undefined'){
throw new Error(path + ' não pode ser baixado');
}
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
};
var file_name = url.parse(file_url).pathname.split('/').pop();
var file = fs.createWriteStream(path + file_name);
http.get(options, function(res) {
res.on('data', function(data) {
file.write(data);
}).on('end', function() {
file.end();
console.log(clc.green('OK') +': '+ path + file_name);
});
});
}catch(e){
console.log(clc.red('ERRO: ')+ e.message);
}
};
new BaixaLivros();
{
"name": "lelivros",
"version": "0.0.0",
"main": "lib/lelivros.js",
"description": "The best module ever.",
"homepage": "user/repo",
"bugs": "user/repo/issues",
"author": {
"name": "Sérgio Vilar",
"email": "vilar@me.com",
"url": "http://sergiovilar.com.br"
},
"repository": {
"type": "git",
"url": "user/repo"
},
"licenses": [
{
"type": "MIT"
}
],
"files": [
""
],
"keywords": [],
"devDependencies": {
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-watch": "~0.5.0",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.0",
"jshint-stylish": "~0.1.3"
},
"scripts": {
"test": "grunt"
},
"dependencies": {
"jquery": "~2.1.0",
"request": "~2.33.0",
"jsdom": "~0.10.1",
"http": "0.0.0",
"mkdirp": "~0.3.5",
"url": "~0.7.9",
"cli-color": "~0.2.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.