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
/
subscene.js
276 lines (254 loc) · 9.04 KB
/
subscene.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const tmdb = require('./tmdb');
const kitsu = require('./kitsu');
const subscene = require('./subsceneAPI');
const config = require('./config');
require('dotenv').config();
const languages = require('./languages.json');
const count = 10;
const NodeCache = require("node-cache");
const sub2vtt = require('sub2vtt');
const { forEach } = require('sub2vtt/ISO639');
const Cache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const MetaCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const KitsuCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const filesCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const subsceneCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const searchCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
async function subtitles(type, id, lang) {
if (id.match(/tt[0-9]/)){
return await (TMDB(type, id, lang))
} if (id.match(/kitsu:[0-9]/)){
return await (Kitsu(type, id, lang))
console.log(type, id, lang)
}
}
async function Kitsu(type, id, lang) {
console.log(type, id, lang);
let metaid = id.split(':')[1];
let meta = KitsuCache.get(metaid);
if (!meta) {
meta = await kitsu(metaid);
if (meta) {
KitsuCache.set(metaid, meta);
}
}
if(meta){
//console.log(meta)
const episode = id.split(':')[2];
const searchID = `kitisu_${metaid}_${id.split(':')[1]}`;
let search = searchCache.get(searchID);
let slug = `${meta.title["en_jp"]} (${meta.title["en"]}) (${meta.year})`;
var moviePath = '';
console.log('slug',slug)
console.log('title',meta.title["en_jp"])
if (!search) {
search = await subscene.search(`${encodeURIComponent(meta.title["en_jp"])}`);
if (search) {
searchCache.set(searchID, search);
}
}
for(let i = 0; i<search.length;i++){
if(search[i].title.includes(meta.title["en_jp"])){
moviePath = search[i].path
break
}
}
//let moviePath = search[0].path;
console.log(moviePath)
return getsubtitles(moviePath, meta.slug.replace('-','_'), lang, episode)
}
}
async function TMDB(type, id, lang) {
console.log(type, id, lang);
let metaid = id.split(':')[0];
let meta = MetaCache.get(metaid);
if (!meta) {
meta = await tmdb(type, metaid);
if (meta) {
MetaCache.set(metaid, meta);
}
}
if(meta){
console.log("meta",meta)
if (type == "movie") {
let moviePath = `/subtitles/${meta.slug}`;
console.log(moviePath);
return getsubtitles(moviePath, id, lang, null, meta.year)
}
else if (type == "series") {
let season = parseInt(id.split(':')[1]);
season = ordinalInWord(season);
const episode = id.split(':')[2];
const searchID = `${metaid}_${id.split(':')[1]}`;
let search = searchCache.get(searchID);
if (!search) {
search = await subscene.search(`${meta.title} ${season} season`);
if (search) {
searchCache.set(searchID, search);
}
}
let moviePath = search[0].path;
return getsubtitles(moviePath, id.split(":")[0] + '_season_' + id.split(":")[1], lang, episode)
/*
var moviePath = '/subtitles/' + meta.slug + '-' + season + '-season';
let subtitles = await subscene.getSubtitles(moviePath).catch(error => { console.error(error) })
console.log('subtitles', Object.keys(subtitles).length)
if (!Object.keys(subtitles).length) {
moviePath = '/subtitles/' + meta.slug;
}
if(meta.slug=='the-100'){
moviePath = `/subtitles/the-100-the-hundred-${season}-season`;
}
console.log(moviePath);
return await sleep(2000).then(() => { return getsubtitles(moviePath, id.split(":")[0] + '_season_' + id.split(":")[1], lang, episode) })
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}*/
}
}
}
async function getsubtitles(moviePath, id, lang, episode, year) {
let breakTitle = moviePath.match(/[a-z]+/gi)
//console.log("breakTitle : " , breakTitle)
console.log(moviePath, id, lang, year, episode)
const cachID = `${id}_${lang}`;
let cached = Cache.get(cachID);
if (cached) {
console.log('cached main', cachID, cached);
return cached
} else {
let subs = [];
console.log(moviePath)
var subtitles = subsceneCache.get(moviePath);
if (!subtitles) {
let subs1 = await subscene.getSubtitles(moviePath).catch(error => { console.error(error) }) // moviepath without year
console.log("no year scraping :", subs1.length)
if (subs1[0].year !== year && !episode) { // if a movie and year isnt matched with the one in imdb
await new Promise((r) => setTimeout(r, 2000)); // prevent too many request, still finding the other way
let subs2 = await subscene.getSubtitles(moviePath + "-" + year).catch(error => { console.error(error) }) // moviepath with year
console.log("with year scraping :", subs2.length)
subs1 = subs1.concat(subs2);
}
subtitles = subscene.sortByLang(subs1)
if (subtitles) {
subsceneCache.set(moviePath, subtitles);
}
}
//console.log('subtitles', Object.keys(subtitles).length)
//console.log('subtitles', moviePath)
if (subtitles[lang]) {
subtitles = subtitles[lang];
console.log('subtitles matched lang : ',subtitles.length)
let sub = [];
let episodeText;
if (episode) {
episodeText = (episode.length == 1) ? ('0' + episode) : episode;
episodeText = 'E' + episodeText
console.log('episode', episodeText)
subtitles.forEach(element => {
if (element.title.match(/S\d\d\w\d/gi)) {
var reg = new RegExp(episodeText.toLowerCase(), 'gi');
if (reg.test(element.title.toLowerCase())) {
console.log(element.title);
sub.push(element);
}
} else {
console.log(element.title);
sub.push(element)
}
})
//sub = filtered(subtitles, 'title', episodeText)
//episodeText = episode.length == 1 ? '0' + episode : episode;
//sub = sub.concat(filtered(subtitles, 'title', episodeText))
sub = [...new Set(sub)];
subtitles = sub;
}
console.log("filtered subs ", subtitles.length)
for (let i = 0; i < (subtitles.length); i++) {
let value = subtitles[i];
let simpleTitle = subtitles[i].title;
breakTitle.forEach(el => {
var regEx = new RegExp(el, "ig");
simpleTitle = simpleTitle.replace(regEx,"")
})
simpleTitle = simpleTitle.replace(/\W/gi,"") // kasih gambaran di stremio
if (value) {
let path = config.BaseURL + value.path;
if (episode) {
url = config.local+"/sub.vtt?"+"episode="+episodeText+"&"+sub2vtt.gerenateUrl(path);
} else {
url = config.local+"/sub.vtt?"+sub2vtt.gerenateUrl(path);
}
subs.push({
lang: languages[lang].iso || languages[lang].id,
//id: "subscn_"+episode?`${cachID}_ep${episode}_${i}`:`${cachID}_${i}`,
title:subtitles[i].title,
id: "s."+`${i}`+"."+ simpleTitle,
url: url
});
}
}
let cached = Cache.set(cachID, subs);
console.log("cached", cached)
return subs;
}else{
return
}
}
}
async function downloadUrl(path, episode) {
let cachID = episode ? path + '_' + episode : path;
let cached = filesCache.get(cachID);
if (cached) {
console.log('File already cached', cachID);
return cached
} else {
return subscene.downloadUrl(path).then(url => {
let cached = filesCache.set(cachID, url);
console.log("Caching File", cached)
return url;
}).catch(error => { console.log(error) });
}
}
function filtered(list, key, value) {
var filtered = [], i = list.length;
var reg = new RegExp(value.toLowerCase(), 'gi');
while (i--) {
if (reg.test(list[i][key].toLowerCase())) {
filtered.push(list[i]);
}
}
return filtered;
};
function ordinalInWord(cardinal) {
const ordinals = ["zeroth", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth", "Twentieth"]
var tens = {
20: 'twenty',
30: 'thirty',
40: 'forty',
50: 'Fifty',
60: 'Sixty',
70: 'Seventy',
80: 'Eighty',
90: 'Ninety'
};
var ordinalTens = {
30: 'thirtieth',
40: 'fortieth',
50: 'fiftieth',
60: 'Sixtieth',
70: 'Seventieth',
80: 'Eightieth',
90: 'Ninetieth'
};
if (cardinal <= 20) {
return ordinals[cardinal];
}
if (cardinal % 10 === 0) {
return ordinalTens[cardinal];
}
return tens[cardinal - (cardinal % 10)] + ordinals[cardinal % 10];
}
module.exports = { subtitles, downloadUrl };