From 56f6464ecd7eea4d5ad0a918b7db7807d9dc41c1 Mon Sep 17 00:00:00 2001 From: ronny1982 Date: Sat, 19 Dec 2020 18:38:36 +0100 Subject: [PATCH] [hentai2read] fixed CSS queries, updated to async API method (#2777) --- src/web/mjs/connectors/Hentai2Read.mjs | 125 +++++++++---------------- 1 file changed, 43 insertions(+), 82 deletions(-) diff --git a/src/web/mjs/connectors/Hentai2Read.mjs b/src/web/mjs/connectors/Hentai2Read.mjs index d0f33f7c48..20621919a3 100644 --- a/src/web/mjs/connectors/Hentai2Read.mjs +++ b/src/web/mjs/connectors/Hentai2Read.mjs @@ -1,103 +1,64 @@ import Connector from '../engine/Connector.mjs'; +import Manga from '../engine/Manga.mjs'; -/** - * - */ export default class Hentai2Read extends Connector { - /** - * - */ constructor() { super(); - // Public members for usage in UI (mandatory) super.id = 'hentai2read'; super.label = 'Hentai2R'; this.tags = [ 'hentai', 'english' ]; super.isLocked = false; - // Private members for internal usage only (convenience) this.url = 'https://hentai2read.com'; - // Private members for internal use that can be configured by the user through settings menu (set to undefined or false to hide from settings menu!) - this.config = undefined; } - /** - * - */ - _getMangaListFromPages( mangaPageLinks, index ) { - index = index || 0; - return this.fetchDOM( mangaPageLinks[ index ], 'div.img-container div.img-overlay > a', 5 ) - .then( data => { - let mangaList = data.map( element => { - return { - id: this.getRelativeLink( element ), - title: element.text.trim() - }; - } ); - if( index < mangaPageLinks.length - 1 ) { - return this._getMangaListFromPages( mangaPageLinks, index + 1 ) - .then( mangas => mangaList.concat( mangas ) ); - } else { - return Promise.resolve( mangaList ); - } - } ); + async _getMangaFromURI(uri) { + const request = new Request(uri, this.requestOptions); + const data = await this.fetchDOM(request, 'main div.content span[itemprop="name"]'); + return new Manga(this, uri.pathname, data[0].textContent.trim()); } - /** - * - */ - _getMangaList( callback ) { - this.fetchDOM( this.url + '/hentai-list', 'ul.pagination li:nth-last-child(2) a' ) - .then( data => { - let pageCount = parseInt( data[0].text.trim() ); - let pageLinks = [... new Array( pageCount ).keys()].map( page => this.url + '/hentai-list/all/any/all/name-az/' + ( page + 1 ) + '/' ); - return this._getMangaListFromPages( pageLinks ); - } ) - .then( data => { - callback( null, data ); - } ) - .catch( error => { - console.error( error, this ); - callback( error, undefined ); - } ); + async _getMangas() { + let mangaList = []; + const uri = new URL('/hentai-list', this.url); + const request = new Request(uri, this.requestOptions); + const data = await this.fetchDOM(request, 'ul.pagination li:nth-last-child(2) a'); + const pageCount = parseInt(data[0].text.trim()); + for(let page = 1; page <= pageCount; page++) { + let mangas = await this._getMangasFromPage(page); + mangaList.push(...mangas); + } + return mangaList; } - /** - * - */ - _getChapterList( manga, callback ) { - this.fetchDOM( this.url + manga.id, 'ul.nav-chapters li div.media > a' ) - .then( data => { - let chapterList = data.map( element => { - return { - id: this.getRelativeLink( element ), - title: element.firstChild.textContent.replace( manga.title, '' ).trim(), - language: 'en' - }; - } ); - callback( null, chapterList ); - } ) - .catch( error => { - console.error( error, manga ); - callback( error, undefined ); - } ); + async _getMangasFromPage(page) { + const uri = new URL(`/hentai-list/all/any/all/name-az/${page}/`, this.url); + const request = new Request(uri, this.requestOptions); + const data = await this.fetchDOM(request, 'div.book-grid div.overlay div.overlay-title a'); + return data.map(element => { + return { + id: this.getRootRelativeOrAbsoluteLink(element, this.url), + title: element.text.trim() + }; + }); } - /** - * - */ - _getPageList( manga, chapter, callback ) { - fetch( this.url + chapter.id, this.requestOptions ) - .then( response => response.text() ) - .then( data => { - let pageList = data.match( /['"]images['"]\s*:\s*(\[[^\]]*?\])/ )[1]; - pageList = JSON.parse( pageList ); - pageList = pageList.map( image => 'https://static.hentaicdn.com/hentai' + image ); - callback( null, pageList ); - } ) - .catch( error => { - console.error( error, chapter ); - callback( error, undefined ); - } ); + async _getChapters(manga) { + const uri = new URL(manga.id, this.url); + const request = new Request(uri, this.requestOptions); + const data = await this.fetchDOM(request, 'ul.nav-chapters li div.media > a'); + return data.map(element => { + return { + id: this.getRootRelativeOrAbsoluteLink(element, this.url), + title: element.firstChild.textContent.trim() + }; + }); + } + + async _getPages(chapter) { + const uri = new URL(chapter.id, this.url); + const request = new Request(uri, this.requestOptions); + const data = await this.fetchRegex(request, /['"]images['"]\s*:\s*(\[[^\]]*?\])/g); + return JSON.parse(data[0]).map(image => 'https://static.hentaicdn.com/hentai' + image); } } \ No newline at end of file