plugin-books-pro
Version:
[](https://badge.fury.io/js/plugin-books-pro)
90 lines (89 loc) • 4.89 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const t2_browser_worker_1 = require("t2-browser-worker");
const base_1 = require("../models/base");
class Mangadex extends base_1.BaseBook {
constructor() {
super(...arguments);
this.API_ENDPOINTS = {
TOP: `https://api.${this.domain}/manga?limit=${this.LIMIT_ITEMS}&offset=0&includes[]=cover_art&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&order[followedCount]=desc&includedTagsMode=AND&excludedTagsMode=OR`,
FAVORITE: `https://api.${this.domain}/manga?limit=${this.LIMIT_ITEMS}&offset=0&includes[]=cover_art&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&order[rating]=desc&includedTagsMode=AND&excludedTagsMode=OR`,
NEW: `https://api.${this.domain}/manga?limit=${this.LIMIT_ITEMS}&offset=0&includes[]=cover_art&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&order[createdAt]=desc&includedTagsMode=AND&excludedTagsMode=OR`,
DETAIL: `https://api.${this.domain}/statistics/manga`,
};
}
transformBookData(book, stat, index) {
var _a, _b, _c, _d, _e;
const title = book.attributes.title.en || Object.values(book.attributes.title)[0];
const imageName = (_a = book.relationships.find((item) => item.type === "cover_art")) === null || _a === void 0 ? void 0 : _a.attributes.fileName;
return {
rank: index + 1,
identifier: this.getIdentifier(title),
name: title,
link: `/title/${book.id}/${book.attributes.links.ap}`,
imageUrlThumbnail: `${this.baseUrl}/covers/${book.id}/${imageName}.256.jpg`,
follow: stat.follows,
comment: (_c = (_b = stat.comments) === null || _b === void 0 ? void 0 : _b.repliesCount) !== null && _c !== void 0 ? _c : 0,
rating: ((_d = stat.rating) === null || _d === void 0 ? void 0 : _d.average) ? (stat.rating.average / 10) * 5 : 0,
tags: book.attributes.tags
.map((tag) => tag.attributes.name.en)
.filter(Boolean)
.join(","),
description: ((_e = book.attributes.description) === null || _e === void 0 ? void 0 : _e.en) ||
Object.values(book.attributes.description || {})[0] ||
"",
lastChapter: book.attributes.lastChapter,
};
}
getInfoABook(page, url, dataType) {
return __awaiter(this, void 0, void 0, function* () {
try {
const result = {
dataType,
data: [],
status: "SUCCESS",
};
const { data } = yield (0, t2_browser_worker_1.usePageFetch)(page, url);
if (!(data === null || data === void 0 ? void 0 : data.length)) {
return this.ErrorIResponseListBook(dataType);
}
const mangaIds = data.map((item) => item.id);
const statsUrl = `${this.API_ENDPOINTS.DETAIL}?${mangaIds
.map((id) => `manga[]=${id}`)
.join("&")}`;
const { statistics } = yield (0, t2_browser_worker_1.usePageFetch)(page, statsUrl);
result.data = data.map((book, index) => this.transformBookData(book, statistics[book.id], index));
return result;
}
catch (error) {
console.error(`Error fetching ${dataType} books:`, error);
return this.ErrorIResponseListBook(dataType);
}
});
}
getNew(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.getInfoABook(page, this.API_ENDPOINTS.NEW, "New");
});
}
getFavorite(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.getInfoABook(page, this.API_ENDPOINTS.FAVORITE, "Favorite");
});
}
getTop(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.getInfoABook(page, this.API_ENDPOINTS.TOP, "Top");
});
}
}
exports.default = Mangadex;