Repository navigation
Expand file tree
/
Copy pathfavorites.js
More file actions
92 lines (79 loc) · 2.36 KB
/
Copy pathfavorites.js
File metadata and controls
92 lines (79 loc) · 2.36 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
const fs = require('fs-extra');
/**
* Módulo para filtrar canales favoritos
*/
class FavoritesFilter {
constructor(favoritesPath) {
this.favoritesPath = favoritesPath;
this.favorites = [];
this.loadFavorites();
}
loadFavorites() {
if (!fs.existsSync(this.favoritesPath)) {
return;
}
try {
const content = fs.readFileSync(this.favoritesPath, 'utf8');
// Soportar diferentes formatos:
// 1. Lista de IDs separados por líneas
// 2. JSON array
// 3. Texto con IDs separados por comas
if (content.trim().startsWith('[')) {
// Formato JSON
this.favorites = JSON.parse(content);
} else {
// Formato texto: líneas o comas
this.favorites = content
.split(/[\n,]/)
.map(line => line.trim())
.filter(line => line.length > 0 && !line.startsWith('#'));
}
console.log(`[INFO] Loaded ${this.favorites.length} favorites from ${this.favoritesPath}`);
} catch (error) {
console.error(`[ERROR] Failed to load favorites from ${this.favoritesPath}:`, error.message);
}
}
isEmpty() {
return this.favorites.length === 0;
}
filter(channel) {
if (this.isEmpty()) {
return true;
}
// Buscar por slug, ID o nombre
return this.favorites.some(fav => {
const favLower = fav.toLowerCase();
return (
channel.slug?.toLowerCase() === favLower ||
channel._id?.toLowerCase() === favLower ||
channel.id?.toLowerCase() === favLower ||
channel.name?.toLowerCase() === favLower ||
channel.slug?.toLowerCase().includes(favLower) ||
channel.name?.toLowerCase().includes(favLower)
);
});
}
printSummary() {
if (this.isEmpty()) {
console.log('[INFO] No favorites filter applied');
return;
}
console.log('========================================');
console.log('[INFO] Favorites Filter Active');
console.log(`[INFO] Filtering for ${this.favorites.length} favorite(s):`);
this.favorites.forEach((fav, index) => {
console.log(` ${index + 1}. ${fav}`);
});
console.log('========================================');
}
}
/**
* Factory function para crear un filtro de favoritos
*/
function from(favoritesPath) {
return new FavoritesFilter(favoritesPath);
}
module.exports = {
from,
FavoritesFilter
};