-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
126 lines (107 loc) · 4.3 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
var addons = require("stremio-addons");
var http = require("http");
var _ = require("underscore");
var moment = require("moment");
var url = require("url");
/* Basic glue
*/
var find = require("./lib/find");
var tracks = require("./lib/tracks");
var hash = require("./lib/hash");
var cacheGet, cacheSet;
// In memory, allow this to be overridden
cacheGet = function (domain, key, cb) { cb(null, null) }
cacheSet = function(domain, key, value, ttl) { }
function subsFindCached(args, cb) {
if (! args) return cb({ code: 14, message: "args required" });
if (! (args.query || args.hash)) return cb({ code: 13, message: "query/hash required" });
var id = args.hash ? args.hash : (args.query.videoHash || args.query.itemHash || args.query.item_hash); // item_hash is the obsolete property
function prep(subtitles) {
if (!args.supportsZip) subtitles.all = subtitles.all.filter(function(sub) { return sub.url && !sub.url.match("zip$") });
return subtitles;
}
cacheGet("subtitles-v3", id, function(err, subs) {
if (err) console.error(err);
if (subs) return cb(null, prep(subs));
find(args, function(err, res) {
if (err || !res) return cb(err, res);
// Do not serve .zip subtitles unless we explicitly allow it
var count = res.all.length;
var mostByMeta = (res.all.filter(function(x) { return x.m === "i" }).length / res.all.length) > 0.9;
var ttlHours = (count < 10 || mostByMeta) ? 12 : (count < 50 ? 24 : 7*24 )
cacheSet("subtitles-v3", id, res, ttlHours * 60 * 60 * 1000)
cb(err, prep(res));
});
});
}
function subsGet(args, cb) {
subsFindCached(args, function(err, res) {
if (err) return cb(err)
res.item_hash = args.item_hash
res.subtitles = _.groupBy(res.all, "lang")
delete res.all
cb(null, res)
})
}
var manifest = {
"name": "OpenSubtitles",
"id": "org.stremio.opensubtitles",
"description": "The official add-on for subtitles from OpenSubtitles",
"version": require("./package").version,
"types": ["series","movie"],
"endpoint": "http://opensubtitles.strem.io/stremioget/stremio/v1",
"logo": "http://www.strem.io/images/addons/opensubtitles-logo.png"
};
var service = new addons.Server({
"subtitles.get": subsGet,
"subtitles.find": subsFindCached,
"subtitles.tracks": tracks,
//"subtitles.hash": hash, // don't expose this, as stremio will send links to localhost:11470, which are not accessible when this add-on is hosted remotely
"stats.get": function(args, cb, user) {
var pkg = require("./package");
cb(null, { name: pkg.name, version: pkg.version, stats: [{name: "subtitles", colour:"green"}], statsNum: "~ 3000000 subtitle files" });
}
}, { stremioget: true, allow: ["http://api9.strem.io"] }, manifest);
service.proxySrtOrVtt = function(req, res) {
// req.params.delay
var isVtt = req.url.match('^/subtitles.vtt'); // we can set it to false for srt
var query = url.parse(req.url, true).query;
var offset = query.offset ? parseInt(query.offset) : null;
service.request("subtitles.tracks", [{ stremioget: true }, { url: query.from }], function(err, handle) {
if (err) {
console.error(err);
res.writeHead(500);
res.end();
return;
}
if (isVtt) res.write("WEBVTT\n\n");
var format = function(d) {
return isVtt ? moment(d).utcOffset(0).format("HH:mm:ss.SSS") : moment(d).utcOffset(0).format("HH:mm:ss,SSS")
};
var applOffset = offset ? function(d) { return new Date(new Date(d).getTime() + offset) } : function(d) { return new Date(d); };
handle.tracks.forEach(function(track, i) {
res.write(i.toString()+"\n");
res.write(format(applOffset(track.startTime)) + " --> " + format(applOffset(track.endTime)) +"\n");
res.write(track.text.replace(/&/g, "&")+"\n\n"); // TODO: sanitize?
});
res.end();
});
}
service.subtitlesHash = hash;
module.exports = service;
module.exports.setCaching = function(get, set) {
cacheGet = get;
cacheSet = set;
}
/* Init server
*/
if (require.main !== module) { module.exports = service; } else {
var server = http.createServer(function (req, res) {
if (req.url.match("^/subtitles.vtt") || req.url.match("^/subtitles.srt")) return service.proxySrtOrVtt(req, res);
service.middleware(req, res, function() { res.end() });
}).listen(process.env.PORT || 3011).on("listening", function()
{
console.log("Subtitles listening on "+server.address().port);
});
server.on("error", function(e) { console.error(e) });
}