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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 | import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import { ANIME } 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 gogoanime = new ANIME.Gogoanime(process.env.GOGOANIME_URL);
const redisCacheTime = 60 * 60;
const redisPrefix = 'gogoanime:';
fastify.get('/', (_, rp) => {
rp.status(200).send({
intro:
"Welcome to the gogoanime provider: check out the provider's website @ https://www1.gogoanime.bid/",
routes: [
'/:query',
'/info/:id',
'/watch/:episodeId',
'/servers/:episodeId',
'/genre/:genre',
'/genre/list',
'/top-airing',
'/movies',
'/popular',
'/recent-episodes',
'/anime-list',
'/download',
],
documentation: 'https://docs.consumet.org/#tag/gogoanime',
});
});
fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => {
const query = (request.params as { query: string }).query;
const page = (request.query as { page: number }).page || 1;
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}search;${page};${query}`,
async () => await gogoanime.search(query, page),
redisCacheTime,
) : await gogoanime.search(query, page);
reply.status(200).send(res);
});
fastify.get('/info/:id', async (request: FastifyRequest, reply: FastifyReply) => {
const id = decodeURIComponent((request.params as { id: string }).id);
try {
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}info;${id}`,
async () => await gogoanime
.fetchAnimeInfo(id)
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchAnimeInfo(id)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
});
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 {
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}genre;${page};${genre}`,
async () => await gogoanime
.fetchGenreInfo(genre, page)
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchGenreInfo(genre, page)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
});
fastify.get('/genre/list', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}genre-list`,
async () => await gogoanime
.fetchGenreList()
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime * 24,
) : await gogoanime
.fetchGenreList()
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
});
fastify.get(
'/watch/:episodeId',
async (request: FastifyRequest, reply: FastifyReply) => {
const episodeId = (request.params as { episodeId: string }).episodeId;
const server = (request.query as { server: StreamingServers }).server;
if (server && !Object.values(StreamingServers).includes(server)) {
reply.status(400).send('Invalid server');
}
try {
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}watch;${server};${episodeId}`,
async () => await gogoanime
.fetchEpisodeSources(episodeId, server)
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchEpisodeSources(episodeId, server)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
},
);
fastify.get(
'/servers/:episodeId',
async (request: FastifyRequest, reply: FastifyReply) => {
const episodeId = (request.params as { episodeId: string }).episodeId;
try {
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}servers;${episodeId}`,
async () => await gogoanime
.fetchEpisodeServers(episodeId)
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchEpisodeServers(episodeId)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
},
);
fastify.get('/top-airing', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page ?? 1;
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}top-airing;${page}`,
async () => await gogoanime.fetchTopAiring(page),
redisCacheTime,
) : await gogoanime.fetchTopAiring(page);
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
});
fastify.get('/movies', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page ?? 1;
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}movies;${page}`,
async () => await gogoanime.fetchRecentMovies(page),
redisCacheTime,
) : await gogoanime.fetchRecentMovies(page);
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
});
fastify.get('/popular', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page ?? 1;
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}popular;${page}`,
async () => await gogoanime.fetchPopular(page),
redisCacheTime,
) : await gogoanime.fetchPopular(page);
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
});
fastify.get(
'/recent-episodes',
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const type = (request.query as { type: number }).type ?? 1;
const page = (request.query as { page: number }).page ?? 1;
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}recent-episodes;${page};${type}`,
async () => await gogoanime.fetchRecentEpisodes(page, type),
redisCacheTime,
) : await gogoanime.fetchRecentEpisodes(page, type);
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
},
);
fastify.get(
'/anime-list',
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page ?? 1;
const res = redis ? await cache.fetch(
redis as Redis,
`gogoanime:anime-list;${page}`,
async () => await gogoanime.fetchAnimeList(page),
redisCacheTime,
) : await gogoanime.fetchAnimeList(page);
reply.status(200).send(res);
} catch (err) {
reply
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
},
);
fastify.get('/download', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const downloadLink = (request.query as { link: string }).link;
if(!downloadLink){
reply.status(400).send('Invalid link');
}
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}download-${downloadLink}`,
async () => await gogoanime
.fetchDirectDownloadLink(downloadLink)
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime * 24,
) : await gogoanime
.fetchDirectDownloadLink(downloadLink, process.env.RECAPTCHATOKEN ?? '')
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch {
reply
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
});
};
export default routes;
|