This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
150 lines (122 loc) · 4.29 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
const express = require("express");
const app = express();
const cors = require('cors');
const path = require('path');
const { subtitles, downloadUrl } = require('./subscene');
const manifest = require("./manifest.json");
const {CacheControl} = require('./config');
const languages = require('./languages.json');
const swStats = require('swagger-stats')
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) => {
console.log("reqpath : ", req.path)
req.setTimeout(60 * 1000); // timeout time
//long timeout, still give time to cache subs, next play will load from cache
req.socket.removeAllListeners('timeout');
req.socket.once('timeout', () => {
req.timedout = true;
//res.setHeader('Cache-Control', CacheControl.off);
res.status(504).end();
});
if (!req.timedout) next()
});
app.set('trust proxy', true)
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', (req, res) => {
res.setHeader('Cache-Control', CacheControl.oneDay);
res.setHeader('content-type', 'text/html');
res.sendFile(path.join(__dirname, 'vue', 'dist', 'index.html'));
});
app.get('/manifest.json', (_, res) => {
res.setHeader('Cache-Control', CacheControl.oneDay);
res.setHeader('Content-Type', 'application/json');
manifest.behaviorHints.configurationRequired = true;
res.send(manifest);
res.end();
});
app.get('/:configuration?/manifest.json', (_, res) => {
res.setHeader('Cache-Control', CacheControl.oneDay);
res.setHeader('Content-Type', 'application/json');
manifest.behaviorHints.configurationRequired = false;
res.send(manifest);
res.end();
});
app.get('/:configuration?/subtitles/:type/:id/:extra?.json', async(req, res) => {
try{
res.setHeader('Content-Type', 'application/json');
console.log(req.params);
var { configuration, type, id } = req.params;
if (configuration && languages[configuration]) {
let lang = configuration;
const subs = await subtitles(type, id, lang)
if(subs){
res.setHeader('Cache-Control', CacheControl.oneHour);
res.send(JSON.stringify({ subtitles: subs }));
} else throw "no subs"
} else throw "no config"
}catch(e){
res.setHeader('Cache-Control', CacheControl.off);
console.error(e);
res.send({ subtitles: [] });
}
})
/*
app.get('/:subtitles/:name/:language/:id/:episode?\.:extension?', limiter, (req, res) => {
console.log(req.params);
let { subtitles, name, language, id, episode, extension } = req.params;
try {
let path = `/${subtitles}/${name}/${language}/${id}`
res.setHeader('Cache-Control', 'max-age=86400, public');
res.setHeader('responseEncoding', 'null');
res.setHeader('Content-Type', 'arraybuffer/json');
console.log(path);
proxyStream(path, episode).then(response => {
res.send(response);
}).catch(err => { console.log(err) })
} catch (err) {
console.log(err)
return res.send("Couldn't get the subtitle.")
}
});
*/
const sub2vtt = require('sub2vtt');
app.get('/sub.vtt', async (req, res,next) => {
try {
let url,proxy,episode;
if (req?.query?.proxy) proxy = JSON.parse(Buffer.from(req.query.proxy, 'base64').toString());
if (req?.query?.from) url = req.query.from
else throw 'error: no url';
if (req?.query?.episode) episode = req.query.episode
proxy = {responseType: "buffer", "user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36'}
url = await downloadUrl(url);
console.log({url,proxy,episode})
let sub = new sub2vtt(url ,proxy,episode);
let file = await sub.getSubtitle();
if (!file?.subtitle?.length) throw file.status
res.setHeader('Cache-Control', CacheControl.oneDay);
res.setHeader('Content-Type', 'text/vtt;charset=UTF-8');
res.send(file.subtitle);
res.end;
} catch (e) {
console.error(e);
//next(e);
}
})
module.exports = app