..
Viewing
tmdb.ts
94 lines (77 loc) • 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 | import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import { META, PROVIDERS_LIST } from '@consumet/extensions';
import { tmdbApi } from '../../main';
const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
fastify.get('/', (_, rp) => {
rp.status(200).send({
intro:
"Welcome to the tmdb provider: check out the provider's website @ https://www.themoviedb.org/",
routes: ['/:query', '/info/:id', '/watch/:episodeId'],
documentation: 'https://docs.consumet.org/#tag/tmdb',
});
});
fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => {
const query = (request.params as { query: string }).query;
const page = (request.query as { page: number }).page;
const tmdb = new META.TMDB(tmdbApi);
const res = await tmdb.search(query, page);
reply.status(200).send(res);
});
fastify.get('/info/:id', async (request: FastifyRequest, reply: FastifyReply) => {
const id = (request.params as { id: string }).id;
const type = (request.query as { type: string }).type;
const provider = (request.query as { provider?: string }).provider;
let tmdb = new META.TMDB(tmdbApi);
if (!type) return reply.status(400).send({ message: "The 'type' query is required" });
if (typeof provider !== 'undefined') {
const possibleProvider = PROVIDERS_LIST.MOVIES.find(
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase(),
);
tmdb = new META.TMDB(tmdbApi, possibleProvider);
}
const res = await tmdb.fetchMediaInfo(id, type);
reply.status(200).send(res);
});
fastify.get('/trending', async (request: FastifyRequest, reply: FastifyReply) => {
const validTimePeriods = new Set(['day', 'week'] as const);
type validTimeType = typeof validTimePeriods extends Set<infer T> ? T : undefined
const type = (request.query as { type?: string }).type || 'all';
let timePeriod = (request.query as { timePeriod?: validTimeType }).timePeriod || 'day';
// make day as default time period
if (!validTimePeriods.has(timePeriod)) timePeriod = 'day';
const page = (request.query as { page?: number }).page || 1;
const tmdb = new META.TMDB(tmdbApi);
try {
const res = await tmdb.fetchTrending(type, timePeriod, page);
reply.status(200).send(res);
} catch (err) {
reply.status(500).send({ message: 'Failed to fetch trending media.' });
}
});
fastify.get(
'/watch/:episodeId',
async (request: FastifyRequest, reply: FastifyReply) => {
const episodeId = (request.params as { episodeId: string }).episodeId;
const id = (request.query as { id: string }).id;
const provider = (request.query as { provider?: string }).provider;
let tmdb = new META.TMDB(tmdbApi);
if (typeof provider !== 'undefined') {
const possibleProvider = PROVIDERS_LIST.MOVIES.find(
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase(),
);
tmdb = new META.TMDB(tmdbApi, possibleProvider);
}
try {
const res = await tmdb
.fetchEpisodeSources(episodeId, id)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developer for help.' });
}
},
);
};
export default routes;
|
|