Skip to content

Commit

Permalink
chore: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
zcrossoverz committed Jun 27, 2023
1 parent df9080d commit 7dc7487
Show file tree
Hide file tree
Showing 10 changed files with 1,650 additions and 1,731 deletions.
14 changes: 7 additions & 7 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"endOfLine": "auto"
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"endOfLine": "auto"
}
216 changes: 106 additions & 110 deletions src/hooks/getDataChapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,127 +7,123 @@ import { not_null } from '../utils/validate';
import { chapter, image_chapter, responseChapter } from '../types/type';

interface paramsSelector {
puppeteer?: Page;
cheerioApi?: CheerioAPI;
mainContentSelector: string;
baseUrl: string;
url: string;
prev_chapter?: string;
next_chapter?: string;
titleSelector: string;
imageSelectorAll: string;
originImageAttr: string;
cdnImageAttr?: string;
prevChapterSelector: string;
nextChapterSelector: string;
puppeteer?: Page;
cheerioApi?: CheerioAPI;
mainContentSelector: string;
baseUrl: string;
url: string;
prev_chapter?: string;
next_chapter?: string;
titleSelector: string;
imageSelectorAll: string;
originImageAttr: string;
cdnImageAttr?: string;
prevChapterSelector: string;
nextChapterSelector: string;
}

export const useGetDataChapter = async (
params: paramsSelector
params: paramsSelector
): Promise<responseChapter> => {
const {
puppeteer,
cheerioApi,
url,
prev_chapter,
next_chapter,
baseUrl,
mainContentSelector,
titleSelector,
imageSelectorAll,
originImageAttr,
cdnImageAttr,
prevChapterSelector,
nextChapterSelector,
} = params;
const {
puppeteer,
cheerioApi,
url,
prev_chapter,
next_chapter,
baseUrl,
mainContentSelector,
titleSelector,
imageSelectorAll,
originImageAttr,
cdnImageAttr,
prevChapterSelector,
nextChapterSelector,
} = params;

if (cheerioApi === undefined) {
const content = await puppeteer!.$(mainContentSelector);
if (cheerioApi === undefined) {
const content = await puppeteer!.$(mainContentSelector);

const title = not_null(
await content!.$eval(titleSelector, (el) => el.textContent)
const title = not_null(
await content!.$eval(titleSelector, (el) => el.textContent)
);
const images: image_chapter[] = await Promise.all(
(
await content!.$$(`${imageSelectorAll}`)
).map(async (e, i) => {
const _data_image = await e.evaluate(
(el, originImageAttr, cdnImageAttr) => {
return {
src_origin: el.getAttribute(originImageAttr),
...(cdnImageAttr
? { src_cdn: el.getAttribute(cdnImageAttr) }
: {}),
alt: el.getAttribute('alt'),
};
},
originImageAttr,
cdnImageAttr
);
const images: image_chapter[] = await Promise.all(
(
await content!.$$(`${imageSelectorAll}`)
).map(async (e, i) => {
const _data_image = await e.evaluate(
(el, originImageAttr, cdnImageAttr) => {
return {
src_origin: el.getAttribute(originImageAttr),
...(cdnImageAttr
? { src_cdn: el.getAttribute(cdnImageAttr) }
: {}),
alt: el.getAttribute('alt'),
};
},
originImageAttr,
cdnImageAttr
);
return {
_id: i,
src_origin: not_null(_data_image.src_origin).startsWith(
'//'
)
? `https:${not_null(_data_image.src_origin)}`
: not_null(_data_image.src_origin),
...(not_null(_data_image.src_cdn) !== ''
? {
src_cdn: not_null(_data_image.src_cdn).startsWith(
'//'
)
? `https:${not_null(_data_image.src_cdn)}`
: not_null(_data_image.src_cdn),
}
: {}),
alt: not_null(_data_image.alt),
};
})
);
const prev: chapter = {} as chapter;

if (prev_chapter === undefined) {
const prev_chapter_get = await content!
.$eval(prevChapterSelector, (el) => {
return {
url_chapter: el.getAttribute('href'),
};
})
.catch(() => null);
return {
_id: i,
src_origin: not_null(_data_image.src_origin).startsWith('//')
? `https:${not_null(_data_image.src_origin)}`
: not_null(_data_image.src_origin),
...(not_null(_data_image.src_cdn) !== ''
? {
src_cdn: not_null(_data_image.src_cdn).startsWith('//')
? `https:${not_null(_data_image.src_cdn)}`
: not_null(_data_image.src_cdn),
}
: {}),
alt: not_null(_data_image.alt),
};
})
);
const prev: chapter = {} as chapter;

if (prev_chapter_get !== null) {
prev.url = not_null(prev_chapter_get?.url_chapter);
prev.parent_href = url;
prev.path = url.substring(`${baseUrl}`.length);
}
}
if (prev_chapter === undefined) {
const prev_chapter_get = await content!
.$eval(prevChapterSelector, (el) => {
return {
url_chapter: el.getAttribute('href'),
};
})
.catch(() => null);

const next: chapter = {} as chapter;
if (prev_chapter_get !== null) {
prev.url = not_null(prev_chapter_get?.url_chapter);
prev.parent_href = url;
prev.path = url.substring(`${baseUrl}`.length);
}
}

if (next_chapter === undefined) {
const next_chapter_get = await content!
.$eval(nextChapterSelector, (el) => {
return {
url_chapter: el.getAttribute('href'),
t: el.outerHTML,
};
})
.catch(() => null);
const next: chapter = {} as chapter;

if (next_chapter_get !== null) {
next.url = not_null(next_chapter_get?.url_chapter);
next.parent_href = url;
next.path = url.substring(`${baseUrl}`.length);
}
}
if (next_chapter === undefined) {
const next_chapter_get = await content!
.$eval(nextChapterSelector, (el) => {
return {
url_chapter: el.getAttribute('href'),
t: el.outerHTML,
};
})
.catch(() => null);

return {
title,
chapter_data: images,
next_chapter: Object.keys(next).length === 0 ? null : next,
prev_chapter: Object.keys(prev).length === 0 ? null : prev,
};
} else {
throw new Error('not yet define');
if (next_chapter_get !== null) {
next.url = not_null(next_chapter_get?.url_chapter);
next.parent_href = url;
next.path = url.substring(`${baseUrl}`.length);
}
}

return {
title,
chapter_data: images,
next_chapter: Object.keys(next).length === 0 ? null : next,
prev_chapter: Object.keys(prev).length === 0 ? null : prev,
};
} else {
throw new Error('not yet define');
}
};
Loading

0 comments on commit 7dc7487

Please sign in to comment.