-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(estimatedSchedule): add
estimatedSchedule
parser
- Loading branch information
1 parent
0ca9a82
commit 8826dd2
Showing
1 changed file
with
67 additions
and
0 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 |
---|---|---|
@@ -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; |