Skip to content

Commit

Permalink
linovelib fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngfish3r committed Oct 29, 2023
1 parent f4892db commit 1eded4d
Showing 1 changed file with 59 additions and 60 deletions.
119 changes: 59 additions & 60 deletions src/sources/ch/linovelib.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ const parseNovelAndChapters = async novelUrl => {
};

const parseChapter = async (novelUrl, chapterUrl) => {
let chapterName, chapterText, hasNextPage;
let pageNumber = 1;
const body = await fetchHtml({ url: chapterUrl, sourceId });
const pageCheerio = cheerio.load(body);
let pageText;
// let hasNextPage, pageNumber = 1;

/*
* TODO: Maybe there are other ways to get the translation table
Expand Down Expand Up @@ -244,67 +246,64 @@ const parseChapter = async (novelUrl, chapterUrl) => {
'': '裸',
};

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 addPage = async pageCheerio => {

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

// Load lazyloaded images
pageCheerio('#ccacontent 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');
}
});

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 };
// Recover the original character
pageText = pageCheerio('#ccacontent').html();
pageText = pageText.replace(/./g, char => mapping_dict[char] || char);

return Promise.resolve();
};

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);
await formatPage();
const chapterName =
pageCheerio('#atitle + h3').text() + ' — ' + pageCheerio('#atitle').text();
const chapterText = pageText;
// };

// 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 1eded4d

Please sign in to comment.