ghoullier
6/30/2013 - 1:35 AM

CasperJS file to download manga from scan-mx.com

CasperJS file to download manga from scan-mx.com

// Casper JS Instance
var casper =  require('casper').create();
// Scan-mx domain
var domain = 'http://www.scan-mx.com';
// URL Separator
var separator = '/';
// Command Line Argument: Manga
var manga = casper.cli.get(0) || 'naruto';
// Command Line Argument: Episode Number
var episode = casper.cli.get(1) || 1;
// Chapter
var chapter = episode;
// RegExp to find latest episode
var finder = new RegExp(manga + '-scan-' + '\\d*');
// Scan-mx entry point url
var start = (function () {
  return [domain, 'mangas', manga].join('/');
}());
// Pages count to download
var pages = 0;
// Function return download URL for a specific episode
var getDownloadURL = function getDownloadURL(page) {
  return [domain, 'lecture-en-ligne/mangas', manga, chapter, page].join('/') + '.jpg';
};
// Function return local file path for a specific episode
var getLocalFile = function getLocalFile (page) {
  var path = ['mangas', manga, chapter].join('/');
  return path + '/' + page + '.jpg';
};
// Redirect to the first reading page
casper.start(start, function () {
  // Use Comand Line Argument or find the latest available episode
  if (typeof casper.cli.get(1) === 'undefined') {
    var temp = this.evaluate(function () {
      return document.querySelector('#archive-posts li:first-child').getAttribute('class');
    });
    temp = temp.match(finder)[0];
    episode = temp.substring(temp.lastIndexOf('-') + 1);
  }
  var first = [domain, 'lecture-en-ligne', manga, episode].join(separator);
  this.open(first);
});
// Check if the pages are available
casper.then(function () {
  // Get pages count
  pages = this.evaluate(function () {
    return document.querySelector('select[name="page"]').querySelectorAll('option').length;
  });
  // Exit if episode is not available
  if (2 === pages) {
    this.echo('L`épisode ' + episode + ' de ' + manga + ' n`éxiste pas ...').exit();
  } else {
    // Get chapter value
    chapter = this.evaluate(function () {
      return document.querySelector('select[name="chapter"]').querySelector('option[selected]').textContent;
    });
  }
});
// Download pages
casper.then(function () {
  var url, file, page;
  for (var index = 0; index < pages - 1; ++index) {
    page = (index < 10) ? '0' + index : index;
    url = getDownloadURL(page);
    file = getLocalFile(page);
    this.echo('Download:' + url);
    this.download(url, file);
  }
});
// Start
casper.run(function () {
  this.echo('Download completed').exit();
});