-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (78 loc) · 1.99 KB
/
index.js
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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const PirateBay = require('thepiratebay');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const searchCache = {};
const movies = async() =>{
const moviesTorrents = await PirateBay.topTorrents(201);
const promises = [];
moviesTorrents.forEach((data) =>{
promises.push(data);
});
return await Promise.all(promises);
}
const tvShows = async() =>{
const tvShowTorrents = await PirateBay.topTorrents(205);
const promises = [];
tvShowTorrents.forEach((data) =>{
promises.push(data);
});
return await Promise.all(promises)
};
const topGames = async() =>{
const gamesTorrents = await PirateBay.topTorrents(400);
const promises = [];
gamesTorrents.forEach((data) =>{
promises.push(data)
})
return await Promise.all(promises);
};
const searchContent = async (q) =>{
if(searchCache[q]){
console.log('Serving from cache: ' , q);
return Promise.resolve(searchCache[q]);
}
const result = await PirateBay.search(q , {
category: 'video',
page: 0 ,
})
const promises = [];
result.forEach((data) =>{
promises.push(data.name)
});
searchCache[q] = promises;
return await Promise.all(promises);
}
app.get('/api/v1' , (req , res) =>{
res.json({
message: '✨ Welcome To The Main End Point ✨'
});
});
app.get('/api/v1/TopMovies' , (req , res) =>{
movies().then((data) =>{
res.status(200).json(data);
});
});
app.get('/api/v1/TopTvShows' , (req , res) =>{
tvShows().then((data) =>{
res.status(200).json(data)
});
});
app.get('/api/v1/TopGames' , (req , res) =>{
topGames().then((data) =>{
res.status(200).json(data)
});
});
app.get('/api/v1/search/:query/' , (req , res) =>{
let q = req.params.query;
searchContent(q).then((data) =>{
res.status(200).json(data);
});
});
const port = process.env.PORT || 8001;
app.listen(port , () =>{
console.log(`\n🚀 Listening on port ${port}`);
})