From d3476668a96f40c6b88e9562ce25935d53dc5cef Mon Sep 17 00:00:00 2001 From: Durgesh Kumar Dwivedi Date: Fri, 15 Nov 2024 21:43:40 +0530 Subject: [PATCH] fix(manga | movies): Add missing mangaId in fetchChapterPages, new sflix provider and changes in /servers endpoint error logging in movies routes, add server params in dramacool's episode fetching --- src/routes/manga/mangakakalot.ts | 6 +- src/routes/movies/dramacool.ts | 4 +- src/routes/movies/flixhq.ts | 6 + src/routes/movies/goku.ts | 6 + src/routes/movies/index.ts | 2 + src/routes/movies/movieshd.ts | 6 + src/routes/movies/sflix.ts | 231 +++++++++++++++++++++++++++++++ 7 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 src/routes/movies/sflix.ts diff --git a/src/routes/manga/mangakakalot.ts b/src/routes/manga/mangakakalot.ts index c0c42500..7b3154c1 100644 --- a/src/routes/manga/mangakakalot.ts +++ b/src/routes/manga/mangakakalot.ts @@ -41,13 +41,17 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { fastify.get('/read', async (request: FastifyRequest, reply: FastifyReply) => { const chapterId = (request.query as { chapterId: string }).chapterId; + const mangaId = (request.query as { mangaId: string }).mangaId; if (typeof chapterId === 'undefined') return reply.status(400).send({ message: 'chapterId is required' }); + if (typeof mangaId === 'undefined') + return reply.status(400).send({ message: 'mangaId is required' }); + try { const res = await mangakakalot - .fetchChapterPages(chapterId) + .fetchChapterPages(chapterId,mangaId) .catch((err: Error) => reply.status(404).send({ message: err.message })); reply.status(200).send(res); diff --git a/src/routes/movies/dramacool.ts b/src/routes/movies/dramacool.ts index 8348955a..73539747 100644 --- a/src/routes/movies/dramacool.ts +++ b/src/routes/movies/dramacool.ts @@ -56,13 +56,13 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { fastify.get('/watch', async (request: FastifyRequest, reply: FastifyReply) => { const episodeId = (request.query as { episodeId: string }).episodeId; // const mediaId = (request.query as { mediaId: string }).mediaId; - // const server = (request.query as { server: StreamingServers }).server; + const server = (request.query as { server: StreamingServers }).server; if (typeof episodeId === 'undefined') return reply.status(400).send({ message: 'episodeId is required' }); try { const res = await dramacool - .fetchEpisodeSources(episodeId) + .fetchEpisodeSources(episodeId,server) .catch((err) => reply.status(404).send({ message: 'Media Not found.' })); reply.status(200).send(res); diff --git a/src/routes/movies/flixhq.ts b/src/routes/movies/flixhq.ts index 8506b2c1..317256bc 100644 --- a/src/routes/movies/flixhq.ts +++ b/src/routes/movies/flixhq.ts @@ -158,6 +158,12 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { fastify.get('/servers', async (request: FastifyRequest, reply: FastifyReply) => { const episodeId = (request.query as { episodeId: string }).episodeId; const mediaId = (request.query as { mediaId: string }).mediaId; + + if (typeof episodeId === 'undefined') + return reply.status(400).send({ message: 'episodeId is required' }); + if (typeof mediaId === 'undefined') + return reply.status(400).send({ message: 'mediaId is required' }); + try { let res = redis ? await cache.fetch( diff --git a/src/routes/movies/goku.ts b/src/routes/movies/goku.ts index 07519800..fbc74048 100644 --- a/src/routes/movies/goku.ts +++ b/src/routes/movies/goku.ts @@ -180,6 +180,12 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { async (request: FastifyRequest, reply: FastifyReply) => { const episodeId = (request.query as { episodeId: string }).episodeId; const mediaId = (request.query as { mediaId: string }).mediaId; + + if (typeof episodeId === "undefined") + return reply.status(400).send({ message: "episodeId is required" }); + if (typeof mediaId === "undefined") + return reply.status(400).send({ message: "mediaId is required" }); + try { let res = redis ? await cache.fetch( diff --git a/src/routes/movies/index.ts b/src/routes/movies/index.ts index bb6a575d..7276e449 100644 --- a/src/routes/movies/index.ts +++ b/src/routes/movies/index.ts @@ -7,6 +7,7 @@ import dramacool from './dramacool'; import fmovies from './fmovies'; import goku from './goku'; import movieshd from './movieshd'; +import sflix from './sflix'; const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { await fastify.register(flixhq, { prefix: '/flixhq' }); await fastify.register(viewasian, { prefix: '/viewasian' }); @@ -14,6 +15,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { await fastify.register(fmovies, { prefix: '/fmovies' }); await fastify.register(goku, { prefix: '/goku' }); await fastify.register(movieshd, { prefix: '/movieshd' }); + await fastify.register(sflix, { prefix: '/sflix' }); fastify.get('/', async (request: any, reply: any) => { reply.status(200).send('Welcome to Consumet Movies and TV Shows'); }); diff --git a/src/routes/movies/movieshd.ts b/src/routes/movies/movieshd.ts index 2d9f625f..3b97111c 100644 --- a/src/routes/movies/movieshd.ts +++ b/src/routes/movies/movieshd.ts @@ -180,6 +180,12 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { async (request: FastifyRequest, reply: FastifyReply) => { const episodeId = (request.query as { episodeId: string }).episodeId; const mediaId = (request.query as { mediaId: string }).mediaId; + + if (typeof episodeId === "undefined") + return reply.status(400).send({ message: "episodeId is required" }); + if (typeof mediaId === "undefined") + return reply.status(400).send({ message: "mediaId is required" }); + try { let res = redis ? await cache.fetch( diff --git a/src/routes/movies/sflix.ts b/src/routes/movies/sflix.ts new file mode 100644 index 00000000..f0d4d793 --- /dev/null +++ b/src/routes/movies/sflix.ts @@ -0,0 +1,231 @@ +import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify'; +import { MOVIES } from '@consumet/extensions'; +import { StreamingServers } from '@consumet/extensions/dist/models'; + +import cache from '../../utils/cache'; +import { redis } from '../../main'; +import { Redis } from 'ioredis'; + +const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { + const sflix = new MOVIES.SFlix(); + + fastify.get('/', (_, rp) => { + rp.status(200).send({ + intro: + "Welcome to the sflix provider: check out the provider's website @ https://sflix.to/", + routes: ['/:query', '/info', '/watch','/recent-shows','/recent-movies','/trending','/servers','/country','/genre'], + documentation: 'https://docs.consumet.org/#tag/sflix', + }); + }); + + fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => { + const query = decodeURIComponent((request.params as { query: string }).query); + + const page = (request.query as { page: number }).page; + + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:${query}:${page}`, + async () => await sflix.search(query, page ? page : 1), + 60 * 60 * 6, + ) + : await sflix.search(query, page ? page : 1); + + reply.status(200).send(res); + }); + + fastify.get('/recent-shows', async (request: FastifyRequest, reply: FastifyReply) => { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:recent-shows`, + async () => await sflix.fetchRecentTvShows(), + 60 * 60 * 3, + ) + : await sflix.fetchRecentTvShows(); + + reply.status(200).send(res); + }); + + fastify.get('/recent-movies', async (request: FastifyRequest, reply: FastifyReply) => { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:recent-movies`, + async () => await sflix.fetchRecentMovies(), + 60 * 60 * 3, + ) + : await sflix.fetchRecentMovies(); + + reply.status(200).send(res); + }); + + fastify.get('/trending', async (request: FastifyRequest, reply: FastifyReply) => { + const type = (request.query as { type: string }).type; + try { + if (!type) { + const res = { + results: [ + ...(await sflix.fetchTrendingMovies()), + ...(await sflix.fetchTrendingTvShows()), + ], + }; + return reply.status(200).send(res); + } + + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:trending:${type}`, + async () => + type === 'tv' + ? await sflix.fetchTrendingTvShows() + : await sflix.fetchTrendingMovies(), + 60 * 60 * 3, + ) + : type === 'tv' + ? await sflix.fetchTrendingTvShows() + : await sflix.fetchTrendingMovies(); + + reply.status(200).send(res); + } catch (error) { + reply.status(500).send({ + message: + 'Something went wrong. Please try again later. or contact the developers.', + }); + } + }); + + fastify.get('/info', async (request: FastifyRequest, reply: FastifyReply) => { + const id = (request.query as { id: string }).id; + + if (typeof id === 'undefined') + return reply.status(400).send({ + message: 'id is required', + }); + + try { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:info:${id}`, + async () => await sflix.fetchMediaInfo(id), + 60 * 60 * 3, + ) + : await sflix.fetchMediaInfo(id); + + reply.status(200).send(res); + } catch (err) { + reply.status(500).send({ + message: + 'Something went wrong. Please try again later. or contact the developers.', + }); + } + }); + + fastify.get('/watch', async (request: FastifyRequest, reply: FastifyReply) => { + const episodeId = (request.query as { episodeId: string }).episodeId; + const mediaId = (request.query as { mediaId: string }).mediaId; + const server = (request.query as { server: StreamingServers }).server; + + if (typeof episodeId === 'undefined') + return reply.status(400).send({ message: 'episodeId is required' }); + if (typeof mediaId === 'undefined') + return reply.status(400).send({ message: 'mediaId is required' }); + + if (server && !Object.values(StreamingServers).includes(server)) + return reply.status(400).send({ message: 'Invalid server query' }); + + try { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:watch:${episodeId}:${mediaId}:${server}`, + async () => await sflix.fetchEpisodeSources(episodeId, mediaId, server), + 60 * 30, + ) + : await sflix.fetchEpisodeSources(episodeId, mediaId, server); + + reply.status(200).send(res); + } catch (err) { + reply + .status(500) + .send({ message: 'Something went wrong. Please try again later.' }); + } + }); + + fastify.get('/servers', async (request: FastifyRequest, reply: FastifyReply) => { + const episodeId = (request.query as { episodeId: string }).episodeId; + const mediaId = (request.query as { mediaId: string }).mediaId; + + if (typeof episodeId === 'undefined') + return reply.status(400).send({ message: 'episodeId is required' }); + if (typeof mediaId === 'undefined') + return reply.status(400).send({ message: 'mediaId is required' }); + + try { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:servers:${episodeId}:${mediaId}`, + async () => await sflix.fetchEpisodeServers(episodeId, mediaId), + 60 * 30, + ) + : await sflix.fetchEpisodeServers(episodeId, mediaId); + + reply.status(200).send(res); + } catch (error) { + reply.status(500).send({ + message: + 'Something went wrong. Please try again later. or contact the developers.', + }); + } + }); + + fastify.get('/country/:country', async (request: FastifyRequest, reply: FastifyReply) => { + const country = (request.params as { country: string }).country; + const page = (request.query as { page: number }).page ?? 1; + try { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:country:${country}:${page}`, + async () => await sflix.fetchByCountry(country, page), + 60 * 60 * 3, + ) + : await sflix.fetchByCountry(country, page); + + reply.status(200).send(res); + } catch (error) { + reply.status(500).send({ + message: + 'Something went wrong. Please try again later. or contact the developers.', + }); + } + }); + + + fastify.get('/genre/:genre', async (request: FastifyRequest, reply: FastifyReply) => { + const genre = (request.params as { genre: string }).genre; + const page = (request.query as { page: number }).page ?? 1; + try { + let res = redis + ? await cache.fetch( + redis as Redis, + `sflix:genre:${genre}:${page}`, + async () => await sflix.fetchByGenre(genre, page), + 60 * 60 * 3, + ) + : await sflix.fetchByGenre(genre, page); + + reply.status(200).send(res); + } catch (error) { + reply.status(500).send({ + message: + 'Something went wrong. Please try again later. or contact the developers.', + }); + } + }); +}; +export default routes;