Skip to content

Commit

Permalink
add cartoonmad (#2583)
Browse files Browse the repository at this point in the history
* add cartoonmad

resolves #166
iconv decoding not currently working properly, no idea how to fix

* lint

* working

fetchDOM is still functional without encoding having a value

* fixes

* lint

* +1

Co-authored-by: 09morbab <30987265+09morbab@users.noreply.github.com>
  • Loading branch information
2 people authored and ronny1982 committed Nov 14, 2020
1 parent 072625c commit 6f247ba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Binary file added src/web/img/connectors/cartoonmad
Binary file not shown.
64 changes: 64 additions & 0 deletions src/web/mjs/connectors/CarToonMad.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Connector from '../engine/Connector.mjs';
import Manga from '../engine/Manga.mjs';

export default class CarToonMad extends Connector {

constructor() {
super();
super.id = 'cartoonmad';
super.label = 'CarToonMad';
this.tags = ['manga', 'chinese'];
this.url = 'https://www.cartoonmad.com';
this.requestOptions.headers.set('x-referer', this.url);
}

async _getMangas() {
let mangaList = [];
const request = new Request(new URL('/comic99.html', this.url), this.requestOptions);
const data = await this.fetchDOM(request, 'td[align] > a:last-child.pages');
const pageCount = parseInt(data[0].href.match(/(\d+)\.html/)[1]);
for(let page = 1; page <= pageCount; page++) {
let mangas = await this._getMangasFromPage(page);
mangaList.push(...mangas);
}
return mangaList;
}

async _getMangasFromPage(page) {
const request = new Request(new URL('/comic99.'+String(page).padStart(2, '0')+'.html', this.url), this.requestOptions);
const data = await this.fetchDOM(request, 'a.a1', 0, 'big5');
return data.map(element => {
return {
id: this.getRootRelativeOrAbsoluteLink(element, this.url),
title: element.textContent.trim()
};
});
}

async _getMangaFromURI(uri) {
const request = new Request(uri, this.requestOptions);
const data = await this.fetchDOM(request, 'td:nth-child(2) tr:nth-child(3) > td:nth-child(2) > a:last-child', 0, 'big5');
const id = uri.pathname + uri.search;
const title = data[0].textContent.trim();
return new Manga(this, id, title);
}

async _getChapters(manga) {
const request = new Request(new URL(manga.id, this.url), this.requestOptions);
const data = await this.fetchDOM(request, '#info td > a', 0, 'big5');
return data.map(element => {
return {
id: this.getRootRelativeOrAbsoluteLink(element, this.url),
title: element.textContent.trim()
};
});
}

async _getPages(chapter) {
const request = new Request(new URL(chapter.id, this.url), this.requestOptions);
const data = await this.fetchDOM(request, 'body');
const maxpage = parseInt(data[0].querySelector('a:nth-last-of-type(2).pages').textContent);
const pageone = data[0].querySelector('a > source[oncontextmenu]').src;
return [...new Array(maxpage)].map((_, int) => pageone.replace(/(\d+)$/, String(int+1).padStart(3, '0')));
}
}
6 changes: 3 additions & 3 deletions src/web/mjs/engine/Connector.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export default class Connector {
* Get the content for the given Request
* and get all elements matching the given CSS selector.
*/
fetchDOM( request, selector, retries ) {
fetchDOM( request, selector, retries, encoding ) {
retries = retries || 0;
if( typeof request === 'string' ) {
request = new Request( request, this.requestOptions );
Expand All @@ -444,9 +444,9 @@ export default class Connector {
.then( () => this.fetchDOM( request, selector, retries - 1 ) );
}
if( response.status === 200 ) {
return response.text()
return response.arrayBuffer()
.then( data => {
let dom = this.createDOM( data );
let dom = this.createDOM( new TextDecoder(encoding || 'utf8').decode(data) );
return Promise.resolve( !selector ? dom : [...dom.querySelectorAll( selector )] );
} );
}
Expand Down

0 comments on commit 6f247ba

Please sign in to comment.