Repository navigation
Expand file tree
/
Copy pathsubt.js
More file actions
83 lines (64 loc) · 2.28 KB
/
Copy pathsubt.js
File metadata and controls
83 lines (64 loc) · 2.28 KB
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
(function () {
'use strict';
const OSV3 = 'https://opensubtitles-v3.strem.io/';
const cache = {};
async function fetchSubs(imdb, season, episode) {
const key = `${imdb}_${season || 0}_${episode || 0}`;
if (cache[key]) return cache[key];
try {
const url = season && episode
? `${OSV3}subtitles/series/${imdb}:${season}:${episode}.json`
: `${OSV3}subtitles/movie/${imdb}.json`;
const r = await fetch(url);
const j = await r.json();
return (cache[key] = j.subtitles || []);
} catch (e) {
console.warn('[OS Subs] fetch error', e);
return [];
}
}
async function setupSubs() {
const activity = Lampa.Activity.active?.();
const playdata = Lampa.Player.playdata?.();
const movie = activity?.movie;
if (!activity || !playdata || !movie) return;
if (!movie.imdb_id) return;
const imdb = movie.imdb_id;
const isSeries = !!movie.first_air_date;
const season = isSeries ? playdata.season : undefined;
const episode = isSeries ? playdata.episode : undefined;
const osSubs = await fetchSubs(imdb, season, episode);
const filtered = osSubs
.filter(s =>
s.url &&
(s.lang === 'eng' || s.lang === 'ukr' || s.lang === 'rus')
)
.map(s => ({
label:
s.lang === 'eng'
? 'ENG'
: s.lang === 'ukr'
? 'UKR'
: 'RUS',
url: s.url,
lang: s.lang
}));
const current = (playdata.subtitles || []).map(s => ({
label: s.label,
url: s.url,
lang: s.lang || ''
}));
const all = [...current];
filtered.forEach(s => {
if (!all.find(x => x.url === s.url)) {
all.push(s);
}
});
if (!all.length) return;
const idx = all.findIndex(s => s.lang === 'eng');
Lampa.Player.subtitles(all, idx >= 0 ? idx : 0);
}
Lampa.Player.listener.follow('start', function () {
setTimeout(setupSubs, 500);
});
})();