-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (61 loc) · 1.74 KB
/
server.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
require('dotenv').config()
const fetch = require('node-fetch')
const path = require('path')
const cheerio = require('cheerio')
const http = require('http');
const express = require("express");
async function queryMovie(movie) {
const data = await getMovies(movie)
const $ = cheerio.load(data)
const names = $(".detName").map((i, elem) => {
return elem.childNodes[1].firstChild.data
});
const links = $('a[title="Download this torrent using magnet"]').map((i, elem) => elem.attribs.href);
const array = []
for (let i = 0; i < names.length; i++) {
array.push({
name: names[i],
link: links[i]
})
}
return array
}
async function getMovies(movie) {
return new Promise((res, rej) => {
fetch(process.env.BASE_URL + movie, {
"headers": {
"upgrade-insecure-requests": "1",
},
"referrer": "https://www.pirate-bay.net/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "omit"
})
.then(res => {
return res.text()
})
.then(data => {
res(data)
})
.catch(err => rej(err))
})
}
const hostname = process.env.HOST
const port = process.env.PORT;
const app = express();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static("dist"));
const server = http.createServer(app)
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
app.get("/movies", async function (req, res) {
const result = await queryMovie(req.query.q)
res.send(result)
});