-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
167 lines (140 loc) · 4.62 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const express = require("express"),
app = express(),
cors = require('cors'),
path = require('path'),
swStats = require('swagger-stats'),
serveIndex = require('serve-index');
const langs = ["hindi", "tamil", "telugu", "malayalam", "kannada", "bengali", "marathi", "punjabi"],
sources = require("./sources"),
config = require('./config'),
manifest = require("./manifest");
app.set('trust proxy', true)
app.use(swStats.getMiddleware({
name: manifest.name,
version: manifest.version,
authentication: true,
onAuthenticate: function (req, username, password) {
// simple check for username and password
const User = process.env.USER ? process.env.USER : 'stremio'
const Pass = process.env.PASS ? process.env.PASS : 'stremioIsTheBest'
return ((username === User
&& (password === Pass)))
}
}));
app.use((req, res, next) => {
req.setTimeout(25 * 1000); // timeout time
req.socket.removeAllListeners('timeout');
req.socket.once('timeout', () => {
req.timedout = true;
res.status(504).end();
});
if (!req.timedout) next()
});
console.log(`Swagger-Stats accessible at: ${config.local}/swagger-stats`)
app.use('/logs',
(req, res, next) => {
res.set('Cache-Control', 'no-store');
next();
},
express.static(path.join(__dirname, 'logs'), {etag: false }),
serveIndex('logs', { 'icons': true, 'view': 'details' })
)
app.use('/configure', express.static(path.join(__dirname, 'vue', 'dist')));
app.use('/assets', express.static(path.join(__dirname, 'vue', 'dist', 'assets')));
app.use(cors())
app.get('/', (_, res) => {
res.redirect('/configure/')
res.end();
});
app.get('/:configuration?/configure/', (_, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('content-type', 'text/html');
res.sendFile(path.join(__dirname, 'vue', 'dist', 'index.html'));
});
app.get('/manifest.json', (_, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('Content-Type', 'application/json');
manifest.behaviorHints.configurationRequired = true;
manifest.catalogs = [];
res.send(manifest);
res.end();
});
app.get('/:configuration?/manifest.json', (req, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('Content-Type', 'application/json');
const { configuration } = req.params;
manifest.behaviorHints.configurationRequired = false;
manifest.catalogs = [{
"type": "movie",
"id": `${configuration}`,
"name": `movies - ${configuration}`,
"extra":
[{
"name": "search",
"isRequired": true
}
]
}];
if (langs.includes(configuration)) res.send(manifest);
res.end();
});
app.get('/:configuration?/catalog/movie/:id/:extra?.json', async (req, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('Content-Type', 'application/json');
try {
console.log(req.params);
let { id, extra } = req.params;
let metas;
if (!langs.includes(id)) {
id = id.split('movies')[0];
if (!id || !langs.includes(id)) throw new Error("invalide catalog id");
}
if (extra) extra = new URLSearchParams(extra);
console.log(extra)
if (extra.has("search")) {
console.log(extra.get("search"))
metas = await sources.search(id, extra.get("search"))
}
if (metas) res.send({ metas: metas })
else res.send({ metas: [] })
res.end();
} catch (e) {
console.error(e)
}
})
app.get('/:configuration?/meta/movie/:id/:extra?.json', async (req, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('Content-Type', 'application/json');
try {
console.log(req.params);
const { id } = req.params;
let meta;
if (id.startsWith("einthusan_id:")) {
meta = await sources.meta(id)
}
if (meta) res.send({ meta: meta })
else res.send({ meta: [] })
res.end();
} catch (e) {
console.error(e)
}
})
app.get('/:configuration?/stream/movie/:id/:extra?.json', async (req, res) => {
res.setHeader('Cache-Control', 'max-age=86400,staleRevalidate=stale-while-revalidate, staleError=stale-if-error, public');
res.setHeader('Content-Type', 'application/json');
try {
console.log(req.params);
const { id } = req.params;
let streams;
if (id.startsWith("einthusan_id:")) {
streams = await sources.stream(id)
}
if (streams) res.send({ streams: streams })
else res.send({ streams: [] })
res.end();
res.end();
} catch (e) {
console.error(e)
}
})
module.exports = app