forked from AirplanegoBrr/Torrent-Search-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (44 loc) · 1.5 KB
/
app.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
const express = require('express');
const combo = require("./torrent/COMBO")
const path = require('path');
let torrents = require("./torrent/torrents")()
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
function sort(list) {
return list.sort((a, b) => {
const seedersA = a.Seeders ?? 0; // Use 0 if Seeders is undefined or null
const seedersB = b.Seeders ?? 0;
return seedersB - seedersA; // Sort in descending order
});
}
app.use('/api/:website/:query/:page?', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
let website = (req.params.website).toLowerCase();
let query = req.params.query;
let page = req.params.page;
if (website == "all") {
combo(query, page).then(v => {
// console.log(v)
res.json(sort(v))
})
} else if (torrents[website]) {
torrents[website](query, page).then((v) => {
// console.log(v)
res.json(sort(v))
})
} else {
res.json({
error: `Please select "${Object.keys(torrents).join(" | ")}"`
})
}
});
app.get("/api/torrents", (req, res) => {
res.json(Object.keys(torrents))
})
app.use('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const PORT = process.env.PORT || 3001;
console.log('Listening on PORT : ', PORT);
app.listen(PORT);