Skip to content

Instantly share code, notes, and snippets.

@thesabbir
Created May 8, 2015 00:03
Show Gist options
  • Save thesabbir/3911fd2a95fc48282406 to your computer and use it in GitHub Desktop.
Save thesabbir/3911fd2a95fc48282406 to your computer and use it in GitHub Desktop.
/**
* Created by sabbir on 5/7/15.
* Config for Elaach server
*/
var base = 'http://elaach.com';
var baseMovie = base + '/index.php/movie';
module.exports.config = {
base : base,
movies : {
english : baseMovie + '/menu-english',
hindi: baseMovie+ '/menu-hindi',
bangla: baseMovie + '/2025-03-06-23-14-19',
others: baseMovie+ '/menu-other-language',
animation: baseMovie+ '/menu-animation'
}
};
var request = require('request-promise'),
cheerio = require('cheerio'),
elaach = require('./elaach'),
base = elaach.config.base,
Datastore = require('nedb')
, moviedb = new Datastore({filename: __dirname + '/data/movies.db', autoload: true});
function getMovies(category, page) {
var limit = 20;
if (page == undefined) page = 0;
request({uri: elaach.config.movies[category] + '?start=' + page + '&&limit=' + limit, timeout: 20000})
.then(function (body) {
return cheerio.load(body, {
normalizeWhitespace: true,
xmlMode: true
})
})
.then(function ($) {
var list = $('table.category > tbody > tr').find('a');
if (list.length < 20) {
page = null;
}
var urls = [];
list.map(function (index, elem) {
urls.push(base + $(this).attr('href'));
});
if (urls.length == limit) {
getMovies(category, page + limit);
}
return urls;
})
.map(function (url) {
request({uri: url, timeout: 20000})
.then(function (body) {
return cheerio.load(body);
})
.then(function ($) {
return {
"title": $('.dd-postheader > a').text(),
"cover": base + $('.dd-article').find('img').attr('src'),
"file": $('.dd-article').find('a').attr('href'),
"category": category
}
})
.then(function (data) {
moviedb.insert(data, function (err, data) {
console.log("Inserted \"%s\" to the database.", data.title);
})
});
}
)
.
catch(console.error);
}
for(var cat in elaach.config.movies) {
getMovies(cat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment