Skip to content

Commit

Permalink
chore: add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tachibana-shin committed Oct 26, 2023
1 parent d6cb984 commit d72ca98
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
6 changes: 4 additions & 2 deletions plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ class Plugin implements API {
async getCategory(type: string, page: number) {
const match = type.match(/-(\d+)$/)

if (!match) throw new Error("not_found")
if (type && !match) throw new Error("not_found")

return general(
`the-loai-${type.slice(0, match.index)}-${match[1]}.html`,
!type
? "danh-sach.html"
: `the-loai-${type.slice(0, match!.index)}-${match![1]}.html`,
page
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ export const Rankings: Ranking[] = [
},
{
value: "week",
match: "danh-sach.html?sort=2",
match: "danh-sach.html?sort=1",
name: {
vi: "Tuần"
}
},
{
value: "month",
match: "danh-sach.html?sort=3",
match: "danh-sach.html?sort=2",
name: { vi: "Tháng" }
},
{
value: "all",
match: "danh-sach.html?sort=4",
match: "danh-sach.html?sort=3",
name: { vi: "Tất" }
},
{
value: "hot",
match: "danh-sach.html?sort=5",
match: "danh-sach.html?sort=4",
name: { vi: "Thịnh hành" }
}
]
Expand Down
76 changes: 64 additions & 12 deletions src/fetch/general.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { General } from "raiku-pgs/plugin"
import type { FilterQuery, FilterURI, General } from "raiku-pgs/plugin"

export function parserGeneral(html: string): General {
// eslint-disable-next-line functional/no-throw-statements
Expand Down Expand Up @@ -34,23 +34,75 @@ export async function general(path: string, page: number) {
let cookie: string | undefined
if (url.searchParams.get("sort")) {
cookie =
// eslint-disable-next-line no-sparse-arrays
[
"view=0;view0=1;view2=0;view3=0;view4=0;view5=0",,

"view=0;view0=0;view2=0;view3=0;view4=0;view5=1",
"view=0;view0=0;view2=1;view3=0;view4=0;view5=0",
"view=0;view0=0;view2=0;view3=1;view4=0;view5=0",
"view=0;view0=0;view2=0;view3=0;view4=1;view5=0",
"view=0;view0=0;view2=0;view3=0;view4=0;view5=1"
"view=1;view0=0;view2=0;view3=0;view4=0;view5=0",
"view=0;view0=1;view2=0;view3=0;view4=0;view5=0"
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
][parseInt(url.searchParams.get("sort") + "")] ??
"view=1;view0=0;view2=0;view3=0;view4=0;view5=0"
"view=0;view0=0;view2=0;view3=0;view4=0;view5=1"
}
// now scam
const { data } = await get({
url: url.href,
headers: cookie ? { "c-cookie": cookie } : undefined
})
const [general, filters] = await Promise.all([
get({
url: url.href,
headers: cookie ? { "c-cookie": cookie } : undefined
}).then(({ data }) => parserGeneral(data)),
post({
url: `${CURL}/tag_box.php`,
data: {
tagid: "1"
}
}).then(({ data }): General["filters"] => {
const $ = parseDom(data)

return [
<FilterURI>{
type: "Thể loại",
select: $("a")
.toArray()
.map((item) => {
const $item = $(item)

const name = $item.text().trim()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const route = parseRouteGenre($item.attr("href")!)

return { name, route }
})
},
<FilterQuery>{
type: "Sắp xếp",
key: "sort",
items: [
{
name: "Ngày",
value: "0"
},
{
name: "Tuần",
value: "1"
},
{
name: "Tháng",
value: "2"
},
{
name: "Tất",
value: "3"
},
{
name: "Thịnh hành",
value: "4"
}
]
}
]
})
])

return parserGeneral(data)
// eslint-disable-next-line functional/immutable-data
return Object.assign(general, { filters })
}

0 comments on commit d72ca98

Please sign in to comment.