UNPKG

jul11co-mangadl

Version:
363 lines (306 loc) 12.4 kB
var path = require('path'); var urlutil = require('url'); var utils = require('jul11co-wdt').Utils; module.exports = { name: 'Hentaifox', website: 'http://hentaifox.com', match: function(link, options) { return /hentaifox\.com\//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.output_dir, 'HentaiFox'); saver.setMangaOutputDir(options.output_dir); } // saver.saveHtmlFile($, page, options); 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) { if (options.chapter_url == '' || typeof options.chapter_url == 'undefined') { return 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); chapter_images = chapter_images.concat(chapter_page.images); } if (options.debug) console.log(chapter_images); var chapter_url = options.chapter_url; // Reset options.chapter_pages = {}; options.chapter_url = ''; saver.downloadMangaChapter({ chapter_url: 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); } } var linkExists = function(link, container) { var links = saver.getLinks($, page, container || 'body', { filters: [link] }); return (links.length > 0); } if (/hentaifox\.com\/gallery\//g.test(page.url) && $('.info').length && $('.gallery').length) { console.log('----'); console.log('Gallery page: ' + page.url); var manga_title = $('.info h1').text().trim(); manga_title = utils.replaceAll(manga_title, '/', '-'); console.log('Gallery: ' + manga_title); if (options.language && !linkExists('/' + options.language, '.info')) { console.log('No language matched: ', options.language); return callback(); } if (options.category && !linkExists('/' + options.category, '.info')) { console.log('No category matched: ', options.category); return callback(); } if (options.tag && !linkExists('/' + options.tag, '.info')) { console.log('No tag matched: ', options.tag); return callback(); } if (options.artist && !linkExists('/' + options.artist, '.info')) { console.log('No artist matched: ', options.artist); return callback(); } if (options.auto_manga_dir && !options.batch_download) { options.output_dir = path.join(options.output_dir, manga_title); saver.deleteStateData(page.url); saver.setMangaOutputDir(options.output_dir); } else if (/*options.auto_manga_dir && */options.batch_download) { var manga_output_dir = path.join(options.batch_output_dir, manga_title); saver.deleteStateData(page.url); saver.setMangaOutputDir(manga_output_dir, {save_old_state: true}); options.output_dir = manga_output_dir; } if (saver.isDone(page.url) && !options.refresh_info) { return callback(); } 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 chapter_output_dir_name = path.basename(chapter_url_obj.pathname); options.chapter_output_dir = path.join(options.output_dir, chapter_output_dir_name); var chapter_links = saver.getLinks($, page, '.gallery .preview_thumb', { filters: ['hentaifox.com/g/'] }) 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_links.length; i++) { var chapter_page_link = chapter_links[i]; if (typeof options.chapter_pages[chapter_page_link] == 'undefined') { var chapter_page_cache = saver.getStateData(chapter_page_link); if (chapter_page_cache && chapter_page_cache.visited && chapter_page_cache.images) { options.chapter_pages[chapter_page_link] = { visited: true, // images: chapter_page_cache.images }; } else { options.chapter_pages[chapter_page_link] = { visited: false, // images: [] }; } } } saver.processPages(chapter_links, options, callback); } else if (/hentaifox\.com\/g\//g.test(page.url) && $('.gallery_content').length) { // Get images on current page var images = saver.getImages($, page, '.gallery_content'); if (options.debug) console.log(images); saver.updateStateData(page.url, { images: images }); 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(); } // Save to options // options.chapter_pages[page.url].images = options.chapter_pages[page.url].images.concat(images); updateDownloadState(callback); } else if ($('.galleries_overview').length) { options.paging_links = options.paging_links || {}; if (options.paging_links[page.url]) { return callback(); } options.paging_links[page.url] = 1; console.log('-----'); console.log('Gallery list: ' + page.url); options.batch_download = true; if (!options.batch_url) { options.batch_url = page.url; } if (!options.batch_output_dir) { options.batch_output_dir = options.output_dir; } if (options.batch_output_dir != options.output_dir) { saver.deleteStateData(page.url); saver.setMangaOutputDir(options.batch_output_dir, {save_old_state: true}); options.output_dir = options.batch_output_dir; } if (!saver.getStateData('url')) { saver.updateStateData('url', page.url); } var gallery_links = saver.getLinks($, page, '.galleries_overview .item', { filters: ['hentaifox.com/gallery/'] }); var paging_links = saver.getLinks($, page, '.pagination'); saver.downloadMangaList(gallery_links, options, function(err) { if (err) return callback(err); saver.setMangaOutputDir(options.batch_output_dir, {save_old_state: true}); options.output_dir = options.batch_output_dir; gallery_links.forEach(function(link) { if (!saver.isVisited(link)) saver.setVisited(link); }); saver.setStateData('last_update', new Date()); if (options.all_pages && paging_links.length > 0) { if (options.refresh_info) { paging_links = paging_links.filter(function(link) { return !options.paging_links[link]; }); paging_links.forEach(function(link) { saver.updateStateData(link, {visited: 0}); }); } else { paging_links = paging_links.filter(function(link) { return !saver.isVisited(link); }); } saver.processPages(paging_links, options, callback); } else { callback(); } }); } else { callback(); } }, getMangaInfo: function($, page, options) { var manga_info = {}; if (page.url.indexOf('/gallery/')!=-1) { manga_info.url = page.url; manga_info.name = $('.info h1').first().text().trim(); manga_info.cover_image = $('.cover img').first().attr('src'); // manga_info.description = $('.manga-detail .detail .content').text().trim(); $('.info span a').each(function() { var tag_url = $(this).attr('href'); var tag_name = $(this).text().trim(); if (tag_url.indexOf('/parody/') != -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('/tag/') != -1) { manga_info.tags = manga_info.tags || []; manga_info.tags.push({ name: tag_name, url: tag_url }); } else if (tag_url.indexOf('/artist/') != -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.toLowerCase() != '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; } }