Skip to content

Commit

Permalink
feat(estimatedSchedule): add estimatedSchedule parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshRitesh12 committed Dec 17, 2023
1 parent 0ca9a82 commit 8826dd2
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/parsers/estimatedSchedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
SRC_HOME_URL,
SRC_AJAX_URL,
USER_AGENT_HEADER,
ACCEPT_ENCODING_HEADER,
} from "../utils/index.js";
import axios, { AxiosError } from "axios";
import createHttpError, { type HttpError } from "http-errors";
import { load, type CheerioAPI, type SelectorType } from "cheerio";
import { type ScrapedEstimatedSchedule } from "../models/parsers/index.js";

// /anime/schedule?date=${date}
async function scrapeEstimatedSchedule(
date: string
): Promise<ScrapedEstimatedSchedule | HttpError> {
const res: ScrapedEstimatedSchedule = {
scheduledAnimes: [],
};

try {
const estScheduleURL =
`${SRC_AJAX_URL}/schedule/list?tzOffset=-330&date=${date}` as const;

const mainPage = await axios.get(estScheduleURL, {
headers: {
Accept: "*/*",
Referer: SRC_HOME_URL,
"User-Agent": USER_AGENT_HEADER,
"X-Requested-With": "XMLHttpRequest",
"Accept-Encoding": ACCEPT_ENCODING_HEADER,
},
});

const $: CheerioAPI = load(mainPage?.data?.html);

const selector: SelectorType = "li";

if ($(selector)?.text()?.trim()?.includes("No data to display")) {
return res;
}

$(selector).each((_, el) => {
res.scheduledAnimes.push({
id: $(el)?.find("a")?.attr("href")?.slice(1)?.trim() || null,
time: $(el)?.find("a .time")?.text()?.trim() || null,
name: $(el)?.find("a .film-name.dynamic-name")?.text()?.trim() || null,
jname:
$(el)
?.find("a .film-name.dynamic-name")
?.attr("data-jname")
?.trim() || null,
});
});

return res;
} catch (err: any) {
if (err instanceof AxiosError) {
throw createHttpError(
err?.response?.status || 500,
err?.response?.statusText || "Something went wrong"
);
}
throw createHttpError.InternalServerError(err?.message);
}
}

export default scrapeEstimatedSchedule;

0 comments on commit 8826dd2

Please sign in to comment.