-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaddon.js
More file actions
284 lines (238 loc) · 8.12 KB
/
addon.js
File metadata and controls
284 lines (238 loc) · 8.12 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
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
277
278
279
280
281
282
283
284
const { addonBuilder } = require("stremio-addon-sdk");
const SubsRoClient = require("./lib/subsro");
const { matchesEpisode, calculateMatchScore } = require("./lib/matcher");
const { listSrtFiles, getArchiveType } = require("./lib/archiveUtils");
const { getLimiter } = require("./lib/rateLimiter");
const manifest = require("./manifest");
const builder = new addonBuilder(manifest);
// --- CACHE SYSTEM ---
// --- CACHE SYSTEM ---
const { ARCHIVE_CACHE, ARCHIVE_CACHE_TTL } = require("./lib/archiveCache");
// Simple LRU implementation to prevent memory leaks
class SimpleLRU {
constructor(maxSize, ttl = 0) {
this.maxSize = maxSize;
this.ttl = ttl;
this.cache = new Map();
}
get(key) {
const item = this.cache.get(key);
if (!item) return null;
if (this.ttl > 0 && Date.now() - item.timestamp > this.ttl) {
this.cache.delete(key);
return null;
}
// Refresh LRU order
this.cache.delete(key);
this.cache.set(key, item);
return item.value;
}
set(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key);
} else if (this.cache.size >= this.maxSize) {
// Evict oldest (first item in Map iteration order)
const oldestKey = this.cache.keys().next().value;
this.cache.delete(oldestKey);
}
this.cache.set(key, { value, timestamp: Date.now() });
}
has(key) {
return this.cache.has(key);
}
}
const CACHE = new SimpleLRU(1000); // Max 1000 subtitle responses
const PENDING_REQUESTS = new Map(); // Pending requests are transient, no LRU needed
const CLIENT_CACHE = new SimpleLRU(500); // Max 500 active API clients
const CACHE_TTL = 15 * 60 * 1000;
const EMPTY_CACHE_TTL = 60 * 1000;
const getClient = (apiKey) => {
let client = CLIENT_CACHE.get(apiKey);
if (!client) {
client = new SubsRoClient(apiKey);
CLIENT_CACHE.set(apiKey, client);
}
return client;
};
const LANGUAGE_MAPPING = {
ro: "ron",
en: "eng",
ita: "ita",
fra: "fra",
ger: "deu",
ung: "hun",
gre: "ell",
por: "por",
spa: "spa",
alt: "und",
};
function parseStremioId(id) {
const parts = id.split(":");
return {
imdbId: parts[0],
season: parts[1] ? parseInt(parts[1], 10) : null,
episode: parts[2] ? parseInt(parts[2], 10) : null,
};
}
/**
* Download archive via rate limiter and list SRT files.
* Uses caching to avoid redundant downloads.
*/
async function getArchiveSrtList(apiKey, subId) {
const cacheKey = `archive_${subId}`;
const cached = ARCHIVE_CACHE.get(cacheKey);
if (cached) {
return cached.srtFiles;
}
try {
const downloadUrl = `https://subs.ro/api/v1.0/subtitle/${subId}/download`;
// Use per-user rate limiter for safe, queued downloads
const limiter = getLimiter(apiKey);
const buffer = await limiter.downloadArchive(downloadUrl, {
headers: { "X-Subs-Api-Key": apiKey },
});
const srtFiles = await listSrtFiles(buffer);
const archiveType = getArchiveType(buffer);
ARCHIVE_CACHE.set(cacheKey, {
buffer,
srtFiles,
archiveType,
timestamp: Date.now(),
});
const status = limiter.getQueueStatus();
const ts = new Date().toISOString().slice(11, 23);
// Only log in development to prevent disk fill
if (process.env.NODE_ENV === "development") {
console.log(
`[${ts}] [SUBS] Archive ${subId}: ${
srtFiles.length
} SRTs (${archiveType.toUpperCase()}) [Active: ${
status.activeDownloads
}, Pending: ${status.download}]`,
);
}
return srtFiles;
} catch (error) {
console.error(`[SUBS] Error downloading archive ${subId}:`, error.message);
return [];
}
}
const subtitlesHandler = async ({ type, id, extra, config }) => {
if (!config || !config.apiKey) return { subtitles: [] };
// NOTE: Removed globalLimiter.clearQueues() - it was causing users to cancel each other's downloads
const { imdbId, season, episode } = parseStremioId(id);
const isSeries = type === "series" && episode !== null;
const videoFilename = extra?.filename || "";
const cacheKey = isSeries
? `${imdbId}_s${season}e${episode}_${config.languages || "all"}`
: `${imdbId}_${config.languages || "all"}`;
// 1. Check Cache
const cachedData = CACHE.get(cacheKey);
if (cachedData && Date.now() - cachedData.timestamp < cachedData.ttl) {
return { subtitles: cachedData.data };
}
// 2. Debounce Pending Requests
if (PENDING_REQUESTS.has(cacheKey)) {
return PENDING_REQUESTS.get(cacheKey);
}
const fetchTask = (async () => {
try {
const subsRo = getClient(config.apiKey);
const results = await subsRo.searchByImdb(imdbId);
// Filter by language
let filteredResults = results;
if (config.languages && config.languages.length > 0) {
filteredResults = results.filter((sub) =>
config.languages.includes(sub.language),
);
}
// BeamUp URL detection - hardcoded for production, dynamic for local dev
const BEAMUP_URL =
"https://cdcd7719a6b3-stremio-subs-ro.baby-beamup.club";
const baseUrl = process.env.NODE_ENV
? BEAMUP_URL
: config.baseUrl || "http://localhost:7000";
const allSubtitles = [];
// Process archives sequentially (rate limiter handles timing)
for (const sub of filteredResults) {
const srtFiles = await getArchiveSrtList(config.apiKey, sub.id);
const lang = LANGUAGE_MAPPING[sub.language] || sub.language;
for (const srtPath of srtFiles) {
// For series: filter out SRTs that don't match the episode
if (isSeries) {
if (!matchesEpisode(srtPath, season, episode)) {
continue;
}
}
const encodedSrtPath = Buffer.from(srtPath).toString("base64url");
// Calculate weighted match score (release group +50, source +20, base fuzzy)
let matchScore = calculateMatchScore(videoFilename, srtPath);
// RETAIL BONUS (KISS Approach): +5 points
// Acts as tie-breaker for identical matches, but won't override Group/Source matches
const isRetail =
(sub.translator &&
sub.translator.toLowerCase().includes("retail")) ||
(sub.title && sub.title.toLowerCase().includes("retail"));
if (isRetail) {
matchScore += 5;
}
allSubtitles.push({
id: `subsro_${sub.id}_${encodedSrtPath.slice(0, 8)}`,
url: `${baseUrl}/${config.apiKey}/proxy/${sub.id}/${encodedSrtPath}/sub.vtt`,
lang,
srtPath,
matchScore,
isRetail, // Passed for debugging/logging
});
}
}
// Sort by weighted match score (highest first)
allSubtitles.sort((a, b) => b.matchScore - a.matchScore);
// Log top matches for debugging (Dev only)
if (
process.env.NODE_ENV === "development" &&
allSubtitles.length > 0 &&
videoFilename
) {
const top = allSubtitles.slice(0, 5); // Show top 5
console.log(`[SUBS] Matching results for "${videoFilename}":`);
top.forEach((s, i) => {
console.log(` ${i + 1}. [Score: ${s.matchScore}] ${s.srtPath}`);
});
}
// Remove internal properties before returning
const subtitles = allSubtitles.map(({ id, url, lang }) => ({
id,
url,
lang,
}));
// Store in Cache
CACHE.set(cacheKey, {
data: subtitles,
timestamp: Date.now(),
ttl: subtitles.length > 0 ? CACHE_TTL : EMPTY_CACHE_TTL,
});
if (process.env.NODE_ENV === "development") {
console.log(
`[SUBS] Served ${subtitles.length} subs for ${imdbId}${
isSeries ? ` S${season}E${episode}` : ""
} (Status: OK)`,
);
}
return { subtitles };
} catch (error) {
// Errors are already logged by globalLimiter
return { subtitles: [] };
} finally {
PENDING_REQUESTS.delete(cacheKey);
}
})();
PENDING_REQUESTS.set(cacheKey, fetchTask);
return fetchTask;
};
builder.defineSubtitlesHandler(subtitlesHandler);
module.exports = {
builder,
addonInterface: builder.getInterface(),
subtitlesHandler,
};