-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.ts
66 lines (58 loc) · 2.02 KB
/
server.ts
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
import express from 'express';
import packageJson from './package.json';
import { csfd } from './src';
import { CSFDFilmTypes } from './src/interfaces/global';
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (_, res) => {
res.json({
name: packageJson.name,
version: packageJson.version,
docs: packageJson.homepage,
links: ['/movie/:id', '/creator/:id', '/search/:query', '/user-ratings/:id']
});
});
app.get(['/movie/', '/creator/', '/search/', '/user-ratings/'], (req, res) => {
res.json({ error: `ID is missing. Provide ID like this: ${req.url}${req.url.endsWith('/') ? '' : '/'}1234` });
});
app.get('/movie/:id', async (req, res) => {
try {
const movie = await csfd.movie(+req.params.id);
res.json(movie);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch movie data' });
}
});
app.get('/creator/:id', async (req, res) => {
try {
const result = await csfd.creator(+req.params.id);
res.json(result);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch creator data: ' + error });
}
});
app.get('/search/:query', async (req, res) => {
try {
const result = await csfd.search(req.params.query);
res.json(result);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch search data: ' + error });
}
});
app.get('/user-ratings/:id', async (req, res) => {
const { allPages, allPagesDelay, excludes, includesOnly } = req.query;
try {
const result = await csfd.userRatings(req.params.id, {
allPages: allPages === 'true',
allPagesDelay: allPagesDelay ? +allPagesDelay : undefined,
excludes: excludes ? (excludes as string).split(',') as CSFDFilmTypes[] : undefined,
includesOnly: includesOnly ? (includesOnly as string).split(',') as CSFDFilmTypes[] : undefined
});
res.json(result);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch user-ratings data: ' + error });
}
});
app.listen(port, () => {
console.log(`API is running on http://localhost:${port}`);
});