Skip to content

Commit

Permalink
refactor(getQtipInfo): update getQtipInfo response schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshRitesh12 committed Dec 25, 2024
1 parent 93be0e4 commit d27f6fb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
8 changes: 4 additions & 4 deletions __tests__/hianime/animeQtip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ test(`returns ${animeId} anime qtip info`, async () => {
const hianime = new HiAnime.Scraper();
const data = await hianime.getQtipInfo(animeId);

expect(data.id).not.toEqual(null);
expect(data.name).not.toEqual(null);
expect(data.description).not.toEqual(null);
expect(data.genres).not.toEqual([]);
expect(data.anime.id).not.toEqual(null);
expect(data.anime.name).not.toEqual(null);
expect(data.anime.description).not.toEqual(null);
expect(data.anime.genres).not.toEqual([]);
});
61 changes: 32 additions & 29 deletions src/hianime/scrapers/animeQtip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ export async function getAnimeQtipInfo(
animeId: string
): Promise<ScrapedAnimeQtipInfo> {
const res: ScrapedAnimeQtipInfo = {
id: animeId.trim(),
name: null,
malscore: null,
quality: null,
episodes: {
sub: null,
dub: null,
},
type: null,
description: null,
anime: {
id: animeId.trim(),
name: null,
malscore: null,
quality: null,
episodes: {
sub: null,
dub: null,
},
type: null,
description: null,

jname: null,
synonyms: null,
aired: null,
status: null,
genres: [],
jname: null,
synonyms: null,
aired: null,
status: null,
genres: [],
},
};

try {
animeId = String(res.id);
animeId = String(res.anime.id);
const id = animeId.split("-").pop();
if (animeId === "" || animeId.indexOf("-") === -1 || !id) {
throw new HiAnimeError("invalid anime id", getAnimeQtipInfo.name, 400);
Expand All @@ -42,32 +44,33 @@ export async function getAnimeQtipInfo(
const $: CheerioAPI = load(mainPage.data);
const selector: SelectorType = ".pre-qtip-content";

res.id =
res.anime.id =
$(selector)
?.find(".pre-qtip-button a.btn-play")
?.attr("href")
?.trim()
?.split("/")
?.pop() || null;
res.name = $(selector)?.find(".pre-qtip-title")?.text()?.trim() || null;
res.malscore =
res.anime.name =
$(selector)?.find(".pre-qtip-title")?.text()?.trim() || null;
res.anime.malscore =
$(selector)
?.find(".pre-qtip-detail")
?.children()
?.first()
?.text()
?.trim() || null;
res.quality =
res.anime.quality =
$(selector)?.find(".tick .tick-quality")?.text()?.trim() || null;
res.type =
res.anime.type =
$(selector)?.find(".badge.badge-quality")?.text()?.trim() || null;

res.episodes.sub =
res.anime.episodes.sub =
Number($(selector)?.find(".tick .tick-sub")?.text()?.trim()) || null;
res.episodes.dub =
res.anime.episodes.dub =
Number($(selector)?.find(".tick .tick-dub")?.text()?.trim()) || null;

res.description =
res.anime.description =
$(selector)?.find(".pre-qtip-description")?.text()?.trim() || null;

$(`${selector} .pre-qtip-line`).each((_, el) => {
Expand All @@ -82,19 +85,19 @@ export async function getAnimeQtipInfo(

switch (key) {
case "japanese":
res.jname = value;
res.anime.jname = value;
break;
case "synonyms":
res.synonyms = value;
res.anime.synonyms = value;
break;
case "aired":
res.aired = value;
res.anime.aired = value;
break;
case "status":
res.status = value;
res.anime.status = value;
break;
case "genres":
res.genres = value?.split(",")?.map((i) => i?.trim()) || [];
res.anime.genres = value?.split(",")?.map((i) => i?.trim()) || [];
break;
}
});
Expand Down
18 changes: 10 additions & 8 deletions src/hianime/types/scrapers/animeQtip.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Anime } from "../anime.js";

export type ScrapedAnimeQtipInfo = {
quality: string | null;
genres: string[];
aired: string | null;
synonyms: string | null;
status: string | null;
malscore: string | null;
description: string | null;
} & Omit<Anime, "poster" | "duration" | "rating">;
anime: {
quality: string | null;
genres: string[];
aired: string | null;
synonyms: string | null;
status: string | null;
malscore: string | null;
description: string | null;
} & Omit<Anime, "poster" | "duration" | "rating">;
};

0 comments on commit d27f6fb

Please sign in to comment.