feat(advancedSearch): add advanced search feature
This commit is contained in:
@@ -3,18 +3,22 @@ import {
|
|||||||
ACCEPT_HEADER,
|
ACCEPT_HEADER,
|
||||||
USER_AGENT_HEADER,
|
USER_AGENT_HEADER,
|
||||||
ACCEPT_ENCODING_HEADER,
|
ACCEPT_ENCODING_HEADER,
|
||||||
extractMostPopularAnimes,
|
|
||||||
extractAnimes,
|
extractAnimes,
|
||||||
|
getSearchFilterValue,
|
||||||
|
extractMostPopularAnimes,
|
||||||
|
getSearchDateFilterValue,
|
||||||
} from "../utils/index.js";
|
} from "../utils/index.js";
|
||||||
import axios, { AxiosError } from "axios";
|
import axios, { AxiosError } from "axios";
|
||||||
import createHttpError, { type HttpError } from "http-errors";
|
import createHttpError, { type HttpError } from "http-errors";
|
||||||
import { load, type CheerioAPI, type SelectorType } from "cheerio";
|
import { load, type CheerioAPI, type SelectorType } from "cheerio";
|
||||||
import type { ScrapedAnimeSearchResult } from "../types/parsers/index.js";
|
import type { ScrapedAnimeSearchResult } from "../types/parsers/index.js";
|
||||||
|
import type { SearchFilters, FilterKeys } from "../types/controllers/index.js";
|
||||||
|
|
||||||
// /anime/search?q=${query}&page=${page}
|
// /anime/search?q=${query}&page=${page}
|
||||||
async function scrapeAnimeSearch(
|
async function scrapeAnimeSearch(
|
||||||
q: string,
|
q: string,
|
||||||
page: number = 1
|
page: number = 1,
|
||||||
|
filters: SearchFilters
|
||||||
): Promise<ScrapedAnimeSearchResult | HttpError> {
|
): Promise<ScrapedAnimeSearchResult | HttpError> {
|
||||||
const res: ScrapedAnimeSearchResult = {
|
const res: ScrapedAnimeSearchResult = {
|
||||||
animes: [],
|
animes: [],
|
||||||
@@ -22,19 +26,45 @@ async function scrapeAnimeSearch(
|
|||||||
currentPage: Number(page),
|
currentPage: Number(page),
|
||||||
hasNextPage: false,
|
hasNextPage: false,
|
||||||
totalPages: 1,
|
totalPages: 1,
|
||||||
|
searchQuery: q,
|
||||||
|
searchFilters: filters,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const mainPage = await axios.get(
|
const url = new URL(SRC_SEARCH_URL);
|
||||||
`${SRC_SEARCH_URL}?keyword=${q}&page=${page}`,
|
url.searchParams.set("keyword", q);
|
||||||
{
|
url.searchParams.set("page", `${page}`);
|
||||||
|
url.searchParams.set("sort", "default");
|
||||||
|
|
||||||
|
for (const key in filters) {
|
||||||
|
if (key.includes("_date")) {
|
||||||
|
const dates = getSearchDateFilterValue(
|
||||||
|
key === "start_date",
|
||||||
|
filters[key as keyof SearchFilters] || ""
|
||||||
|
);
|
||||||
|
if (!dates) continue;
|
||||||
|
|
||||||
|
dates.map((dateParam) => {
|
||||||
|
const [key, val] = dateParam.split("=");
|
||||||
|
url.searchParams.set(key, val);
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filterVal = getSearchFilterValue(
|
||||||
|
key as FilterKeys,
|
||||||
|
filters[key as keyof SearchFilters] || ""
|
||||||
|
);
|
||||||
|
filterVal && url.searchParams.set(key, filterVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mainPage = await axios.get(url.href, {
|
||||||
headers: {
|
headers: {
|
||||||
"User-Agent": USER_AGENT_HEADER,
|
"User-Agent": USER_AGENT_HEADER,
|
||||||
"Accept-Encoding": ACCEPT_ENCODING_HEADER,
|
"Accept-Encoding": ACCEPT_ENCODING_HEADER,
|
||||||
Accept: ACCEPT_HEADER,
|
Accept: ACCEPT_HEADER,
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const $: CheerioAPI = load(mainPage.data);
|
const $: CheerioAPI = load(mainPage.data);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user