Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions plugins/multisrc/madara/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export const generateAll = function () {
});
};

const madaraTemplate = readFileSync(join(folder, 'template.ts'), {
encoding: 'utf-8',
});

const generator = function generator(source) {
const madaraTemplate = readFileSync(join(folder, 'template.ts'), {
encoding: 'utf-8',
});

const pluginScript = `
${madaraTemplate.replace('// CustomJS HERE', source.options?.customJs || '')}
Expand Down
12 changes: 4 additions & 8 deletions plugins/multisrc/madara/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@
"sourceName": "Foxaholic",
"options": {
"useNewChapterEndpoint": true,
"resolveCoverDataUri": true,
"versionIncrements": 1
"resolveCoverDataUri": true
}
},
{
Expand Down Expand Up @@ -414,8 +413,7 @@
"sourceName": "Belle Repository",
"options": {
"useNewChapterEndpoint": true,
"lang": "English",
"versionIncrements": 1
"lang": "English"
}
},
{
Expand Down Expand Up @@ -632,8 +630,7 @@
"sourceName": "BoxNovel",
"options": {
"lang": "English",
"useNewChapterEndpoint": true,
"versionIncrements": 2
"useNewChapterEndpoint": true
}
},
{
Expand All @@ -660,8 +657,7 @@
"sourceName": "Lovelyblossoms",
"options": {
"useNewChapterEndpoint": true,
"browserHeaders": true,
"versionIncrements": 1
"browserHeaders": true
}
},
{
Expand Down
132 changes: 79 additions & 53 deletions plugins/multisrc/madara/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MadaraPlugin implements Plugin.PluginBase {
this.icon = `multisrc/madara/${metadata.id.toLowerCase()}/icon.png`;
this.site = metadata.sourceSite;
const versionIncrements = metadata.options?.versionIncrements || 0;
this.version = `2.2.${versionIncrements}`;
this.version = `2.3.${versionIncrements}`;
this.options = metadata.options;
this.filters = metadata.filters;

Expand Down Expand Up @@ -167,17 +167,12 @@ export class MadaraPlugin implements Plugin.PluginBase {

loadedCheerio('.page-item-detail, .c-tabs-item__content').each(
(index, element) => {
const novelName = loadedCheerio(element)
.find('.post-title')
.text()
.trim();
const novelUrl =
loadedCheerio(element).find('.post-title').find('a').attr('href') ||
'';
const item = loadedCheerio(element);
const postTitle = item.find('.post-title');
const novelName = postTitle.text().trim();
const novelUrl = postTitle.find('a').attr('href') || '';
if (!novelName || !novelUrl) return;
const novelCover = this.extractCover(
loadedCheerio(element).find('img'),
);
const novelCover = this.extractCover(item.find('img'));

const novel: Plugin.NovelItem = {
name: novelName,
Expand Down Expand Up @@ -280,9 +275,9 @@ export class MadaraPlugin implements Plugin.PluginBase {

loadedCheerio('.post-content_item, .post-content').each(function () {
const detailName = loadedCheerio(this).find('h5').text().trim();
const detail =
loadedCheerio(this).find('.summary-content') ||
loadedCheerio(this).find('.summary_content');
const detail = loadedCheerio(this).find(
'.summary-content, .summary_content',
);

switch (detailName) {
case 'Genre(s)':
Expand Down Expand Up @@ -412,16 +407,32 @@ export class MadaraPlugin implements Plugin.PluginBase {
const queryIndex = lastHref.indexOf('?');
if (queryIndex !== -1) {
const queryTemplate = lastHref.slice(queryIndex).replace(/\d+$/, '');
const pages: number[] = [];
for (let page = 2; page <= maxPage; page++) {
const pageHtml = await fetchApi(
this.site + novelPath + 'ajax/chapters/' + queryTemplate + page,
{
method: 'POST',
headers: this.requestHeaders,
referrer: this.site + novelPath,
},
).then((res: Response) => res.text());
if (pageHtml && pageHtml !== '0') html += pageHtml;
pages.push(page);
}
const BATCH_SIZE = 5;
for (let i = 0; i < pages.length; i += BATCH_SIZE) {
const batch = pages.slice(i, i + BATCH_SIZE);
const batchHtmls = await Promise.all(
batch.map(page =>
fetchApi(
this.site +
novelPath +
'ajax/chapters/' +
queryTemplate +
page,
{
method: 'POST',
headers: this.requestHeaders,
referrer: this.site + novelPath,
},
).then((res: Response) => res.text()),
),
);
for (const pageHtml of batchHtmls) {
if (pageHtml && pageHtml !== '0') html += pageHtml;
}
}
}
}
Expand All @@ -447,25 +458,25 @@ export class MadaraPlugin implements Plugin.PluginBase {
}

const totalChapters = loadedCheerio('.wp-manga-chapter').length;
const defaultDate = dayjs().format('LL');
loadedCheerio('.wp-manga-chapter').each((chapterIndex, element) => {
let chapterName = loadedCheerio(element).find('a').text().trim();
const link = loadedCheerio(element).find('a');
let chapterName = link.text().trim();
const locked = element.attribs['class'].includes('premium-block');
if (locked) {
chapterName = '🔒 ' + chapterName;
}

let releaseDate = loadedCheerio(element)
const rawReleaseDate = loadedCheerio(element)
.find('span.chapter-release-date')
.text()
.trim();

if (releaseDate) {
releaseDate = this.parseData(releaseDate);
} else {
releaseDate = dayjs().format('LL');
}
const releaseDate = rawReleaseDate
? this.parseData(rawReleaseDate)
: defaultDate;

const chapterUrl = loadedCheerio(element).find('a').attr('href') || '';
const chapterUrl = link.attr('href') || '';

if (chapterUrl && chapterUrl != '#' && !(locked && this.hideLocked)) {
chapters.push({
Expand All @@ -483,11 +494,25 @@ export class MadaraPlugin implements Plugin.PluginBase {

async parseChapter(chapterPath: string): Promise<string> {
const loadedCheerio = await this.getCheerio(this.site + chapterPath, false);
const chapterText =
loadedCheerio('.text-left') ||
loadedCheerio('.text-right') ||
loadedCheerio('.entry-content') ||
loadedCheerio('.c-blog-post > div > div:nth-child(2)');
const candidates = [
'.text-left',
'.text-right',
'.entry-content',
'.c-blog-post > div > div:nth-child(2)',
'.reading-content',
];
let chapterText: Cheerio<AnyNode> = loadedCheerio('.entry-content');
for (const selector of candidates) {
const el = loadedCheerio(selector);
if (el.length > 0) {
chapterText = el;
break;
}
}

chapterText
.find('script, style, noscript, .code-block, .adsbygoogle')
.remove();

if (this.options?.customJs) {
try {
Expand Down Expand Up @@ -518,14 +543,17 @@ export class MadaraPlugin implements Plugin.PluginBase {
}

parseData = (date: string) => {
let dayJSDate = dayjs(); // today
const timeAgo = date.match(/\d+/)?.[0] || '';
const timeAgoInt = parseInt(timeAgo, 10);
const timeAgo = date.match(/\d+/)?.[0];
if (!timeAgo) {
const formatted = dayjs(date).format('LL');
return formatted !== 'Invalid Date' ? formatted : date;
}

if (!timeAgo) return date; // there is no number!
const timeAgoInt = parseInt(timeAgo, 10);
let dayJSDate = dayjs();

if (includesAny(date, ['detik', 'segundo', 'second', 'วินาที'])) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'second'); // go back N seconds
dayJSDate = dayJSDate.subtract(timeAgoInt, 'second');
} else if (
includesAny(date, [
'menit',
Expand All @@ -537,7 +565,7 @@ export class MadaraPlugin implements Plugin.PluginBase {
'دقائق',
])
) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'minute'); // go back N minute
dayJSDate = dayJSDate.subtract(timeAgoInt, 'minute');
} else if (
includesAny(date, [
'jam',
Expand All @@ -552,34 +580,32 @@ export class MadaraPlugin implements Plugin.PluginBase {
'小时',
])
) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'hours'); // go back N hours
dayJSDate = dayJSDate.subtract(timeAgoInt, 'hours');
} else if (
includesAny(date, [
'hari',
'gün',
'jour',
'día',
'ngày',
'dia',
'día',
'day',
'วัน',
'ngày',
'giorni',
'أيام',
'天',
])
) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'days'); // go back N days
dayJSDate = dayJSDate.subtract(timeAgoInt, 'days');
} else if (includesAny(date, ['week', 'semana'])) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'week'); // go back N a week
dayJSDate = dayJSDate.subtract(timeAgoInt, 'week');
} else if (includesAny(date, ['month', 'mes'])) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'month'); // go back N months
dayJSDate = dayJSDate.subtract(timeAgoInt, 'month');
} else if (includesAny(date, ['year', 'año'])) {
dayJSDate = dayJSDate.subtract(timeAgoInt, 'year'); // go back N years
dayJSDate = dayJSDate.subtract(timeAgoInt, 'year');
} else {
if (dayjs(date).format('LL') !== 'Invalid Date') {
return dayjs(date).format('LL');
}
return date;
const formatted = dayjs(date).format('LL');
return formatted !== 'Invalid Date' ? formatted : date;
}

return dayJSDate.format('LL');
Expand Down
Loading