Skip to content

Commit

Permalink
feat, add episodes myanimelist
Browse files Browse the repository at this point in the history
  • Loading branch information
tachibana-shin committed Apr 23, 2023
1 parent deac2f4 commit 9ee2df9
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"js-cookie": "^3.0.1",
"lz-string": "^1.4.4",
"message-port-api": "^0.0.5",
"minisearch": "^6.0.1",
"pinia": "^2.0.29",
"pinia-plugin-persistedstate": "^3.0.2",
"pinia-plugin-persistedstate-2": "^2.0.10",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions setup.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
window.Http = {
version: "0.0.21",
get(options) {
return fetch(options.url)
.then((res) => res.text())
.then((text) => {
return {
data: text,
}
})
},
post(options) {
return fetch(options.url, {
method: "POST",
body: JSON.stringify(options.data),
})
.then((res) => res.text())
.then((text) => {
return {
data: text,
}
})
},
}
19 changes: 18 additions & 1 deletion src/apis/parser/myanimelist/episodes.ts
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
export function AnimeEpisodes(html) {}
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 }
})
}
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)
})
})
})
27 changes: 27 additions & 0 deletions src/apis/runs/myanimelist/episodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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"
import { PostWorker } from "../../wrap-worker"

export async function getEpisodesMyAnimeList(url: string, offset: number = 0) {
return await useCache(`${url}/episode`, async () => {
const html =
import.meta.env.MODE === "test"
? await fetch(`${url}/episode?offset=${offset}`).then((res) => res.text())
: 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)
})
}
13 changes: 6 additions & 7 deletions src/apis/runs/myanimelist/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import MiniSearch from "minisearch"
import type MyAnimeListSearchParser from "src/apis/parser/myanimelist/search"
import { get } from "src/logic/http"

import { useCache } from "../../useCache"
import Worker from "../../workers/myanimelist/search?worker"
Expand All @@ -12,11 +13,11 @@ const BEFORE_SEASON = "ss|\\wd"

async function runRawSearch(query: string) {
return await useCache(`myanimelist/search/${query}`, async () => {
const html = await fetch(
const { data: html } = await get(
`https://myanimelist.net/anime.php?cat=anime&q=${encodeURIComponent(
query
)}&type=0&score=0&status=0&p=0&r=0&sm=0&sd=0&sy=0&em=0&ed=0&ey=0&c%5B%5D=a&c%5B%5D=b&c%5B%5D=c&c%5B%5D=f`
).then((res) => res.text())
)

if (import.meta.env.MODE === "test") {
return import("../../parser/myanimelist/search").then((res) =>
Expand Down Expand Up @@ -168,7 +169,9 @@ export async function getAmimeMyAnimeList(
// Index all documents
miniSearch.addAll(anime)

const result = miniSearch.search(otherRmd)[0] as unknown as Awaited<ReturnType<typeof runRawSearch>>[0]
const result = miniSearch.search(otherRmd)[0] as unknown as Awaited<
ReturnType<typeof runRawSearch>
>[0]
console.log(otherRmd, result)
// console.log(otherRmd, result.slice(0, 10))
// .filter((item) => {
Expand All @@ -192,7 +195,3 @@ export async function getAmimeMyAnimeList(
throw err
}
}

export function getEpisodesAnimeMyAnimeList(url: string) {
return fetch(`${url}/episode`).then((res) => res.text())
}
4 changes: 4 additions & 0 deletions src/apis/workers/myanimelist/episodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { WrapWorker } from "../..//wrap-worker"
import Episodes from "../../parser/myanimelist/episodes"

WrapWorker(Episodes)
4 changes: 2 additions & 2 deletions src/boot/installed-extension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { i18n } from "src/boot/i18n"
import { ref, watch } from "vue"

const installed = ref<boolean>()
const installed = ref<boolean>(typeof window.Http === 'object')
setTimeout(() => {
if (!installed.value) installed.value = false
}, 5_0000)

// eslint-disable-next-line functional/no-let
let Http: Http
let Http: Http = window.Http
Object.defineProperty(window, "Http", {
get() {
// console.log("================= set Http ==================")
Expand Down
8 changes: 8 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import path from "path"
import { defineConfig } from "vitest/config"

export default defineConfig({
resolve: {
alias: {
boot: path.resolve(__dirname, "src/boot"),
stores: path.resolve(__dirname, "src/stores"),
},
},
test: {
environment: "jsdom",
globals: true,
setupFiles: ["./setup.vitest.ts"],
},
})

0 comments on commit 9ee2df9

Please sign in to comment.