Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hentai2read] fixed CSS queries, updated to async API method #2777

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 43 additions & 82 deletions src/web/mjs/connectors/Hentai2Read.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
}