Skip to content

Commit

Permalink
[mangahub] migrated to new API (#1033)
Browse files Browse the repository at this point in the history
improved GraphQL queries
updated image CDN
  • Loading branch information
ronny1982 authored Feb 1, 2020
1 parent 7b44781 commit 732dbee
Showing 1 changed file with 56 additions and 113 deletions.
169 changes: 56 additions & 113 deletions src/web/mjs/connectors/MangaHub.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

0 comments on commit 732dbee

Please sign in to comment.