This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
132 lines (104 loc) · 3.71 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require('express')
const app = express()
const cors = require('cors')
const myModule = require('./scraper/scraper')
const getJSON = myModule.getJSON
const scrapping = myModule.scrapping
const videoLink = myModule.getVideoLink
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const axios = require('axios')
const domain = 'http://103.194.171.18'
app.use(cors({ origin: '*' }))
app.get('/', function (req, res) {
res.json({
docs: 'Please read documentation at https://github.com/devnazir/api-filmapik'
})
})
function checkNumPage(req) {
return req.query.page ?? "1"
}
function checkVideo(req) {
return req.query.video ?? false
}
function checkSearch(req) {
return req.query.search.toLowerCase()
}
function maxResult(req) {
return req.query.maxResult ?? 21
}
async function scrapHomePage() {
const html = await axios(`${domain}`)
const response = html.data
const { window } = new JSDOM(response)
return window
}
app.get('/play', async function (req, res) {
try {
const movieId = req.query.id
const target = `https://www.googleapis.com/customsearch/v1?key=AIzaSyDEkTkys3gPYdUvcG7lIFHWSnh93X1xSho&cx=017552896205506834830:6zakuimpdcb&q=${movieId}`
const response = await axios(target)
if (response.data.items) {
let url = response.data.items[0].link
if (!url.includes('play')) {
url = `${url}/play`
}
const html = await scrapping(url)
const { window } = new JSDOM(html)
const iframe = window.document.querySelector('.iframe #myFrame').getAttribute('src')
res.json({ link: await videoLink(iframe) })
} else {
res.json({ error: "Movie not Found" })
}
} catch (err) {
console.log(err)
}
})
app.get('/search', function (req, res) {
const q = req.query.q
const url = `${domain}/page/${checkNumPage(req)}?s=${q}`
getJSON(res, url, checkNumPage(req), checkVideo(req), maxResult(req))
})
app.get('/latest', function (req, res) {
const url = `${domain}/latest/page/${checkNumPage(req)}`
getJSON(res, url, checkNumPage(req), checkVideo(req), maxResult(req))
})
app.get('/country', async function (req, res) {
try {
const country = []
const items = (await scrapHomePage()).document.querySelectorAll('#menu li#menu-item-13566 .menu-item')
items.forEach(item => {
country.push(item.textContent.toLowerCase())
})
if (country.includes(checkSearch(req))) {
const url = `${domain}/negara/${checkSearch(req)}/page/${checkNumPage(req)}`
getJSON(res, url, checkNumPage(req), checkVideo(req), maxResult(req))
return
}
res.json({ error: `Movies in ${checkSearch(req)} not found` })
} catch (err) {
console.log(err)
}
})
app.get('/category', async function (req, res) {
try {
const category = []
const items = (await scrapHomePage()).document.querySelectorAll('#menu li.menu-item .menu-item-object-category')
items.forEach(item => {
category.push(item.textContent.toLowerCase())
})
category.push('box-office')
if (category.includes(checkSearch(req))) {
const url = `${domain}/category/${checkSearch(req)}/page/${checkNumPage(req)}`
getJSON(res, url, checkNumPage(req), checkVideo(req), maxResult(req))
return
}
res.json({ error: `Category ${checkSearch(req)} not found` })
} catch (err) {
console.log(err)
}
})
app.get('*', function (req, res) {
res.json({ error: "Error" })
})
app.listen(process.env.PORT || 8000, () => console.log("Server work at localhost:8000"))