Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(source): add all pages in the same chapter for Linovelib #722

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
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