refactor: replace amvstrm source with aniwatch

This commit is contained in:
2024-08-18 21:37:13 -04:00
parent 0bcc547ede
commit 1a06eb51eb
14 changed files with 179 additions and 262 deletions

View File

@@ -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;
}

View 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",
}

View File

@@ -59,8 +59,8 @@ app.openapi(route, async (c) => {
getEpisodesFromConsumet(aniListId),
),
() =>
import("./amvstrm").then(({ getEpisodesFromAmvstrm }) =>
getEpisodesFromAmvstrm(aniListId),
import("./aniwatch").then(({ getEpisodesFromAniwatch }) =>
getEpisodesFromAniwatch(aniListId),
),
]);

View File

@@ -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;
}

View File

@@ -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;

View 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;
}

View 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;
}

View File

@@ -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) {