Skip to content

Commit 67a5a56

Browse files
authored
feat(anilist): Enhance episode source fetching with dub/sub support (#671)
1 parent 1d42ccd commit 67a5a56

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/routes/meta/anilist.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Redis } from 'ioredis';
22
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
33
import { ANIME, META, PROVIDERS_LIST } from '@consumet/extensions';
4-
import { Genres } from '@consumet/extensions/dist/models';
4+
import { Genres, SubOrSub } from '@consumet/extensions/dist/models';
55
import Anilist from '@consumet/extensions/dist/providers/meta/anilist';
66
import { StreamingServers } from '@consumet/extensions/dist/models';
77

@@ -133,7 +133,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
133133
const weekEnd = (request.query as { weekEnd: number | string }).weekEnd;
134134
const notYetAired = (request.query as { notYetAired: boolean }).notYetAired;
135135

136-
const anilist = generateAnilistMeta();
136+
const anilist = generateAnilistMeta();
137137
const _weekStart = Math.ceil(Date.now() / 1000);
138138

139139
const res = await anilist.fetchAiringSchedule(
@@ -313,10 +313,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
313313
const episodeId = (request.params as { episodeId: string }).episodeId;
314314
const provider = (request.query as { provider?: string }).provider;
315315
const server = (request.query as { server?: StreamingServers }).server;
316+
let isDub = (request.query as { dub?: string | boolean }).dub;
316317

317318
if (server && !Object.values(StreamingServers).includes(server))
318319
return reply.status(400).send('Invalid server');
319320

321+
if (isDub === 'true' || isDub === '1') isDub = true;
322+
else isDub = false;
323+
320324
let anilist = generateAnilistMeta(provider);
321325

322326
try {
@@ -326,12 +330,29 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
326330
.send(
327331
await cache.fetch(
328332
redis,
329-
`anilist:watch;${episodeId};${anilist.provider.name.toLowerCase()};${server}`,
330-
async () => anilist.fetchEpisodeSources(episodeId, server),
333+
`anilist:watch;${episodeId};${anilist.provider.name.toLowerCase()};${server};${isDub ? 'dub' : 'sub'}`,
334+
async () =>
335+
provider === 'zoro' || provider === 'animekai'
336+
? await anilist.fetchEpisodeSources(
337+
episodeId,
338+
server,
339+
isDub ? SubOrSub.DUB : SubOrSub.SUB,
340+
)
341+
: await anilist.fetchEpisodeSources(episodeId, server),
331342
600,
332343
),
333344
)
334-
: reply.status(200).send(await anilist.fetchEpisodeSources(episodeId, server));
345+
: reply
346+
.status(200)
347+
.send(
348+
provider === 'zoro' || provider === 'animekai'
349+
? await anilist.fetchEpisodeSources(
350+
episodeId,
351+
server,
352+
isDub ? SubOrSub.DUB : SubOrSub.SUB,
353+
)
354+
: await anilist.fetchEpisodeSources(episodeId, server),
355+
);
335356

336357
anilist = new META.Anilist(undefined, {
337358
url: process.env.PROXY as string | string[],
@@ -346,22 +367,23 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
346367

347368
//anilist staff info from character id (for example: voice actors)
348369
//http://127.0.0.1:3000/meta/anilist/staff/95095 (gives info of sukuna's voice actor (Junichi Suwabe) )
349-
fastify.get("/staff/:id", async (request: FastifyRequest, reply: FastifyReply) => {
370+
fastify.get('/staff/:id', async (request: FastifyRequest, reply: FastifyReply) => {
350371
const id = (request.params as { id: string }).id;
351372

352373
const anilist = generateAnilistMeta();
353374
try {
354375
redis
355-
? reply.status(200).send(
356-
await cache.fetch(
357-
redis,
358-
`anilist:staff;${id}`,
359-
async () => await anilist.fetchStaffById(Number(id)),
360-
60 * 60,
361-
),
362-
)
376+
? reply
377+
.status(200)
378+
.send(
379+
await cache.fetch(
380+
redis,
381+
`anilist:staff;${id}`,
382+
async () => await anilist.fetchStaffById(Number(id)),
383+
60 * 60,
384+
),
385+
)
363386
: reply.status(200).send(await anilist.fetchStaffById(Number(id)));
364-
365387
} catch (err: any) {
366388
reply.status(404).send({ message: err.message });
367389
}

0 commit comments

Comments
 (0)