-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
58 lines (50 loc) · 1.86 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
57
58
import express from 'express'
import youtubedl from 'youtube-dl'
import mcache from 'memory-cache'
const app = express()
/** CONFIG **/
const port = process.env.PORT || 4000
const requestCache = (duration) => {
return (req, res, next) => {
let key = `__express__${req.originalUrl}` || req.url
let cachedBody = mcache.get(key)
if (cachedBody) {
res.send(cachedBody)
} else {
res.sendResponse = res.send
res.send = (body) => {
mcache.put(key, body, duration)
res.sendResponse(body)
}
next()
}
}
}
/** ROUTES **/
/* home */
app.get('/', (req, res) => {
const text = '<h1>OpenLoad.CO API project</h1><p>This is just a scraper, if you have content you wish to be removed, please contact them (openload.co), not me, at their email: <a href="mailto:[email protected]">[email protected]</a>.</p><p>Full source code available on Github: <a href="https://www.github.com/milankragujevic/openload-api/" target="_blank">openload-api.git</a>. </p><hr><p>Copyright © 2018 Milan Kragujević. Some rights reserved. By using this API you may be committing copyright infringement. I am not responsible for the contents of the API. </p>'
res.send(text)
})
/* popular items, with pagination */
app.get('/:videoId', requestCache(60 * 60 * 12), (req, res) => {
if (req.params.videoId === '' || req.params.videoId === 'favicon.ico') { return }
let url = `https://openload.co/embed/${req.params.videoId}/`
youtubedl.getInfo(url, (err, info) => {
if (err) {
res.send({ status: false, error: 'Unknown error occurred!' })
}
res.send({
success: true,
data: {
id: info.id,
title: info.title,
stream: info.url,
thumbnail: info.thumbnail
}
})
})
})
/** LISTEN **/
app.listen(port)
console.log(`Listening on: ${port}`)