Skip to content

Commit

Permalink
fix(source): add all pages in the same chapter for Linovelib (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair3149 authored Aug 2, 2023
1 parent d260eb1 commit e760bd1
Showing 1 changed file with 66 additions and 35 deletions.
101 changes: 66 additions & 35 deletions src/sources/ch/linovelib.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,13 @@ const parseNovelAndChapters = async novelUrl => {
};

const parseChapter = async (novelUrl, chapterUrl) => {
const url = chapterUrl;
const body = await fetchHtml({ url, sourceId });

const loadedCheerio = cheerio.load(body);

// Remove JS
loadedCheerio('#acontent .cgo').remove();

// Load lazyloaded images
loadedCheerio('#acontent img.imagecontent').each(function () {
// Sometimes images are either in data-src or src
const imgSrc =
loadedCheerio(this).attr('data-src') || loadedCheerio(this).attr('src');
if (imgSrc) {
// The original CDN URL is locked behind a CF-like challenge, switch the URL to bypass that
// There are no react-native-url-polyfill lib, can't use URL API
const regex = /\/\/.+\.com\//;
const imgUrl = imgSrc.replace(regex, '//img.linovelib.com/');
// Clean up img element
loadedCheerio(this)
.attr('src', imgUrl)
.removeAttr('data-src')
.removeClass('lazyload');
}
});

// Recover the original character
let chapterText = loadedCheerio('#acontent').html();
let chapterName, chapterText, hasNextPage;
let pageNumber = 1;

/*
* TODO: Maybe there are other ways to get the translation table
* It is embed and encrypted inside readtool.js
*/
const mapping_dict = {
'“': '「',
'’': '』',
Expand Down Expand Up @@ -266,14 +244,67 @@ const parseChapter = async (novelUrl, chapterUrl) => {
'': '裸',
};

Object.entries(mapping_dict).forEach(([key, value]) => {
chapterText = chapterText.replaceAll(key, value);
});
const addPage = async pageCheerio => {
let pageText;

const formatPage = async () => {
// Remove JS
pageCheerio('#acontent .cgo').remove();

// Load lazyloaded images
pageCheerio('#acontent img.imagecontent').each(function () {
// Sometimes images are either in data-src or src
const imgSrc =
pageCheerio(this).attr('data-src') || pageCheerio(this).attr('src');
if (imgSrc) {
// The original CDN URL is locked behind a CF-like challenge, switch the URL to bypass that
// There are no react-native-url-polyfill lib, can't use URL API
const regex = /\/\/.+\.com\//;
const imgUrl = imgSrc.replace(regex, '//img.linovelib.com/');
// Clean up img element
pageCheerio(this)
.attr('src', imgUrl)
.removeAttr('data-src')
.removeClass('lazyload');
}
});

// Recover the original character
pageText = pageCheerio('#acontent').html();
pageText = pageText.replace(/./g, char => mapping_dict[char] || char);

return Promise.resolve();
};

await formatPage();
chapterName ??=
pageCheerio('#atitle + h3').text() +
' — ' +
pageCheerio('#atitle').text();
chapterText += pageText;
};

const chapterName =
loadedCheerio('#atitle + h3').text() +
' — ' +
loadedCheerio('#atitle').text();
const loadPage = async url => {
const body = await fetchHtml({ url, sourceId });
const pageCheerio = cheerio.load(body);
addPage(pageCheerio);
pageHasNextPage =
pageCheerio('#footlink > a[onclick$=ReadParams.url_next;]').text() ===
'下一页'
? true
: false;
return { pageCheerio, pageHasNextPage };
};

let url = chapterUrl;
do {
const page = await loadPage(url);
hasNextPage = page.pageHasNextPage;
if (hasNextPage === true) {
pageNumber++;
url = chapterUrl.replace(/\.html/gi, `_${pageNumber}` + '.html');
}
} while (hasNextPage === true);

const chapter = {
sourceId,
Expand Down

0 comments on commit e760bd1

Please sign in to comment.