Skip to content

Commit

Permalink
fix: duplicate import, Updated README, and run lint (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
Layendan authored Oct 20, 2023
1 parent 41a2bed commit ea82c58
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 89 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ $ docker run -p 3000:3000 riimuru/consumet-api
```
This will start the server on port 3000. You can access the server at http://localhost:3000/, And can change the port by changing the -p option to `-p <port>:3000`.

Be sure to set `NODE_ENV` to `PROD` in your environment variables when running your own instance.
Check out the `.env.example` file for more information.

You can add `-d` flag to run the server in detached mode.

### Heroku
Expand Down
34 changes: 18 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import anime from './routes/anime';
import manga from './routes/manga';
import comics from './routes/comics';
import lightnovels from './routes/light-novels';
import fs from 'fs';
import movies from './routes/movies';
import meta from './routes/meta';
import news from './routes/news';
Expand Down Expand Up @@ -105,28 +104,31 @@ export const tmdbApi = process.env.TMDB_KEY && process.env.TMDB_KEY;
});

// set interval to delete expired sessions every 1 hour
setInterval(() => {
const currentTime = new Date();
for (const [ip, session] of map.entries()) {
const { expiresIn } = session;
const sessionTime = new Date(expiresIn);

// check if the session is expired
if (currentTime.getTime() > sessionTime.getTime()) {
console.log('session expired for', ip);
// if expired, delete the session and continue
map.delete(ip);
setInterval(
() => {
const currentTime = new Date();
for (const [ip, session] of map.entries()) {
const { expiresIn } = session;
const sessionTime = new Date(expiresIn);

// check if the session is expired
if (currentTime.getTime() > sessionTime.getTime()) {
console.log('session expired for', ip);
// if expired, delete the session and continue
map.delete(ip);
}
}
}
}, 1000 * 60 * 60);
},
1000 * 60 * 60,
);
}

console.log(chalk.green(`Starting server on port ${PORT}... 🚀`));
if (!process.env.REDIS_HOST)
console.warn(chalk.yellowBright('Redis not found. Cache disabled.'));
if (!process.env.TMDB_KEY)
console.warn(
chalk.yellowBright('TMDB api key not found. the TMDB meta route may not work.')
chalk.yellowBright('TMDB api key not found. the TMDB meta route may not work.'),
);

await fastify.register(books, { prefix: '/books' });
Expand All @@ -147,7 +149,7 @@ export const tmdbApi = process.env.TMDB_KEY && process.env.TMDB_KEY;
process.env.NODE_ENV === 'DEMO'
? 'This is a demo of the api. You should only use this for testing purposes.'
: ''
}`
}`,
);
});
fastify.get('*', (request, reply) => {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/anime/9anime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
{
url: process.env.NINE_ANIME_PROXY as string,
},
process.env?.NINE_ANIME_HELPER_KEY as string
process.env?.NINE_ANIME_HELPER_KEY as string,
);

fastify.get('/', (_, rp) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Contact developer for help.' });
}
}
},
);

fastify.get(
Expand All @@ -87,7 +87,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
}
},
);

fastify.get('/helper', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/anime/animefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const res = await animefox.fetchRecentEpisodes(page);

reply.status(200).send(res);
}
},
);

fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/anime/animepahe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Contact developer for help.' });
}
}
},
);
};

Expand Down
6 changes: 3 additions & 3 deletions src/routes/anime/gogoanime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
}
},
);

fastify.get(
Expand All @@ -102,7 +102,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
}
},
);

fastify.get('/top-airing', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down Expand Up @@ -134,7 +134,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Contact developers for help.' });
}
}
},
);
};

Expand Down
4 changes: 2 additions & 2 deletions src/routes/anime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
};

queries.animeProvider = decodeURIComponent(
(request.params as { animeProvider: string; page: number }).animeProvider
(request.params as { animeProvider: string; page: number }).animeProvider,
);

queries.page = (request.query as { animeProvider: string; page: number }).page;

if (queries.page! < 1) queries.page = 1;

const provider = PROVIDERS_LIST.ANIME.find(
(provider: any) => provider.toString.name === queries.animeProvider
(provider: any) => provider.toString.name === queries.animeProvider,
);

try {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/anime/marin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
async (request: FastifyRequest, reply: FastifyReply) => {
const page = (request.query as { page: number }).page;
reply.status(200).send(await marin.recentEpisodes(page));
}
},
);

fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down Expand Up @@ -71,7 +71,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Contact developer for help.' });
}
}
},
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/routes/anime/zoro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const res = await zoro.fetchRecentEpisodes(page);

reply.status(200).send(res);
}
},
);

fastify.get('/info', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/light-novels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

queries.lightNovelProvider = decodeURIComponent(
(request.params as { lightNovelProvider: string; page: number })
.lightNovelProvider
.lightNovelProvider,
);

queries.page = (request.query as { lightNovelProvider: string; page: number }).page;

if (queries.page! < 1) queries.page = 1;

const provider = PROVIDERS_LIST.LIGHT_NOVELS.find(
(provider: any) => provider.toString.name === queries.lightNovelProvider
(provider: any) => provider.toString.name === queries.lightNovelProvider,
);

try {
Expand All @@ -42,7 +42,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
} catch (err) {
reply.status(500).send('Something went wrong. Please try again later.');
}
}
},
);
};

Expand Down
4 changes: 2 additions & 2 deletions src/routes/manga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
};

queries.mangaProvider = decodeURIComponent(
(request.params as { mangaProvider: string; page: number }).mangaProvider
(request.params as { mangaProvider: string; page: number }).mangaProvider,
);

queries.page = (request.query as { mangaProvider: string; page: number }).page;

if (queries.page! < 1) queries.page = 1;

const provider = PROVIDERS_LIST.MANGA.find(
(provider: any) => provider.toString.name === queries.mangaProvider
(provider: any) => provider.toString.name === queries.mangaProvider,
);

try {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/manga/mangadex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Please try again later.' });
}
}
},
);
};

Expand Down
4 changes: 2 additions & 2 deletions src/routes/meta/anilist-manga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

if (typeof provider !== 'undefined') {
const possibleProvider = PROVIDERS_LIST.MANGA.find(
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase()
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase(),
);
anilist = new META.Anilist.Manga(possibleProvider);
}
Expand Down Expand Up @@ -57,7 +57,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

if (typeof provider !== 'undefined') {
const possibleProvider = PROVIDERS_LIST.MANGA.find(
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase()
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase(),
);
anilist = new META.Anilist.Manga(possibleProvider);
}
Expand Down
40 changes: 20 additions & 20 deletions src/routes/meta/anilist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
id,
year,
status,
season
season,
);

reply.status(200).send(res);
}
},
);

fastify.get('/trending', async (request: FastifyRequest, reply: FastifyReply) => {
Expand All @@ -97,8 +97,8 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
redis as Redis,
`anilist:trending;${page};${perPage}`,
async () => await anilist.fetchTrendingAnime(page, perPage),
60 * 60
)
60 * 60,
),
)
: reply.status(200).send(await anilist.fetchTrendingAnime(page, perPage));
});
Expand All @@ -117,8 +117,8 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
redis as Redis,
`anilist:popular;${page};${perPage}`,
async () => await anilist.fetchPopularAnime(page, perPage),
60 * 60
)
60 * 60,
),
)
: reply.status(200).send(await anilist.fetchPopularAnime(page, perPage));
});
Expand All @@ -139,11 +139,11 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
perPage,
weekStart,
weekEnd,
notYetAired
notYetAired,
);

reply.status(200).send(res);
}
},
);

fastify.get('/genre', async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down Expand Up @@ -178,7 +178,7 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const res = await anilist.fetchRecentEpisodes(provider, page);

reply.status(200).send(res);
}
},
),
fastify.get('/random-anime', async (request: FastifyRequest, reply: FastifyReply) => {
const anilist = generateAnilistMeta();
Expand Down Expand Up @@ -230,10 +230,10 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
anilist.fetchEpisodesListById(
id,
dub as boolean,
fetchFiller as boolean
fetchFiller as boolean,
),
dayOfWeek === 0 || dayOfWeek === 6 ? 60 * 120 : (60 * 60) / 2
)
dayOfWeek === 0 || dayOfWeek === 6 ? 60 * 120 : (60 * 60) / 2,
),
)
: reply
.status(200)
Expand Down Expand Up @@ -281,13 +281,13 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
`anilist:info;${id};${isDub};${fetchFiller};${anilist.provider.name.toLowerCase()}`,
async () =>
anilist.fetchAnimeInfo(id, isDub as boolean, fetchFiller as boolean),
dayOfWeek === 0 || dayOfWeek === 6 ? 60 * 120 : (60 * 60) / 2
)
dayOfWeek === 0 || dayOfWeek === 6 ? 60 * 120 : (60 * 60) / 2,
),
)
: reply
.status(200)
.send(
await anilist.fetchAnimeInfo(id, isDub as boolean, fetchFiller as boolean)
await anilist.fetchAnimeInfo(id, isDub as boolean, fetchFiller as boolean),
);
} catch (err: any) {
reply.status(500).send({ message: err.message });
Expand Down Expand Up @@ -325,8 +325,8 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
redis,
`anilist:watch;${episodeId};${anilist.provider.name.toLowerCase()};${server}`,
async () => anilist.fetchEpisodeSources(episodeId, server),
600
)
600,
),
)
: reply.status(200).send(await anilist.fetchEpisodeSources(episodeId, server));

Expand All @@ -338,14 +338,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
.status(500)
.send({ message: 'Something went wrong. Contact developer for help.' });
}
}
},
);
};

const generateAnilistMeta = (provider: string | undefined = undefined): Anilist => {
if (typeof provider !== 'undefined') {
let possibleProvider = PROVIDERS_LIST.ANIME.find(
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase()
(p) => p.name.toLowerCase() === provider.toLocaleLowerCase(),
);

if (possibleProvider instanceof NineAnime) {
Expand All @@ -354,7 +354,7 @@ const generateAnilistMeta = (provider: string | undefined = undefined): Anilist
{
url: process.env?.NINE_ANIME_PROXY as string,
},
process.env?.NINE_ANIME_HELPER_KEY as string
process.env?.NINE_ANIME_HELPER_KEY as string,
);
}

Expand Down
Loading

0 comments on commit ea82c58

Please sign in to comment.