refactor: replace amvstrm source with aniwatch
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
import { Episode, type EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromAmvstrm(
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
try {
|
||||
const episodes: Episode[] | null = await fetch(
|
||||
`https://amvstrm.up.railway.app/api/v2/episode/${aniListId}`,
|
||||
)
|
||||
.then((res) => res.json<AmvstrmEpisodesResponse>())
|
||||
.then(({ code, message, episodes }) => {
|
||||
if (code >= 400) {
|
||||
console.error(
|
||||
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}, code: ${code}, message: ${message}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return episodes.map<Episode>(
|
||||
({ id, description, image, title, episode, airDate }) => ({
|
||||
id,
|
||||
number: episode,
|
||||
description,
|
||||
img: image,
|
||||
title,
|
||||
updatedAt: airDate ?? 0,
|
||||
}),
|
||||
);
|
||||
});
|
||||
if (!episodes || episodes.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { providerId: "amvstrm", episodes };
|
||||
} catch (error) {
|
||||
console.error(
|
||||
new Error(
|
||||
`Error trying to load episodes from amvstrm; aniListId: ${aniListId}`,
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
interface AmvstrmEpisodesResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
episodes: AmvstrmEpisode[];
|
||||
}
|
||||
|
||||
interface AmvstrmEpisode {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string | null;
|
||||
episode: number;
|
||||
image: string;
|
||||
airDate: null;
|
||||
}
|
||||
107
src/controllers/episodes/getByAniListId/aniwatch.ts
Normal file
107
src/controllers/episodes/getByAniListId/aniwatch.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { Episode, type EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromAniwatch(
|
||||
aniListId: number,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
try {
|
||||
const animeTitle = await import("~/controllers/title/anilist")
|
||||
.then(({ fetchTitleFromAnilist }) =>
|
||||
fetchTitleFromAnilist(aniListId, undefined),
|
||||
)
|
||||
.then((title) => title?.title?.userPreferred ?? title?.title?.english);
|
||||
|
||||
if (!animeTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const aniwatchId = await getAniwatchId(animeTitle);
|
||||
if (!aniwatchId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const episodes: Episode[] | null = await fetch(
|
||||
`https://aniwatch.up.railway.app/anime/episodes/${aniwatchId}`,
|
||||
)
|
||||
.then((res) => res.json<AniwatchEpisodesResponse>())
|
||||
.then(({ totalEpisodes, episodes }) => {
|
||||
if (totalEpisodes === 0) {
|
||||
console.error(
|
||||
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${totalEpisodes}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return episodes.map<Episode>(({ episodeId, number, title }) => ({
|
||||
id: episodeId,
|
||||
number,
|
||||
title,
|
||||
updatedAt: 0,
|
||||
}));
|
||||
});
|
||||
if (!episodes || episodes.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { providerId: "aniwatch", episodes };
|
||||
} catch (error) {
|
||||
console.error(
|
||||
new Error(
|
||||
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}`,
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getAniwatchId(animeTitle: string): Promise<string | undefined> {
|
||||
return fetch(
|
||||
`https://aniwatch.up.railway.app/anime/search?q=${encodeURIComponent(animeTitle)}`,
|
||||
)
|
||||
.then((res) => res.json<AniwatchSearchResponse>())
|
||||
.then(({ animes }) => animes[0]?.id);
|
||||
}
|
||||
|
||||
export interface AniwatchEpisodesResponse {
|
||||
totalEpisodes: number;
|
||||
episodes: AniwatchEpisode[];
|
||||
}
|
||||
|
||||
export interface AniwatchEpisode {
|
||||
title: string;
|
||||
episodeId: string;
|
||||
number: number;
|
||||
isFiller: boolean;
|
||||
}
|
||||
|
||||
export interface AniwatchSearchResponse {
|
||||
animes: Anime[];
|
||||
currentPage: number;
|
||||
hasNextPage: boolean;
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
interface Anime {
|
||||
id: string;
|
||||
name: string;
|
||||
jname: string;
|
||||
poster: string;
|
||||
duration: string;
|
||||
type: Type;
|
||||
rating: null | string;
|
||||
episodes: Episodes;
|
||||
}
|
||||
|
||||
interface Episodes {
|
||||
sub: number | null;
|
||||
dub: number | null;
|
||||
}
|
||||
|
||||
enum Type {
|
||||
Movie = "Movie",
|
||||
Ona = "ONA",
|
||||
Ova = "OVA",
|
||||
Special = "Special",
|
||||
Tv = "TV",
|
||||
}
|
||||
@@ -59,8 +59,8 @@ app.openapi(route, async (c) => {
|
||||
getEpisodesFromConsumet(aniListId),
|
||||
),
|
||||
() =>
|
||||
import("./amvstrm").then(({ getEpisodesFromAmvstrm }) =>
|
||||
getEpisodesFromAmvstrm(aniListId),
|
||||
import("./aniwatch").then(({ getEpisodesFromAniwatch }) =>
|
||||
getEpisodesFromAniwatch(aniListId),
|
||||
),
|
||||
]);
|
||||
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
import type { FetchUrlResponse } from "./responseType";
|
||||
|
||||
export async function getSourcesFromAmvstrm(
|
||||
watchId: string,
|
||||
): Promise<FetchUrlResponse | null> {
|
||||
const source = await fetch(
|
||||
`https://amvstrm.up.railway.app/api/v2/stream/${watchId}`,
|
||||
)
|
||||
.then((res) => res.json<AmvstrmStreamResponse>())
|
||||
.then(({ stream }) => {
|
||||
const streamObj = stream?.multi;
|
||||
if (!!streamObj) {
|
||||
return streamObj.main ?? streamObj.backup;
|
||||
}
|
||||
})
|
||||
.then((streamObj) => streamObj?.url);
|
||||
|
||||
if (!source) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
source,
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
};
|
||||
}
|
||||
|
||||
interface AmvstrmStreamResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
info: Info;
|
||||
stream: Stream;
|
||||
iframe: Iframe[];
|
||||
plyr: Nspl;
|
||||
nspl: Nspl;
|
||||
}
|
||||
|
||||
interface Iframe {
|
||||
name: string;
|
||||
iframe: string;
|
||||
}
|
||||
|
||||
interface Info {
|
||||
title: string;
|
||||
id: string;
|
||||
episode: string;
|
||||
}
|
||||
|
||||
interface Nspl {
|
||||
main: string;
|
||||
backup: string;
|
||||
}
|
||||
|
||||
interface Stream {
|
||||
multi: Multi;
|
||||
tracks: Tracks;
|
||||
}
|
||||
|
||||
interface Multi {
|
||||
main: Backup;
|
||||
backup: Backup;
|
||||
}
|
||||
|
||||
interface Backup {
|
||||
url: string;
|
||||
label: string;
|
||||
isM3U8: boolean;
|
||||
quality: string;
|
||||
}
|
||||
|
||||
interface Tracks {
|
||||
file: string;
|
||||
kind: string;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { sortByProperty } from "~/libs/sortByProperty";
|
||||
|
||||
import { type SkipTime, convertSkipTime } from "./convertSkipTime";
|
||||
import {
|
||||
audioPriority,
|
||||
qualityPriority,
|
||||
@@ -37,14 +38,8 @@ export async function getSourcesFromAnify(
|
||||
source,
|
||||
audio,
|
||||
subtitles,
|
||||
intro:
|
||||
typeof intro?.start === "number" && typeof intro?.end === "number"
|
||||
? [intro.start, intro.end].map((seconds) => Math.floor(seconds))
|
||||
: undefined,
|
||||
outro:
|
||||
typeof outro?.start === "number" && typeof outro?.end === "number"
|
||||
? [outro.start, outro.end].map((seconds) => Math.floor(seconds))
|
||||
: undefined,
|
||||
intro: convertSkipTime(intro),
|
||||
outro: convertSkipTime(outro),
|
||||
headers: Object.keys(headers ?? {}).length > 0 ? headers : undefined,
|
||||
};
|
||||
}
|
||||
@@ -58,11 +53,6 @@ interface AnifySourcesResponse {
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
interface SkipTime {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
interface VideoSource {
|
||||
url: string;
|
||||
quality: string;
|
||||
|
||||
54
src/controllers/episodes/getEpisodeUrl/aniwatch.ts
Normal file
54
src/controllers/episodes/getEpisodeUrl/aniwatch.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { type SkipTime, convertSkipTime } from "./convertSkipTime";
|
||||
import type { FetchUrlResponse } from "./responseType";
|
||||
|
||||
export async function getSourcesFromAmvstrm(
|
||||
watchId: string,
|
||||
): Promise<FetchUrlResponse | null> {
|
||||
const { source, intro, outro, subtitles } = await fetch(
|
||||
`https://aniwatch.up.railway.app/anime/episode-srcs?id=${encodeURIComponent(watchId)}`,
|
||||
)
|
||||
.then((res) => res.json<AniwatchEpisodeUrlResponse>())
|
||||
.then(({ intro, outro, sources, tracks }) => {
|
||||
return {
|
||||
intro: convertSkipTime(intro),
|
||||
outro: convertSkipTime(outro),
|
||||
source: sources[0].url,
|
||||
subtitles: tracks
|
||||
.filter(({ kind }) => kind === "captions")
|
||||
.map(({ file, label }) => ({ url: file, lang: label ?? "" })),
|
||||
};
|
||||
});
|
||||
|
||||
if (!source) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
source,
|
||||
intro,
|
||||
outro,
|
||||
subtitles,
|
||||
audio: [],
|
||||
};
|
||||
}
|
||||
|
||||
interface AniwatchEpisodeUrlResponse {
|
||||
tracks: Track[];
|
||||
intro: SkipTime;
|
||||
outro: SkipTime;
|
||||
sources: Source[];
|
||||
anilistID: number;
|
||||
malID: number;
|
||||
}
|
||||
|
||||
interface Source {
|
||||
url: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface Track {
|
||||
file: string;
|
||||
label?: string;
|
||||
kind: string;
|
||||
default?: boolean;
|
||||
}
|
||||
11
src/controllers/episodes/getEpisodeUrl/convertSkipTime.ts
Normal file
11
src/controllers/episodes/getEpisodeUrl/convertSkipTime.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export interface SkipTime {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export function convertSkipTime(skipTime: SkipTime): number[] | undefined {
|
||||
return typeof skipTime?.start === "number" &&
|
||||
typeof skipTime?.end === "number"
|
||||
? [skipTime.start, skipTime.end].map((seconds) => Math.floor(seconds))
|
||||
: undefined;
|
||||
}
|
||||
@@ -94,9 +94,9 @@ app.openapi(route, async (c) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (provider === "amvstrm") {
|
||||
if (provider === "aniwatch") {
|
||||
try {
|
||||
const result = await import("./amvstrm").then(
|
||||
const result = await import("./aniwatch").then(
|
||||
({ getSourcesFromAmvstrm }) => getSourcesFromAmvstrm(id),
|
||||
);
|
||||
if (!result) {
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
export async function fetchSearchResultsFromAmvstrm(
|
||||
query: string,
|
||||
page: number,
|
||||
limit: number,
|
||||
) {
|
||||
return fetch(
|
||||
`https://amvstrm.up.railway.app/api/v2/search?q=${query}&p=${page}&limit=${limit}`,
|
||||
)
|
||||
.then((res) => res.json<any>())
|
||||
.then(({ pageInfo: { hasNextPage }, results }) => ({
|
||||
hasNextPage,
|
||||
results: results.map(
|
||||
({
|
||||
id,
|
||||
title: { userPreferred, english },
|
||||
coverImage: { extraLarge, large, medium },
|
||||
}: any) => ({
|
||||
id,
|
||||
title: { userPreferred, english },
|
||||
coverImage: { extraLarge, large, medium },
|
||||
}),
|
||||
),
|
||||
}))
|
||||
.then((searchResults) => {
|
||||
if (searchResults.results.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return searchResults;
|
||||
});
|
||||
}
|
||||
@@ -12,12 +12,6 @@ describe('requests the "/search" route', () => {
|
||||
expect(response.json()).resolves.toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("valid query that returns amvstrm results", async () => {
|
||||
const response = await app.request("/search?query=amvstrm");
|
||||
|
||||
expect(response.json()).resolves.toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("query that returns no results", async () => {
|
||||
const response = await app.request("/search?query=a");
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||
import { PaginatedResponseSchema } from "~/types/schema";
|
||||
|
||||
import { fetchSearchResultsFromAmvstrm } from "./amvstrm";
|
||||
import { fetchSearchResultsFromAnilist } from "./anilist";
|
||||
import { SearchResult } from "./searchResult";
|
||||
|
||||
@@ -41,7 +40,6 @@ app.openapi(route, async (c) => {
|
||||
|
||||
const { result: response, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchSearchResultsFromAnilist(query, page, limit),
|
||||
() => fetchSearchResultsFromAmvstrm(query, page, limit),
|
||||
]);
|
||||
|
||||
if (!response) {
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import { Title } from "~/types/title";
|
||||
|
||||
export async function fetchTitleFromAmvstrm(
|
||||
aniListId: number,
|
||||
): Promise<Title | undefined> {
|
||||
return Promise.all([
|
||||
fetch(`https://amvstrm.up.railway.app/api/v2/info/${aniListId}`).then(
|
||||
(res) => res.json<any>(),
|
||||
),
|
||||
fetchMissingInformationFromAnify(aniListId).catch((err) => {
|
||||
console.error("Failed to get missing information from Anify", err);
|
||||
return null;
|
||||
}),
|
||||
]).then(async ([amvstrmInfo, anifyInfo]) => {
|
||||
if (amvstrmInfo.code >= 400) {
|
||||
console.error(
|
||||
`Error trying to load title from amvstrm; aniListId: ${aniListId}, code: ${amvstrmInfo.code}, message: ${amvstrmInfo.message}`,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
id: amvstrmInfo.id,
|
||||
idMal: amvstrmInfo.idMal,
|
||||
title: {
|
||||
userPreferred: amvstrmInfo.title.userPreferred,
|
||||
english: amvstrmInfo.title.english,
|
||||
},
|
||||
description: amvstrmInfo.description,
|
||||
episodes: amvstrmInfo.episodes,
|
||||
genres: amvstrmInfo.genres,
|
||||
status: amvstrmInfo.status,
|
||||
averageScore: amvstrmInfo.score.averageScore,
|
||||
bannerImage: amvstrmInfo.bannerImage ?? anifyInfo?.bannerImage,
|
||||
coverImage: {
|
||||
extraLarge: amvstrmInfo.coverImage.extraLarge,
|
||||
large: amvstrmInfo.coverImage.large,
|
||||
medium: amvstrmInfo.coverImage.medium,
|
||||
},
|
||||
countryOfOrigin:
|
||||
amvstrmInfo.countryOfOrigin ?? anifyInfo?.countryOfOrigin,
|
||||
nextAiringEpisode: amvstrmInfo.nextair,
|
||||
mediaListEntry: null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
type AnifyInformation = {
|
||||
bannerImage: string | null;
|
||||
countryOfOrigin: string;
|
||||
};
|
||||
|
||||
function fetchMissingInformationFromAnify(
|
||||
aniListId: number,
|
||||
): Promise<AnifyInformation> {
|
||||
return fetch(`https://anify.eltik.cc/info?id=${aniListId}`)
|
||||
.then((res) => res.json() as Promise<AnifyInformation>)
|
||||
.then(({ bannerImage, countryOfOrigin }) => ({
|
||||
bannerImage,
|
||||
countryOfOrigin,
|
||||
}));
|
||||
}
|
||||
@@ -22,13 +22,6 @@ describe('requests the "/title" route', () => {
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it("with an unknown title from anilist but valid title from amvstrm", async () => {
|
||||
const response = await app.request("/title?id=50");
|
||||
|
||||
expect(response.json()).resolves.toMatchSnapshot();
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it("with an unknown title from all sources", async () => {
|
||||
const response = await app.request("/title?id=-1");
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
} from "~/types/schema";
|
||||
import { Title } from "~/types/title";
|
||||
|
||||
import { fetchTitleFromAmvstrm } from "./amvstrm";
|
||||
import { fetchTitleFromAnilist } from "./anilist";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
@@ -50,7 +49,6 @@ app.openapi(route, async (c) => {
|
||||
|
||||
const { result: title, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
||||
() => fetchTitleFromAmvstrm(aniListId),
|
||||
]);
|
||||
|
||||
if (errorOccurred) {
|
||||
|
||||
Reference in New Issue
Block a user