also removed any references to Anify
This commit is contained in:
253
src/services/episodes/getByAniListId/aniwatch.ts
Normal file
253
src/services/episodes/getByAniListId/aniwatch.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
import { findBestMatchingTitle } from "~/libs/findBestMatchingTitle";
|
||||
import { sleep } from "~/libs/sleep";
|
||||
import { Episode, type EpisodesResponse } from "~/types/episode";
|
||||
|
||||
export async function getEpisodesFromAniwatch(
|
||||
aniListId: number,
|
||||
shouldRetry: boolean = false,
|
||||
): Promise<EpisodesResponse | null> {
|
||||
try {
|
||||
const animeTitle = await import("~/libs/anilist/getTitle")
|
||||
.then(({ fetchTitleFromAnilist }) =>
|
||||
fetchTitleFromAnilist(aniListId, undefined),
|
||||
)
|
||||
.then((title) => ({
|
||||
english: title?.title?.english?.substring(0, 100),
|
||||
userPreferred: title?.title?.userPreferred?.substring(0, 100),
|
||||
}));
|
||||
|
||||
if (!animeTitle.english && !animeTitle.userPreferred) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const aniwatchId = await getAniwatchId(animeTitle);
|
||||
if (!aniwatchId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const episodes: Episode[] | null = await fetchEpisodes(
|
||||
aniwatchId,
|
||||
aniListId,
|
||||
);
|
||||
if (!episodes || episodes.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Tower of God S2
|
||||
if (aniListId == 153406) {
|
||||
const aniwatchId = await getAniwatchId({
|
||||
english: "Tower of God Season 2: Workshop Battle",
|
||||
});
|
||||
if (aniwatchId) {
|
||||
const lastEpisodeOfPreviousTitle = episodes.at(-1)!!.number;
|
||||
return {
|
||||
providerId: "aniwatch",
|
||||
episodes: await fetchEpisodes(aniwatchId, aniListId).then(
|
||||
(extraEpisodes) =>
|
||||
episodes.concat(
|
||||
extraEpisodes?.map(({ number, ...episode }) => ({
|
||||
...episode,
|
||||
number: number + lastEpisodeOfPreviousTitle,
|
||||
})) ?? [],
|
||||
),
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { providerId: "aniwatch", episodes };
|
||||
} catch (error) {
|
||||
if (shouldRetry && "response" in error && error.response.status === 429) {
|
||||
console.log(
|
||||
"429, retrying in",
|
||||
error.response.headers.get("Retry-After"),
|
||||
);
|
||||
return sleep(
|
||||
Number(error.response.headers.get("Retry-After")!) * 1000,
|
||||
).then(() => getEpisodesFromAniwatch(aniListId));
|
||||
}
|
||||
|
||||
console.error(
|
||||
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}`,
|
||||
);
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function fetchEpisodes(
|
||||
aniwatchId: string,
|
||||
aniListId: number,
|
||||
): Promise<
|
||||
| {
|
||||
number: number;
|
||||
id: string;
|
||||
updatedAt: number;
|
||||
description?: string | null | undefined;
|
||||
title?: string | null | undefined;
|
||||
img?: string | null | undefined;
|
||||
rating?: number | null | undefined;
|
||||
}[]
|
||||
| null
|
||||
> {
|
||||
return await fetch(
|
||||
`https://aniwatch.up.railway.app/api/v2/hianime/anime/${aniwatchId}/episodes`,
|
||||
)
|
||||
.then(
|
||||
(res) =>
|
||||
res.json() as Promise<{
|
||||
status: number;
|
||||
data: AniwatchEpisodesResponse;
|
||||
}>,
|
||||
)
|
||||
.then(({ status, data }) => {
|
||||
if (status >= 300 || data.totalEpisodes === 0) {
|
||||
console.error(
|
||||
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}, totalEpisodes: ${data.totalEpisodes}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const { episodes } = data;
|
||||
return episodes.map<Episode>(({ episodeId, number, title }) => ({
|
||||
id: episodeId,
|
||||
number,
|
||||
title,
|
||||
updatedAt: 0,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
// function updateTitles(title: Partial<{ english: string; userPreferred: string }>) {
|
||||
// const english = title.english?.toLowerCase();
|
||||
// const userPreferred = title.userPreferred?.toLowerCase();
|
||||
|
||||
// if (english?.match(/my hero academia.+[0-9]$/)) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
function getAniwatchId(
|
||||
animeTitle: Partial<{ english: string; userPreferred: string }>,
|
||||
): Promise<string | undefined> {
|
||||
animeTitle = {
|
||||
english: animeTitle?.english?.toLowerCase(),
|
||||
userPreferred: animeTitle?.userPreferred?.toLowerCase(),
|
||||
};
|
||||
const promises = [];
|
||||
if (animeTitle.userPreferred) {
|
||||
promises.push(
|
||||
fetch(
|
||||
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(
|
||||
animeTitle.userPreferred,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (animeTitle.english && animeTitle.english !== animeTitle.userPreferred) {
|
||||
promises.push(
|
||||
fetch(
|
||||
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(
|
||||
animeTitle.english,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.allSettled(promises)
|
||||
.then((responses) => {
|
||||
return responses.reduce(
|
||||
async (current, res) => {
|
||||
if (res.status === "rejected") {
|
||||
return current;
|
||||
}
|
||||
|
||||
const json = (await res.value.json()) as {
|
||||
status: number;
|
||||
data: AniwatchSearchResponse;
|
||||
};
|
||||
const currentValue = await current;
|
||||
return {
|
||||
success: currentValue.success || json.status === 200,
|
||||
data: {
|
||||
...currentValue.data,
|
||||
animes: [
|
||||
...currentValue.data.animes,
|
||||
...(json.data?.animes ?? []),
|
||||
],
|
||||
},
|
||||
};
|
||||
},
|
||||
Promise.resolve({
|
||||
success: false,
|
||||
data: { animes: [] },
|
||||
}),
|
||||
);
|
||||
})
|
||||
.then(({ success, data: { animes } }) => {
|
||||
if (!success) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { title: bestMatchingTitle, score } = findBestMatchingTitle(
|
||||
animeTitle,
|
||||
animes.map((anime) => ({
|
||||
english: anime.name,
|
||||
userPreferred: anime.jname,
|
||||
})),
|
||||
);
|
||||
if (score < 0.8) {
|
||||
return;
|
||||
}
|
||||
|
||||
return animes.find(
|
||||
(anime) =>
|
||||
anime.name?.toLowerCase() === bestMatchingTitle ||
|
||||
anime.jname?.toLowerCase() === bestMatchingTitle,
|
||||
)?.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",
|
||||
}
|
||||
19
src/services/episodes/getByAniListId/index.ts
Normal file
19
src/services/episodes/getByAniListId/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Episode } from "~/types/episode";
|
||||
|
||||
export async function fetchEpisodes(
|
||||
aniListId: number,
|
||||
shouldRetry: boolean = false,
|
||||
): Promise<Episode[]> {
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
const { mockEpisodes } = await import("~/mocks/mockData");
|
||||
return mockEpisodes();
|
||||
}
|
||||
|
||||
return import("./aniwatch")
|
||||
.then(({ getEpisodesFromAniwatch }) =>
|
||||
getEpisodesFromAniwatch(aniListId, shouldRetry),
|
||||
)
|
||||
.then((episodeResults) => episodeResults?.episodes ?? []);
|
||||
}
|
||||
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 };
|
||||
31
src/services/episodes/markEpisodeAsWatched/anilist.ts
Normal file
31
src/services/episodes/markEpisodeAsWatched/anilist.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { env } from "cloudflare:workers";
|
||||
|
||||
export async function markEpisodeAsWatched(
|
||||
aniListToken: string,
|
||||
titleId: number,
|
||||
episodeNumber: number,
|
||||
markTitleAsComplete: boolean,
|
||||
) {
|
||||
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
|
||||
const stub = env.ANILIST_DO.get(durableObjectId);
|
||||
|
||||
let data;
|
||||
if (markTitleAsComplete) {
|
||||
data = await stub.markTitleAsWatched(titleId, aniListToken);
|
||||
} else {
|
||||
data = await stub.markEpisodeAsWatched(
|
||||
titleId,
|
||||
episodeNumber,
|
||||
aniListToken,
|
||||
);
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
throw new Error(`Failed to mark episode as watched`);
|
||||
}
|
||||
|
||||
return {
|
||||
...data?.user,
|
||||
statistics: data?.user?.statistics?.anime,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user