-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e111cf
commit afd79a6
Showing
2 changed files
with
153 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,146 @@ | ||
import type TypeNormalValueParser from "src/apis/parser/[type_normal]/[value]" | ||
import { useCache } from "src/apis/useCache" | ||
import Worker from "src/apis/workers/[type_normal]/[value]?worker" | ||
import { PostWorker } from "src/apis/wrap-worker" | ||
import { get } from "src/logic/http" | ||
|
||
export async function TypeNormalValue( | ||
type: string, | ||
value: string | string[], | ||
page = 1, | ||
onlyItems: boolean, | ||
options?: { | ||
genres: string[] | ||
seaser: string | null | ||
sorter: string | null | ||
typer: string | null | ||
year: string | null | ||
}, | ||
defaultsOptions: { | ||
genres?: string | ||
seaser?: string | ||
typer?: string | ||
year?: string | ||
} = {} | ||
import { getInfoTPost } from "../__helpers__/getInfoTPost" | ||
import { parserDom } from "../__helpers__/parserDom" | ||
|
||
export default function TypeNormalValue( | ||
html: string, | ||
now: number, | ||
onlyItems: boolean | ||
) { | ||
const isCustom = | ||
options && | ||
(options.genres.length > 1 || | ||
options.seaser || | ||
options.typer || | ||
options.year || | ||
/* exclude */ type === "danh-sach") | ||
|
||
const url = isCustom | ||
? [ | ||
"/danh-sach", | ||
options.typer ?? defaultsOptions.typer ?? "all", | ||
options.genres | ||
.concat( | ||
defaultsOptions.typer && defaultsOptions.typer !== "all" | ||
? [defaultsOptions.typer] | ||
: [] | ||
) | ||
.join("-") || "all", | ||
options.seaser ?? defaultsOptions.seaser ?? "all", | ||
options.year ?? defaultsOptions.year ?? "all", | ||
`/trang-${page}.html` | ||
].join("/") + (options.sorter ? "?sort=" + options.sorter : "") | ||
: `/${type}/${ | ||
Array.isArray(value) ? value.join("/") : value | ||
}/trang-${page}.html` + (options?.sorter ? "?sort=" + options.sorter : "") | ||
|
||
return await useCache(url, async () => { | ||
const { data: html } = await get(url) | ||
|
||
const now = Date.now() | ||
|
||
return PostWorker<typeof TypeNormalValueParser>( | ||
Worker, | ||
html, | ||
now, | ||
onlyItems | ||
) | ||
}) | ||
const $ = parserDom(html) | ||
|
||
const items = $(".MovieList:eq(0)") | ||
.find(".TPostMv") | ||
.map((_i, item) => getInfoTPost($(item), now)) | ||
.toArray() | ||
const curPage = parseInt( | ||
$(".current").attr("data") ?? $(".current").attr("title") ?? "1" | ||
) | ||
const maxPage = parseInt( | ||
$(".larger:last-child, .wp-pagenavi > *:last-child").attr("data") ?? | ||
$(".larger:last-child, .wp-pagenavi > *:last-child").attr("title") ?? | ||
"1" | ||
) | ||
|
||
if (onlyItems) { | ||
return { items, curPage, maxPage } | ||
} | ||
|
||
const title = $(".breadcrumb:eq(0) > li") | ||
.slice(1) | ||
.map((_i, item) => $(item).text().trim()) | ||
.toArray() | ||
.join(" ") | ||
.replace(/:/g, "") | ||
.replace(/^Danh sách /i, "") | ||
|
||
const filter = { | ||
sorter: $("#filter") | ||
.find(".fc-main-list > li > a") | ||
.map((_i, anchor) => { | ||
const text = $(anchor).text() | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const value = new URL( | ||
$(anchor).attr("href") ?? "", | ||
"https://example.com" | ||
).searchParams.get("sort")! | ||
return { text, value } | ||
}) | ||
.toArray(), | ||
typer: $("#filter") | ||
.find(".fc-filmtype label") | ||
.map((_i, item) => { | ||
const $item = $(item) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const value = $item.find("input").attr("value")! | ||
const checked = !!$item.attr("checked") | ||
|
||
// eslint-disable-next-line array-callback-return | ||
if (value === "all") return | ||
|
||
const text = $(item).text() | ||
|
||
return { | ||
text, | ||
value, | ||
checked | ||
} | ||
}) | ||
.toArray() | ||
.filter(Boolean), | ||
seaser: $("#filter") | ||
.find(".fc-quality label") | ||
.map((_i, item) => { | ||
const $item = $(item) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const value = $item.find("input").attr("value")! | ||
const checked = !!$item.attr("checked") | ||
|
||
// eslint-disable-next-line array-callback-return | ||
if (value === "all") return | ||
|
||
const text = $(item).text() | ||
|
||
return { | ||
text, | ||
value, | ||
checked | ||
} | ||
}) | ||
.toArray() | ||
.filter(Boolean), | ||
gener: $("#filter") | ||
.find(".fc-genre label") | ||
.map((_i, item) => { | ||
const $item = $(item) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const value = $(item).find("input").attr("value")! | ||
const checked = !!$item.attr("checked") | ||
|
||
// eslint-disable-next-line array-callback-return | ||
if (value === "all") return | ||
|
||
const text = $(item).text() | ||
|
||
return { | ||
text, | ||
value, | ||
checked | ||
} | ||
}) | ||
.toArray() | ||
.filter(Boolean), | ||
year: $("#filter") | ||
.find(".fc-release label") | ||
.map((_i, item) => { | ||
const $item = $(item) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const value = $(item).find("input").attr("value")! | ||
const checked = !!$item.attr("checked") | ||
|
||
// eslint-disable-next-line array-callback-return | ||
if (value === "all") return | ||
|
||
const text = $(item).text() | ||
|
||
return { | ||
text, | ||
value, | ||
checked | ||
} | ||
}) | ||
.toArray() | ||
.filter(Boolean) | ||
} | ||
|
||
return { | ||
title, | ||
items, | ||
curPage, | ||
maxPage, | ||
filter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters