also removed any references to Anify
This commit is contained in:
131
src/services/episodes/getEpisodeUrl/aniwatch.ts
Normal file
131
src/services/episodes/getEpisodeUrl/aniwatch.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import type { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
||||
|
||||
import { type SkipTime, convertSkipTime } from "./convertSkipTime";
|
||||
|
||||
export async function getSourcesFromAniwatch(
|
||||
watchId: string,
|
||||
): Promise<FetchUrlResponse | null> {
|
||||
console.log(`Fetching sources from aniwatch for ${watchId}`);
|
||||
const url = await getEpisodeUrl(watchId);
|
||||
if (url) {
|
||||
return { success: true, result: url };
|
||||
}
|
||||
|
||||
const servers = await getEpisodeServers(watchId);
|
||||
if (servers.length === 0) {
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
for (const server of servers) {
|
||||
const url = await getEpisodeUrl(watchId, server.serverName);
|
||||
if (url) {
|
||||
return { success: true, result: url };
|
||||
}
|
||||
}
|
||||
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
async function getEpisodeUrl(watchId: string, server?: string) {
|
||||
let url = `https://aniwatch.up.railway.app/api/v2/hianime/episode/sources?animeEpisodeId=${encodeURIComponent(watchId)}`;
|
||||
if (server) {
|
||||
url += `&server=${encodeURIComponent(server)}`;
|
||||
}
|
||||
|
||||
const { source, intro, outro, subtitles, headers } = await fetch(url)
|
||||
.then(
|
||||
(res) =>
|
||||
res.json() as Promise<{
|
||||
status: number;
|
||||
data: AniwatchEpisodeUrlResponse;
|
||||
}>,
|
||||
)
|
||||
.then(({ status, data }) => {
|
||||
if (status >= 300 || !data.sources || data.sources.length === 0) {
|
||||
return { source: null };
|
||||
}
|
||||
|
||||
const { intro, outro, sources, tracks, headers } = data;
|
||||
return {
|
||||
intro: convertSkipTime(intro),
|
||||
outro: convertSkipTime(outro),
|
||||
source: sources[0].url,
|
||||
subtitles: tracks.map(({ url, lang }) => ({
|
||||
url,
|
||||
lang,
|
||||
})),
|
||||
headers,
|
||||
};
|
||||
});
|
||||
|
||||
if (!source) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
source,
|
||||
headers,
|
||||
intro,
|
||||
outro,
|
||||
subtitles,
|
||||
audio: [],
|
||||
};
|
||||
}
|
||||
|
||||
async function getEpisodeServers(watchId: string) {
|
||||
const { data } = await fetch(
|
||||
`https://aniwatch.up.railway.app/api/v2/hianime/episode/servers?animeEpisodeId=${encodeURIComponent(
|
||||
watchId,
|
||||
)}`,
|
||||
)
|
||||
.then((res) => res.json() as Promise<AniwatchEpisodeServersResponse>)
|
||||
.then((res) => {
|
||||
if (res.status >= 300 || !res.data) {
|
||||
throw new Error("Failed to fetch episode servers");
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
|
||||
return data.sub;
|
||||
}
|
||||
|
||||
interface AniwatchEpisodeUrlResponse {
|
||||
headers?: Record<string, string>;
|
||||
tracks: Track[];
|
||||
intro: SkipTime;
|
||||
outro: SkipTime;
|
||||
sources: Source[];
|
||||
anilistID: number;
|
||||
malID: number;
|
||||
}
|
||||
|
||||
interface Source {
|
||||
url: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface Track {
|
||||
url: string;
|
||||
lang?: string;
|
||||
kind: string;
|
||||
default?: boolean;
|
||||
}
|
||||
|
||||
interface AniwatchEpisodeServersResponse {
|
||||
status: number;
|
||||
data: AniwatchEpisodeServers;
|
||||
}
|
||||
|
||||
interface AniwatchEpisodeServers {
|
||||
sub: AniwatchEpisodeServer[];
|
||||
dub: AniwatchEpisodeServer[];
|
||||
raw: AniwatchEpisodeServer[];
|
||||
episodeID: string;
|
||||
episodeNo: number;
|
||||
}
|
||||
|
||||
interface AniwatchEpisodeServer {
|
||||
serverName: string;
|
||||
serverID: number;
|
||||
}
|
||||
18
src/services/episodes/getEpisodeUrl/convertSkipTime.ts
Normal file
18
src/services/episodes/getEpisodeUrl/convertSkipTime.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export interface SkipTime {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export function convertSkipTime(skipTime: SkipTime): number[] | undefined {
|
||||
if (
|
||||
typeof skipTime?.start !== "number" ||
|
||||
typeof skipTime?.end !== "number"
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
if (skipTime.end === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return [skipTime.start, skipTime.end].map((seconds) => Math.floor(seconds));
|
||||
}
|
||||
47
src/services/episodes/getEpisodeUrl/index.spec.ts
Normal file
47
src/services/episodes/getEpisodeUrl/index.spec.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import app from "~/index";
|
||||
import { server } from "~/mocks";
|
||||
|
||||
server.listen();
|
||||
|
||||
describe('requests the "/episodes/:id/url" route', () => {
|
||||
it("with sources from Aniwatch", async () => {
|
||||
const response = await app.request(
|
||||
"/episodes/4/url",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
episodeNumber: 1,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
source:
|
||||
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("with no URL from Aniwatch source", async () => {
|
||||
const response = await app.request("/episodes/-1/url", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
episodeNumber: -1,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
50
src/services/episodes/getEpisodeUrl/index.ts
Normal file
50
src/services/episodes/getEpisodeUrl/index.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { FetchUrlResponse } from "~/types/episode/fetch-url-response";
|
||||
|
||||
import { fetchEpisodes } from "../getByAniListId";
|
||||
|
||||
export async function fetchEpisodeUrl({
|
||||
id,
|
||||
aniListId,
|
||||
episodeNumber,
|
||||
}:
|
||||
| { id: string; aniListId?: number; episodeNumber?: number }
|
||||
| {
|
||||
id?: string;
|
||||
aniListId: number;
|
||||
episodeNumber: number;
|
||||
}): Promise<FetchUrlResponse | null> {
|
||||
try {
|
||||
let episodeId = id;
|
||||
if (!id) {
|
||||
const episodes = await fetchEpisodes(aniListId!);
|
||||
if (episodes.length === 0) {
|
||||
console.error(`Failed to fetch episodes for title ${aniListId}`);
|
||||
return null;
|
||||
}
|
||||
const episode = episodes.find(
|
||||
(episode) => episode.number === episodeNumber,
|
||||
);
|
||||
if (!episode) {
|
||||
console.error(
|
||||
`Episode ${episodeNumber} not found for title ${aniListId}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
episodeId = episode.id;
|
||||
}
|
||||
|
||||
const result = await import("./aniwatch").then(
|
||||
({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(episodeId!),
|
||||
);
|
||||
if (!result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch download URL from Aniwatch", e);
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
9
src/services/episodes/getEpisodeUrl/priorities.ts
Normal file
9
src/services/episodes/getEpisodeUrl/priorities.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const qualityPriority = {
|
||||
default: 1,
|
||||
auto: 1,
|
||||
backup: 2,
|
||||
"1080p": 3,
|
||||
"720p": 4,
|
||||
};
|
||||
export const subtitlesPriority = { English: 1 };
|
||||
export const audioPriority = { Japanese: 1 };
|
||||
Reference in New Issue
Block a user