jul11co-mangadl
Version:
Jul11co Web Manga Downloader
273 lines (225 loc) • 9.13 kB
JavaScript
var path = require('path');
var urlutil = require('url');
var utils = require('jul11co-wdt').Utils;
module.exports = {
name: 'ASMHentai',
website: 'http://asmhentai.com',
match: function(link, options) {
return /asmhentai\.com\/g/g.test(link);
},
dispatch: function(saver, $, page, options, callback) {
if (options.group_by_site && !options.orig_output_dir) {
options.orig_output_dir = options.output_dir;
options.output_dir = path.join(options.orig_output_dir, 'ASMHentai');
saver.setMangaOutputDir(options.output_dir);
}
var state = saver.getStateData(page.url);
if (state && state.done && !options.refresh_info) {
if (options.cbz && state.chapter_images) {
return saver.createComicFile(page.output_dir, state.chapter_images, callback);
} else {
return callback();
}
}
var updateDownloadState = function(cb) {
// Check if all chapter pages are visited
var all_chapter_pages_visited = true;
for (var chapter_page_url in options.chapter_pages) {
if (options.chapter_pages[chapter_page_url].visited == false) {
all_chapter_pages_visited = false;
break;
}
}
if (all_chapter_pages_visited) {
var chapter_images = [];
for (var chapter_page_url in options.chapter_pages) {
// var chapter_page = options.chapter_pages[chapter_page_url];
var chapter_page = saver.getStateData(chapter_page_url);
if (chapter_page.images) chapter_images = chapter_images.concat(chapter_page.images);
}
if (options.debug) console.log(chapter_images);
// Reset chapter_pages
options.chapter_pages = {};
var chapter_url = options.chapter_url;
var chapter_output_dir = options.chapter_output_dir;
saver.downloadMangaChapter({
chapter_url: options.chapter_url,
chapter_title: options.chapter_title,
chapter_images: chapter_images,
output_dir: options.chapter_output_dir
}, options, cb);
} else {
// Get next unprocessed page in options.chapter_pages
var next_page_url = '';
for (var chapter_page_url in options.chapter_pages) {
if (options.chapter_pages[chapter_page_url].visited == false) {
next_page_url = chapter_page_url;
break;
}
}
var all_chapter_pages_count = 0;
var visited_chapter_pages_count = 0;
for (var chapter_page_url in options.chapter_pages) {
all_chapter_pages_count++;
if (options.chapter_pages[chapter_page_url].visited) visited_chapter_pages_count++;
}
console.log('Chapter page:', (visited_chapter_pages_count+1) + '/' + all_chapter_pages_count, next_page_url);
// Process next page
saver.processPage(next_page_url, options, cb);
}
}
if ($('.gallery').length) {
console.log('----')
console.log('Gallery page:', page.url);
var manga_title = page.title.replace(' - AsmHentai.com', '').trim();
manga_title = utils.replaceAll(manga_title, '/', '-');
console.log('Gallery:', manga_title);
if (options.auto_manga_dir) {
options.output_dir = path.join(options.output_dir, manga_title);
saver.setMangaOutputDir(options.output_dir);
}
saver.setStateData('url', page.url);
if (options.save_index_html) {
saver.saveHtmlSync(path.join(options.output_dir, 'index.html'), $.html());
}
var manga_info = this.getMangaInfo($, page, options);
if (manga_info && manga_info.url) {
saver.saveJsonSync(path.join(options.output_dir, 'manga.json'), manga_info);
}
options.chapter_url = page.url;
options.chapter_title = manga_title;
var chapter_url_obj = urlutil.parse(options.chapter_url);
var output_dir_name = path.basename(chapter_url_obj.pathname);
options.chapter_output_dir = path.join(options.output_dir, output_dir_name);
var chapter_page_links = [];
// chapter_page_links.push(page.url);
saver.fixLinks($, page, '.gallery');
$('.gallery .preview_thumb a').each(function() {
chapter_page_links.push($(this).attr('href'));
});
if (options.debug) console.log(chapter_page_links);
if (typeof options.chapter_pages == 'undefined') {
// Init data holder for chapter pages in options (passed through all callback)
options.chapter_pages = {};
}
for (var i = 0; i < chapter_page_links.length; i++) {
var chapter_page_link = chapter_page_links[i];
if (typeof options.chapter_pages[chapter_page_link] == 'undefined') {
var chapter_page = saver.getStateData(chapter_page_link);
if (chapter_page && chapter_page.visited && chapter_page.images) {
options.chapter_pages[chapter_page_link] = {
visited: true,
// images: []
};
} else {
options.chapter_pages[chapter_page_link] = {
visited: false,
// images: []
};
}
}
}
updateDownloadState(callback);
} else if ($('.full_gallery').length) {
if (typeof options.chapter_pages != 'undefined'
&& typeof options.chapter_pages[page.url] != 'undefined') {
options.chapter_pages[page.url].visited = true;
} else {
console.log('Wrong state! This page not included in chapter links');
return callback();
}
// Get images on current page
var images = saver.getImages($, page, '.full_gallery');
if (options.debug) console.log(images);
saver.updateStateData(page.url, {
images: images
});
// Save to options
// options.chapter_pages[page.url].images = options.chapter_pages[page.url].images.concat(images);
updateDownloadState(callback);
} else {
callback();
}
},
getMangaInfo: function($, page, options) {
var manga_info = {};
if (page.url.indexOf('/g/')!=-1 && $('.book_page').length) {
manga_info = {};
manga_info.url = page.url;
manga_info.name = $('.book_page .info h1').first().text().trim();
manga_info.subtitle = $('.book_page .info h2').first().text().trim();
manga_info.cover_image = $('.book_page .cover img').first().attr('src');
// manga_info.description = $('.manga-detail .detail .content').text().trim();
$('.book_page .info .tags a').each(function() {
var tag_url = $(this).attr('href');
var tag_name = $(this).text().trim();
if (tag_name) tag_name = tag_name.split('(')[0].trim();
if (tag_url.indexOf('/parodies/') != -1) {
manga_info.parodies = manga_info.parodies || [];
manga_info.parodies.push({
name: tag_name,
url: tag_url
});
} else if (tag_url.indexOf('/character/') != -1) {
manga_info.characters = manga_info.characters || [];
manga_info.characters.push({
name: tag_name,
url: tag_url
});
} else if (tag_url.indexOf('/tags/') != -1) {
manga_info.tags = manga_info.tags || [];
manga_info.tags.push({
name: tag_name,
url: tag_url
});
} else if (tag_url.indexOf('/artists/') != -1) {
manga_info.artists = manga_info.artists || [];
manga_info.artists.push({
name: tag_name,
url: tag_url
});
} else if (tag_url.indexOf('/group/') != -1) {
manga_info.groups = manga_info.groups || [];
manga_info.groups.push({
name: tag_name,
url: tag_url
});
} else if (tag_url.indexOf('/language/') != -1) {
if (tag_name != 'translated') {
manga_info.language = {
name: tag_name,
url: tag_url
};
}
} else if (tag_url.indexOf('/category/') != -1) {
manga_info.categories = manga_info.categories || [];
manga_info.categories.push({
name: tag_name,
url: tag_url
});
}
});
if (options.with_chapters) {
manga_info.chapter_url = manga_info.url;
manga_info.chapter_title = manga_info.name;
manga_info.chapters = [];
manga_info.chapters.push({
url: manga_info.url,
title: manga_info.name
});
}
manga_info.chapter_count = 1;
if (options.verbose) {
console.log('Manga:');
console.log(' Name:', manga_info.name);
console.log(' Cover image:', manga_info.cover_image);
// console.log(' Description:', manga_info.description);
console.log(' Authors:', manga_info.authors);
console.log(' Genres:', manga_info.genres);
console.log(' Status:', manga_info.status);
console.log(' Chapter count:', manga_info.chapter_count);
}
}
return manga_info;
}
}