forked from ardoviniandrea/ViniPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvodProcessor.js
More file actions
383 lines (328 loc) · 20.3 KB
/
Copy pathvodProcessor.js
File metadata and controls
383 lines (328 loc) · 20.3 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// vodProcessor.js
const XtreamClient = require('./xtreamClient');
/**
* Main function to refresh all VOD content for a given provider.
* @param {sqlite.Database} db - The database instance.
* @param {object} provider - The provider object (id, server_url, username, password).
* @param {function} sendStatus - Function to send status updates to the client.
*/
async function refreshVodContent(db, dbGet, dbAll, dbRun, provider, sendStatus = () => {}, userAgent) {
console.log(`[VOD Processor] Starting VOD refresh for: ${provider.name}`);
const scanStartTime = new Date().toISOString();
let server_url, username, password;
try {
if (!provider.xc_data) {
throw new Error('Provider object is missing xc_data.');
}
const xcInfo = JSON.parse(provider.xc_data);
server_url = xcInfo.server;
username = xcInfo.username;
password = xcInfo.password;
if (!server_url || !username || !password) {
throw new Error('Missing server, username, or password within xc_data.');
}
} catch (parseError) {
console.error(`[VOD Processor] Failed to parse XC credentials for provider ${provider.name}: ${parseError.message}`);
sendStatus(`Failed to parse XC credentials for ${provider.name}`, 'error');
return; // Stop processing this provider if credentials are bad
}
const client = new XtreamClient(server_url, username, password, userAgent);
const providerId = provider.id;
// --- Schema Migration ---
try {
await dbRun(db, "ALTER TABLE movies ADD COLUMN provider_unique_id TEXT");
} catch (e) {
if (!e.message.includes('duplicate column name')) {
console.error('[VOD Processor] Schema migration for movies failed:', e.message);
}
}
try {
await dbRun(db, "ALTER TABLE series ADD COLUMN provider_unique_id TEXT");
} catch (e) {
if (!e.message.includes('duplicate column name')) {
console.error('[VOD Processor] Schema migration for series failed:', e.message);
}
}
// --- 0. Process Categories ---
const categoryMap = new Map();
try {
sendStatus(`Fetching VOD and Series categories for ${provider.name}...`, 'info');
const vodCategories = await client.getVodCategories();
const seriesCategories = await client.getSeriesCategories();
const allCategories = {};
if (vodCategories && Array.isArray(vodCategories)) {
vodCategories.forEach(cat => allCategories[cat.category_id] = cat);
}
if (seriesCategories && Array.isArray(seriesCategories)) {
seriesCategories.forEach(cat => allCategories[cat.category_id] = cat);
}
const combinedCategories = Object.values(allCategories);
if (combinedCategories.length > 0) {
console.log(`[VOD Processor] Fetched ${combinedCategories.length} combined VOD/Series categories from provider.`);
sendStatus(`Processing ${combinedCategories.length} VOD/Series categories...`, 'info');
await dbRun(db, "BEGIN TRANSACTION");
const categoryInsertStmt = db.prepare(`INSERT OR IGNORE INTO vod_categories (category_id, category_name) VALUES (?, ?)`);
for (const catData of combinedCategories) {
if (catData.category_id && catData.category_name) {
await new Promise((resolve, reject) => {
categoryInsertStmt.run(catData.category_id, catData.category_name, function(err) {
if (err) return reject(err);
resolve(this);
});
});
categoryMap.set(String(catData.category_id), catData.category_name);
}
}
await new Promise(resolve => categoryInsertStmt.finalize(resolve));
await dbRun(db, "COMMIT");
}
} catch (error) {
await dbRun(db, "ROLLBACK");
console.error(`[VOD Processor] Category processing FAILED for ${provider.name}:`, error.message);
sendStatus(`Category processing FAILED for ${provider.name}: ${error.message}`, 'error');
return; // Stop if categories fail
}
// --- 1. Process Movies ---
try {
sendStatus(`Fetching movies for ${provider.name}...`, 'info');
const movies = await client.getVodStreams();
if (movies && Array.isArray(movies)) {
console.log(`[VOD Processor] Fetched ${movies.length} movies from provider.`);
sendStatus(`Processing ${movies.length} movies...`, 'info');
await dbRun(db, "BEGIN TRANSACTION");
const existingMovies = await dbAll(db, 'SELECT id, provider_unique_id FROM movies WHERE provider_unique_id IS NOT NULL');
const providerUniqueIdMap = new Map(existingMovies.map(m => [m.provider_unique_id, m.id]));
const movieInsertStmt = db.prepare(`INSERT INTO movies (name, year, description, logo, category_name, provider_unique_id) VALUES (?, ?, ?, ?, ?, ?)`);
const movieUpdateStmt = db.prepare(`UPDATE movies SET name = ?, year = ?, description = ?, logo = ?, category_name = ? WHERE id = ?`);
const relationInsertStmt = db.prepare(`INSERT OR REPLACE INTO provider_movie_relations (provider_id, movie_id, stream_id, container_extension, last_seen) VALUES (?, ?, ?, ?, ?)`);
for (const movieData of movies) {
const { name, plot, stream_icon, stream_id, container_extension, category_id } = movieData;
if (!stream_id) continue;
const providerUniqueId = `movie_${providerId}_${stream_id}`;
let year = null;
if (movieData.releaseDate) year = new Date(movieData.releaseDate).getFullYear();
else if (name) {
const yearMatch = name.match(/\((\d{4})\)/);
if (yearMatch) year = parseInt(yearMatch[1]);
}
const categoryName = categoryMap.get(String(category_id)) || 'VOD';
let movieId = providerUniqueIdMap.get(providerUniqueId);
if (movieId) {
// Update existing movie
await new Promise((resolve, reject) => movieUpdateStmt.run(name, year, plot, stream_icon, categoryName, movieId, (err) => err ? reject(err) : resolve()));
} else {
// Create new movie
const result = await new Promise((resolve, reject) => {
movieInsertStmt.run(name, year, plot, stream_icon, categoryName, providerUniqueId, function(err) {
if (err) return reject(err);
resolve(this);
});
});
movieId = result.lastID;
providerUniqueIdMap.set(providerUniqueId, movieId);
}
await new Promise((resolve, reject) => relationInsertStmt.run(providerId, movieId, stream_id, container_extension || 'mp4', scanStartTime, (err) => err ? reject(err) : resolve()));
}
await new Promise(resolve => movieInsertStmt.finalize(resolve));
await new Promise(resolve => movieUpdateStmt.finalize(resolve));
await new Promise(resolve => relationInsertStmt.finalize(resolve));
await dbRun(db, "COMMIT");
}
} catch (error) {
await dbRun(db, "ROLLBACK");
console.error(`[VOD Processor] Movie processing FAILED for ${provider.name}:`, error.message);
sendStatus(`Movie processing FAILED for ${provider.name}: ${error.message}`, 'error');
}
// --- 2. Process Series ---
try {
sendStatus(`Fetching series for ${provider.name}...`, 'info');
const series = await client.getSeries();
if (series && Array.isArray(series)) {
console.log(`[VOD Processor] Fetched ${series.length} series from provider.`);
sendStatus(`Processing ${series.length} series...`, 'info');
await dbRun(db, "BEGIN TRANSACTION");
const existingSeries = await dbAll(db, 'SELECT id, provider_unique_id FROM series WHERE provider_unique_id IS NOT NULL');
const providerUniqueIdMap = new Map(existingSeries.map(s => [s.provider_unique_id, s.id]));
const seriesInsertStmt = db.prepare(`INSERT INTO series (name, year, description, logo, category_name, provider_unique_id) VALUES (?, ?, ?, ?, ?, ?)`);
const seriesUpdateStmt = db.prepare(`UPDATE series SET name = ?, year = ?, description = ?, logo = ?, category_name = ? WHERE id = ?`);
const seriesRelationInsertStmt = db.prepare(`INSERT OR REPLACE INTO provider_series_relations (provider_id, series_id, external_series_id, last_seen) VALUES (?, ?, ?, ?)`);
for (const seriesData of series) {
const { name, plot, cover, series_id: external_series_id, category_id } = seriesData;
if (!external_series_id) continue;
const providerUniqueId = `series_${providerId}_${external_series_id}`;
let year = null;
if (seriesData.releaseDate) year = new Date(seriesData.releaseDate).getFullYear();
else if (name) {
const yearMatch = name.match(/\((\d{4})\)/);
if (yearMatch) year = parseInt(yearMatch[1]);
}
const categoryName = categoryMap.get(String(category_id)) || 'Series';
let seriesId = providerUniqueIdMap.get(providerUniqueId);
if (seriesId) {
// Update existing series
await new Promise((resolve, reject) => seriesUpdateStmt.run(name, year, plot, cover, categoryName, seriesId, (err) => err ? reject(err) : resolve()));
} else {
// Create new series
const result = await new Promise((resolve, reject) => {
seriesInsertStmt.run(name, year, plot, cover, categoryName, providerUniqueId, function(err) {
if (err) return reject(err);
resolve(this);
});
});
seriesId = result.lastID;
providerUniqueIdMap.set(providerUniqueId, seriesId);
}
await new Promise((resolve, reject) => seriesRelationInsertStmt.run(providerId, seriesId, external_series_id, scanStartTime, (err) => err ? reject(err) : resolve()));
}
await new Promise(resolve => seriesInsertStmt.finalize(resolve));
await new Promise(resolve => seriesUpdateStmt.finalize(resolve));
await new Promise(resolve => seriesRelationInsertStmt.finalize(resolve));
await dbRun(db, "COMMIT");
}
} catch (error) {
await dbRun(db, "ROLLBACK");
console.error(`[VOD Processor] Series processing FAILED for ${provider.name}:`, error.message);
sendStatus(`Series processing FAILED for ${provider.name}: ${error.message}`, 'error');
}
// --- 4. Cleanup Stale Content ---
try {
console.log(`[VOD Processor] Cleaning up stale VOD content for ${provider.name}...`);
sendStatus(`Cleaning up old VOD entries for ${provider.name}...`, 'info');
await dbRun(db, "BEGIN TRANSACTION");
const staleMovies = await dbRun(db, 'DELETE FROM provider_movie_relations WHERE provider_id = ? AND last_seen < ?', [providerId, scanStartTime]);
if (staleMovies.changes > 0) console.log(`[VOD Processor] Removed ${staleMovies.changes} stale movie relations.`);
const staleSeries = await dbRun(db, 'DELETE FROM provider_series_relations WHERE provider_id = ? AND last_seen < ?', [providerId, scanStartTime]);
if (staleSeries.changes > 0) console.log(`[VOD Processor] Removed ${staleSeries.changes} stale series relations.`);
const staleEpisodes = await dbRun(db, 'DELETE FROM provider_episode_relations WHERE provider_id = ? AND last_seen < ?', [providerId, scanStartTime]);
if (staleEpisodes.changes > 0) console.log(`[VOD Processor] Removed ${staleEpisodes.changes} stale episode relations.`);
// --- 5. Cleanup Orphaned Content ---
await dbRun(db, `DELETE FROM movies WHERE id NOT IN (SELECT DISTINCT movie_id FROM provider_movie_relations)`);
await dbRun(db, `DELETE FROM series WHERE id NOT IN (SELECT DISTINCT series_id FROM provider_series_relations)`);
await dbRun(db, `DELETE FROM episodes WHERE id NOT IN (SELECT DISTINCT episode_id FROM provider_episode_relations)`);
await dbRun(db, "COMMIT");
console.log(`[VOD Processor] VOD cleanup completed for: ${provider.name}`);
} catch (error) {
await dbRun(db, "ROLLBACK");
console.error(`[VOD Processor] Cleanup FAILED for ${provider.name}:`, error.message);
sendStatus(`Cleanup FAILED for ${provider.name}: ${error.message}`, 'error');
}
console.log(`[VOD Processor] VOD refresh completed for: ${provider.name}`);
sendStatus(`VOD refresh successful for ${provider.name}.`, 'success');
}
/**
* Processes VOD content from a raw M3U file string.
* @param {sqlite.Database} db - The database instance.
* @param {function} dbGet - Promisified db.get.
* @param {function} dbAll - Promisified db.all.
* @param {function} dbRun - Promisified db.run.
* @param {string} m3uContent - The full M3U file content as a string.
* @param {object} provider - The provider object from settings.
* @param {function} sendStatus - Function to send status updates to the client.
*/
async function processM3uVod(db, dbGet, dbAll, dbRun, m3uContent, provider, sendStatus = () => {}) {
console.log(`[VOD Processor M3U] Starting VOD processing for M3U source: ${provider.name}`);
const scanStartTime = new Date().toISOString();
const providerId = provider.id;
try {
const lines = m3uContent.split('\n');
let currentExtInf = null;
const movies = [];
const series = [];
// Regex to extract attributes from #EXTINF
const attributeRegex = /([a-zA-Z0-9_-]+)="([^"]*)"/g;
for (const line of lines) {
if (line.startsWith('#EXTINF:')) {
currentExtInf = { line: line.trim(), attributes: {} };
let match;
while ((match = attributeRegex.exec(line)) !== null) {
currentExtInf.attributes[match[1]] = match[2];
}
const nameMatch = line.match(/,(.*)$/);
currentExtInf.name = nameMatch ? nameMatch[1].trim() : 'Untitled';
} else if (line.trim().startsWith('http') && currentExtInf) {
const url = line.trim();
const isMovie = url.includes('/movie/') || currentExtInf.attributes['tvg-type'] === 'movie';
const isSeries = url.includes('/series/') || currentExtInf.attributes['tvg-type'] === 'series';
if (isMovie) {
movies.push({ ...currentExtInf, url });
} else if (isSeries) {
series.push({ ...currentExtInf, url });
}
currentExtInf = null; // Reset after processing a URL
}
}
console.log(`[VOD Processor M3U] Found ${movies.length} movies and ${series.length} series in M3U.`);
sendStatus(`Processing ${movies.length} movies and ${series.length} series from ${provider.name}...`, 'info');
// Use a transaction for efficiency
await dbRun(db, "BEGIN TRANSACTION");
// Process Movies
if (movies.length > 0) {
const movieInsertStmt = db.prepare(`INSERT OR IGNORE INTO movies (name, year, logo, category_name, provider_unique_id) VALUES (?, ?, ?, ?, ?)`);
const movieRelationInsertStmt = db.prepare(`INSERT OR REPLACE INTO provider_movie_relations (provider_id, movie_id, stream_id, container_extension, last_seen) VALUES (?, ?, ?, ?, ?)`);
const existingMovies = await dbAll(db, 'SELECT id, provider_unique_id FROM movies WHERE provider_unique_id IS NOT NULL');
const providerUniqueIdMap = new Map(existingMovies.map(m => [m.provider_unique_id, m.id]));
for (const movieData of movies) {
const { name, attributes, url } = movieData;
const streamId = url.substring(url.lastIndexOf('/') + 1).split('.')[0];
const providerUniqueId = `movie_${providerId}_${streamId}`;
const yearMatch = name.match(/\((\d{4})\)/);
const year = yearMatch ? parseInt(yearMatch[1]) : null;
const logo = attributes['tvg-logo'] || null;
const categoryName = attributes['group-title'] || 'VOD';
let movieId = providerUniqueIdMap.get(providerUniqueId);
if (!movieId) {
const result = await new Promise((resolve, reject) => {
movieInsertStmt.run(name, year, logo, categoryName, providerUniqueId, function(err) {
if (err) return reject(err);
resolve(this);
});
});
movieId = result.lastID;
providerUniqueIdMap.set(providerUniqueId, movieId);
}
const extension = url.split('.').pop() || 'mp4';
await new Promise((resolve, reject) => movieRelationInsertStmt.run(providerId, movieId, streamId, extension, scanStartTime, (err) => err ? reject(err) : resolve()));
}
await new Promise(resolve => movieInsertStmt.finalize(resolve));
await new Promise(resolve => movieRelationInsertStmt.finalize(resolve));
}
// Process Series (basic info, not episodes from M3U)
if (series.length > 0) {
const seriesInsertStmt = db.prepare(`INSERT OR IGNORE INTO series (name, year, logo, category_name, provider_unique_id) VALUES (?, ?, ?, ?, ?)`);
const seriesRelationInsertStmt = db.prepare(`INSERT OR REPLACE INTO provider_series_relations (provider_id, series_id, external_series_id, last_seen) VALUES (?, ?, ?, ?)`);
const existingSeries = await dbAll(db, 'SELECT id, provider_unique_id FROM series WHERE provider_unique_id IS NOT NULL');
const providerUniqueIdMap = new Map(existingSeries.map(s => [s.provider_unique_id, s.id]));
for (const seriesData of series) {
const { name, attributes, url } = seriesData;
// For M3U, we might not have a distinct series_id, so we create one from the name
const externalSeriesId = name.replace(/\s+/g, '_').toLowerCase();
const providerUniqueId = `series_${providerId}_${externalSeriesId}`;
const yearMatch = name.match(/\((\d{4})\)/);
const year = yearMatch ? parseInt(yearMatch[1]) : null;
const logo = attributes['tvg-logo'] || null;
const categoryName = attributes['group-title'] || 'Series';
let seriesId = providerUniqueIdMap.get(providerUniqueId);
if (!seriesId) {
const result = await new Promise((resolve, reject) => {
seriesInsertStmt.run(name, year, logo, categoryName, providerUniqueId, function(err) {
if (err) return reject(err);
resolve(this);
});
});
seriesId = result.lastID;
providerUniqueIdMap.set(providerUniqueId, seriesId);
}
await new Promise((resolve, reject) => seriesRelationInsertStmt.run(providerId, seriesId, externalSeriesId, scanStartTime, (err) => err ? reject(err) : resolve()));
}
await new Promise(resolve => seriesInsertStmt.finalize(resolve));
await new Promise(resolve => seriesRelationInsertStmt.finalize(resolve));
}
await dbRun(db, "COMMIT");
sendStatus(`Successfully processed VOD content from ${provider.name}.`, 'success');
} catch (error) {
await dbRun(db, "ROLLBACK");
console.error(`[VOD Processor M3U] Processing FAILED for ${provider.name}:`, error.message);
sendStatus(`M3U VOD processing FAILED for ${provider.name}: ${error.message}`, 'error');
}
}
module.exports = { refreshVodContent, processM3uVod };