Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

106 a small support for showing episode titles and images instead of just numbers #118

26 changes: 26 additions & 0 deletions setup.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
window.Http = {
version: "0.0.21",
get(options) {
return fetch(options.url)
.then((res) => res.text())
.then((text) => {
return {
data: text,
status: 200,
}
})
},
post(options) {
return fetch(options.url, {
method: "POST",
body: JSON.stringify(options.data),
})
.then((res) => res.text())
.then((text) => {
return {
data: text,
status: 200,
}
})
},
}
18 changes: 18 additions & 0 deletions src/apis/parser/myanimelist/episodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { parserDom } from "../__helpers__/parserDom"

export default function AnimeEpisodes(html: string) {
const $ = parserDom(html)

return $(".episode-list-data")
.toArray()
.map((ep) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const number = $(ep).find(".episode-number").attr("data-raw")!
const name = $(ep).find(".episode-title > a").text().trim()
const japanese = $(ep).find(".di-ib").text().trim()
const time = $(ep).find(".episode-aired").text().trim()
const average = $(ep).find(".episode-aired").text()

return { number, name, japanese, time, average }
})
}
37 changes: 37 additions & 0 deletions src/apis/parser/myanimelist/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { parserDom } from "../__helpers__/parserDom"

interface AnimeItem {
name_lower: string
search_name: string
id: number
type: string
name: string
url: string
image_url: string
}

export default function AnimeSearch(html: string) {
const $ = parserDom(html)

return $(".js-categories-seasonal tr")
.toArray()
.map((tr) => {
const url = $(tr).find(".hoverinfo_trigger").attr("href")
if (!url) return null

const indexParamAnime = url.indexOf("/anime/") + 7

if (indexParamAnime === -1) return null

const id = parseInt(
url.slice(indexParamAnime, url.indexOf("/", indexParamAnime))
)
const name = $(tr).find("strong").text()
// eslint-disable-next-line camelcase
const image_url = $(tr).find("img").attr("src")

// eslint-disable-next-line camelcase
return { id, type: "anime", name, url, image_url }
})
.filter(Boolean) as AnimeItem[]
}
106 changes: 106 additions & 0 deletions src/apis/runs/myanimelist/episodes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { getEpisodesMyAnimeList } from "./episodes"

describe("episodes", () => {
describe("getEpisodesMyAnimeList", () => {
test("should work in all of one page", async () => {
expect(
await getEpisodesMyAnimeList(
"https://myanimelist.net/anime/41389/Tonikaku_Kawaii"
).then((items) =>
items.map((item) => {
delete item.japanese
return item
})
)
).toEqual([
{
number: "1",
name: "Marriage",
time: "Oct 3, 2020",
average: "Oct 3, 2020",
},
{
number: "2",
name: "The First Night",
time: "Oct 10, 2020",
average: "Oct 10, 2020",
},
{
number: "3",
name: "Sisters",
time: "Oct 17, 2020",
average: "Oct 17, 2020",
},
{
number: "4",
name: "Promise",
time: "Oct 24, 2020",
average: "Oct 24, 2020",
},
{
number: "5",
name: "Rings",
time: "Oct 31, 2020",
average: "Oct 31, 2020",
},
{
number: "6",
name: "News",
time: "Nov 7, 2020",
average: "Nov 7, 2020",
},
{
number: "7",
name: "Trip",
time: "Nov 14, 2020",
average: "Nov 14, 2020",
},
{
number: "8",
name: "Parents",
time: "Nov 21, 2020",
average: "Nov 21, 2020",
},
{
number: "9",
name: "Daily Life",
time: "Nov 28, 2020",
average: "Nov 28, 2020",
},
{
number: "10",
name: "The Way Home",
time: "Dec 5, 2020",
average: "Dec 5, 2020",
},
{
number: "11",
name: "Friends",
time: "Dec 12, 2020",
average: "Dec 12, 2020",
},
{
number: "12",
name: "Husband and Wife",
time: "Dec 19, 2020",
average: "Dec 19, 2020",
},
{
number: "13",
name: "SNS",
time: "N/A",
average: "N/A",
},
])
})

test("should search by offset", async () => {
const diff = await getEpisodesMyAnimeList(
"https://myanimelist.net/anime/235/Detective_Conan",
100
)

expect(diff.length).toBe(100)
})
})
})
22 changes: 22 additions & 0 deletions src/apis/runs/myanimelist/episodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// first in query id anime

import type MyAnimeListEpisodesParser from "src/apis/parser/myanimelist/episodes"
import { get } from "src/logic/http"

import { useCache } from "../../useCache"
import Worker from "../../workers/myanimelist/episodes?worker"
import { PostWorker } from "../../wrap-worker"

export async function getEpisodesMyAnimeList(url: string, offset: number = 0) {
return await useCache(`${url}/episode`, async () => {
const html = await (await get(`${url}/episode?offset=${offset}`)).data

if (import.meta.env.MODE === "test") {
return import("../../parser/myanimelist/episodes").then((res) =>
res.default(html)
)
}

return PostWorker<typeof MyAnimeListEpisodesParser>(Worker, html)
})
}
Loading