From 732dbee591dda37c738fac6f0b230d7d7941dde1 Mon Sep 17 00:00:00 2001 From: ronny1982 Date: Sat, 1 Feb 2020 22:19:44 +0100 Subject: [PATCH] [mangahub] migrated to new API (#1033) improved GraphQL queries updated image CDN --- src/web/mjs/connectors/MangaHub.mjs | 169 +++++++++------------------- 1 file changed, 56 insertions(+), 113 deletions(-) diff --git a/src/web/mjs/connectors/MangaHub.mjs b/src/web/mjs/connectors/MangaHub.mjs index b1cc1e0625d..a88f090cd10 100644 --- a/src/web/mjs/connectors/MangaHub.mjs +++ b/src/web/mjs/connectors/MangaHub.mjs @@ -10,33 +10,28 @@ export default class MangaHub extends Connector { this.tags = [ 'manga', 'english' ]; this.url = 'https://mangahub.io'; this.apiURL = 'https://api2.mangahub.io/graphql'; - this.cdnURL = 'https://cdn.mangahub.io/file/imghub/'; + this.cdnURL = 'https://img.mghubcdn.com/file/imghub/'; this.path = 'm01'; } - /** - * - */ - _getJsonResponse( payload ) { + async _getGraphQL(gql) { this.requestOptions.method = 'POST'; - this.requestOptions.body = JSON.stringify( payload ); - this.requestOptions.headers.set( 'content-type', 'application/json' ); - let promise = fetch( this.apiURL, this.requestOptions ) - .then( response => response.json() ) - .then( data => { - if( data[ 'errors' ] ) { - throw new Error( this.label + ' errors: ' + data.errors.map( error => error.message ).join( '\n' ) ); - } - if( !data[ 'data' ] ) { - throw new Error( this.label + 'No data available!' ); - } - return Promise.resolve( data.data ); - } ); - this.requestOptions.headers.delete( 'content-type' ); + this.requestOptions.body = JSON.stringify({ query: gql }); + let request = new Request(this.apiURL, this.requestOptions); + request.headers.set('content-type', 'application/json'); + this.requestOptions.headers.delete('content-type'); delete this.requestOptions.body; this.requestOptions.method = 'GET'; - return promise; + + let data = await this.fetchJSON(request); + if(data.errors) { + throw new Error(this.label + ' errors: ' + data.errors.map(error => error.message).join('\n')); + } + if(!data.data) { + throw new Error(this.label + 'No data available!'); + } + return data.data; } async _getMangaFromURI(uri) { @@ -47,103 +42,51 @@ export default class MangaHub extends Connector { return new Manga(this, id, title); } - /** - * - */ - _getMangaListFromPages( offset ) { - offset = offset || 0; - let payload = { - query: `{ - search( x:${this.path}, q:"", genre:"all", mod:ALPHABET, count:true, offset:${offset} ) - { - rows { - id, title, slug - } - } - }` - }; - return this._getJsonResponse( payload ) - .then( data => { - let mangaList = data.search.rows.map( manga => { - return { - id: manga.slug, // manga.id - title: manga.title - }; - } ); - if( mangaList.length > 0 ) { - return this._getMangaListFromPages( offset + 30 ) - .then( mangas => mangaList.concat( mangas ) ); - } else { - return Promise.resolve( mangaList ); + async _getMangas() { + let gql = `{ + search(x: ${this.path}, q: "", genre: "all", mod: ALPHABET, limit: 99999) { + rows { + id, slug, title } - } ); + } + }`; + let data = await this._getGraphQL(gql); + return data.search.rows.map(manga => { + return { + id: manga.slug, // manga.id + title: manga.title + }; + }); } - /** - * - */ - _getMangaList( callback ) { - this._getMangaListFromPages() - .then( data => { - callback( null, data ); - } ) - .catch( error => { - console.error( error, this ); - callback( error, undefined ); - } ); - } - - /** - * - */ - _getChapterList( manga, callback ) { - let payload = { - query: `{ - manga( x:${this.path}, slug:"${manga.id}" ) { - chapters { - id, number, title, slug - } - } - }` - }; - this._getJsonResponse( payload ) - .then( data => { - let chapterList = data.manga.chapters.map( chapter => { - // .padStart( 4, '0' ) - let title = `Ch. ${chapter.number} - ${chapter.title}`; - return { - id: chapter.number, // chapter.id, chapter.slug, - title: title.trim(), - language: '' - }; - } ); - callback( null, chapterList ); - } ) - .catch( error => { - console.error( error, manga ); - callback( error, undefined ); - } ); + async _getChapters(manga) { + let gql = `{ + manga(x: ${this.path}, slug: "${manga.id}") { + chapters { + id, number, title, slug + } + } + }`; + let data = await this._getGraphQL(gql); + return data.manga.chapters.map(chapter => { + // .padStart( 4, '0' ) + let title = `Ch. ${chapter.number} - ${chapter.title}`; + return { + id: chapter.number, // chapter.id, chapter.slug, + title: title.trim(), + language: '' + }; + }); } - /** - * - */ - _getPageList( manga, chapter, callback ) { - let payload = { - query: `{ - chapter( x:${this.path}, slug:"${manga.id}", number:${chapter.id}) { - pages - } - }` - }; - this._getJsonResponse( payload ) - .then( data => { - let pageList = Object.values( JSON.parse( data.chapter.pages ) ).map( page => this.cdnURL + page ); - callback( null, pageList ); - } ) - .catch( error => { - console.error( error, chapter ); - callback( error, undefined ); - } ); + async _getPages(chapter) { + let gql = `{ + chapter(x: ${this.path}, slug: "${chapter.manga.id}", number: ${chapter.id}) { + pages + } + }`; + let data = await this._getGraphQL(gql); + data = JSON.parse(data.chapter.pages); + return Object.values(data).map(page => new URL(page, this.cdnURL).href); } } \ No newline at end of file