Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
446 lines (383 loc) · 15.9 KB
/
Copy pathserver.ts
File metadata and controls
446 lines (383 loc) · 15.9 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import express, { Request, Response } from 'express';
import path from 'path';
import Parser from 'rss-parser';
import { GoogleGenAI } from '@google/genai';
const app = express();
const PORT = process.env.PORT ? Number(process.env.PORT) : 3000;
app.use(express.json());
const rssParser = new Parser({
customFields: {
item: [
['media:content', 'mediaContent', { keepArray: true }],
['media:thumbnail', 'mediaThumbnail', { keepArray: true }],
['media:group', 'mediaGroup'],
['enclosure', 'enclosure', { keepArray: true }],
['content:encoded', 'contentEncoded'],
['image', 'image'],
['thumbnail', 'thumbnail'],
['thumb', 'thumb'],
['og:image', 'ogImage'],
['twitter:image', 'twitterImage'],
],
},
timeout: 4500,
});
// Cache map with 60 seconds TTL
interface CacheEntry {
timestamp: number;
data: any[];
}
const feedCache = new Map<string, CacheEntry>();
const CACHE_TTL_MS = 60 * 1000;
// Utility to decode HTML entities
function decodeHtmlEntities(str: string): string {
if (!str) return '';
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/'/g, "'");
}
// Utility to extract image URL from item
function extractImageUrl(item: any, baseUrl?: string): string | undefined {
let src: string | undefined = undefined;
// 1. Check enclosure (object or array)
if (item.enclosure) {
const arr = Array.isArray(item.enclosure) ? item.enclosure : [item.enclosure];
for (const enc of arr) {
const url = enc.url || enc.$?.url || enc.href;
const type = enc.type || enc.$?.type || '';
if (url && (type.includes('image') || /\.(jpg|jpeg|gif|png|webp|bmp|svg|avif)/i.test(url) || !type)) {
src = url;
break;
}
}
}
// 2. Check mediaContent
if (!src && item.mediaContent && item.mediaContent.length > 0) {
const img = item.mediaContent.find((m: any) => m.$?.url || m.url || m.href);
if (img) src = img.$?.url || img.url || img.href;
}
// 3. Check mediaThumbnail
if (!src && item.mediaThumbnail && item.mediaThumbnail.length > 0) {
const img = item.mediaThumbnail.find((m: any) => m.$?.url || m.url || m.href);
if (img) src = img.$?.url || img.url || img.href;
}
// 4. Check mediaGroup
if (!src && item.mediaGroup) {
const group = item.mediaGroup;
const content = group['media:content'] || group.mediaContent;
if (content) {
const arr = Array.isArray(content) ? content : [content];
const img = arr.find((m: any) => m.$?.url || m.url || m.href);
if (img) src = img.$?.url || img.url || img.href;
}
if (!src) {
const thumbnail = group['media:thumbnail'] || group.mediaThumbnail;
if (thumbnail) {
const arr = Array.isArray(thumbnail) ? thumbnail : [thumbnail];
const img = arr.find((m: any) => m.$?.url || m.url || m.href);
if (img) src = img.$?.url || img.url || img.href;
}
}
}
// 5. Check item.image, thumbnail, thumb, ogImage, twitterImage
if (!src && item.image) {
if (typeof item.image === 'string') src = item.image;
else if (typeof item.image === 'object') src = item.image.url || item.image.$?.url || item.image.href;
}
if (!src && item.thumbnail) {
if (typeof item.thumbnail === 'string') src = item.thumbnail;
else if (typeof item.thumbnail === 'object') src = item.thumbnail.url || item.thumbnail.$?.url || item.thumbnail.href;
}
if (!src && item.thumb) {
if (typeof item.thumb === 'string') src = item.thumb;
else if (typeof item.thumb === 'object') src = item.thumb.url || item.thumb.$?.url;
}
if (!src && (item.ogImage || item.twitterImage)) {
src = item.ogImage || item.twitterImage;
}
// 6. Regex scan description / content / contentEncoded / summary with decoded HTML
if (!src) {
const rawHtml = `${item.contentEncoded || ''} ${item.content || ''} ${item.description || ''} ${item.summary || ''}`;
const decoded = decodeHtmlEntities(rawHtml);
// Look for data-src, data-original, data-lazy-src, src attributes
const imgRegexes = [
/<img[^>]+(?:data-src|data-original|data-lazy-src|data-hi-res-src)=["']([^"'\s>]+)["']/i,
/<img[^>]+src=["']([^"'\s>]+)["']/i,
/<img[^>]+srcset=["']([^"'\s,>]+)/i,
/<source[^>]+srcset=["']([^"'\s,>]+)/i,
/url\(["']?(https?:\/\/[^'"\)\s]+\.(?:jpg|jpeg|png|webp|avif|gif))["']?\)/i
];
for (const rx of imgRegexes) {
const match = decoded.match(rx);
if (match && match[1]) {
const candidate = match[1].trim();
if (!candidate.startsWith('data:image') && !candidate.includes('1x1') && !candidate.includes('pixel') && !candidate.includes('tracker')) {
src = candidate;
break;
}
}
}
}
if (!src) return undefined;
src = decodeHtmlEntities(src).trim();
// Filter out tracking pixels / web beacons
if (src.includes('pixel') || src.includes('tracker') || src.includes('1x1') || src.includes('statcounter') || src.includes('doubleclick')) {
return undefined;
}
// Handle protocol-relative URL
if (src.startsWith('//')) {
src = `https:${src}`;
}
// Handle relative URL
if (src.startsWith('/') && baseUrl) {
try {
src = new URL(src, baseUrl).toString();
} catch (e) {
// Ignore URL parsing errors
}
}
return src;
}
// Simple heuristic sentiment analyzer
function calculateSentiment(text: string): 'positive' | 'negative' | 'neutral' {
const lower = text.toLowerCase();
const posWords = ['sucesso', 'cresce', 'lucro', 'recorde', 'avanço', 'alta', 'inovação', 'vitória', 'ganho', 'lançado', 'revolução', 'supera', 'bom'];
const negWords = ['queda', 'crise', 'alerta', 'morte', 'risco', 'prejuízo', 'falha', 'ataque', 'crime', 'violência', 'guerra', 'ameaça', 'multa', 'processo'];
let posCount = 0;
let negCount = 0;
posWords.forEach(w => { if (lower.includes(w)) posCount++; });
negWords.forEach(w => { if (lower.includes(w)) negCount++; });
if (posCount > negCount) return 'positive';
if (negCount > posCount) return 'negative';
return 'neutral';
}
// 1. Fetch & parse RSS Feeds endpoint
app.get('/api/rss', async (req: Request, res: Response) => {
try {
const urlsParam = req.query.urls as string;
if (!urlsParam) {
return res.json({ success: true, items: [] });
}
const urls = urlsParam.split(',').map(u => u.trim()).filter(Boolean);
const allItems: any[] = [];
await Promise.allSettled(
urls.map(async (url) => {
const cached = feedCache.get(url);
if (cached && Date.now() - cached.timestamp < CACHE_TTL_MS) {
allItems.push(...cached.data);
return;
}
try {
let feed: any;
try {
const resp = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
'Accept': 'application/rss+xml, application/xml, text/xml, application/atom+xml, text/html;q=0.9, */*;q=0.8',
},
signal: AbortSignal.timeout(4500),
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const text = await resp.text();
const cleanXml = text.trim().replace(/^\uFEFF/, '');
feed = await rssParser.parseString(cleanXml);
} catch {
feed = await rssParser.parseURL(url);
}
const sourceTitle = feed.title || new URL(url).hostname.replace('www.', '');
const parsedItems = (feed.items || []).map((item: any, idx: number) => {
const rawSnippet = (item.contentSnippet || item.summary || item.content || '').replace(/<[^>]*>?/gm, '').trim();
const cleanSnippet = rawSnippet.slice(0, 300) + (rawSnippet.length > 300 ? '...' : '');
const imageUrl = extractImageUrl(item, feed.link || url);
const pubTime = item.isoDate ? new Date(item.isoDate).getTime() : (item.pubDate ? new Date(item.pubDate).getTime() : Date.now());
const baseGuid = (typeof item.guid === 'string' && item.guid.trim()) ? item.guid.trim() : (item.link || url);
const itemId = `${baseGuid}#${idx}-${pubTime}`;
return {
id: itemId,
title: (item.title || 'Sem título').trim(),
link: item.link || url,
contentSnippet: cleanSnippet,
contentHtml: item.contentEncoded || item.content || item.description || '',
pubDate: item.pubDate || new Date(pubTime).toLocaleString('pt-BR'),
timestamp: isNaN(pubTime) ? Date.now() : pubTime,
sourceId: url,
sourceName: sourceTitle,
sourceCategory: 'general',
author: item.creator || item.author || sourceTitle,
imageUrl,
sentiment: calculateSentiment((item.title || '') + ' ' + cleanSnippet),
};
});
feedCache.set(url, { timestamp: Date.now(), data: parsedItems });
allItems.push(...parsedItems);
} catch (err: any) {
console.warn(`[RSS Parser] Error fetching ${url}:`, err?.message);
}
})
);
// Sort combined items by newest first
allItems.sort((a, b) => b.timestamp - a.timestamp);
res.json({ success: true, items: allItems });
} catch (err: any) {
res.status(500).json({ success: false, error: err.message });
}
});
// 2. Validate Custom Feed URL
app.post('/api/rss/validate', async (req: Request, res: Response) => {
try {
const { url } = req.body;
if (!url) return res.status(400).json({ success: false, error: 'URL é obrigatória' });
const feed = await rssParser.parseURL(url);
res.json({
success: true,
title: feed.title || new URL(url).hostname,
description: feed.description || '',
itemCount: feed.items ? feed.items.length : 0,
});
} catch (err: any) {
res.status(400).json({ success: false, error: `Não foi possível carregar o feed RSS: ${err.message}` });
}
});
// 3. Gemini Helper setup
function getGeminiClient() {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error('GEMINI_API_KEY não configurada no servidor.');
}
return new GoogleGenAI({
apiKey,
httpOptions: {
headers: {
'User-Agent': 'aistudio-build',
},
},
});
}
// 4. AI Block Summarizer Endpoint
app.post('/api/gemini/summarize', async (req: Request, res: Response) => {
try {
const { items, topic, isMultiSourceStory } = req.body;
if (!items || !Array.isArray(items) || items.length === 0) {
return res.status(400).json({ success: false, error: 'Itens de notícias inválidos' });
}
const ai = getGeminiClient();
let prompt: string;
if (isMultiSourceStory && items.length > 1) {
const sourcesText = items.slice(0, 10).map((it: any, idx: number) =>
`Fonte ${idx + 1}: [${it.sourceName}] "${it.title}"\nTrecho da cobertura: ${it.contentSnippet || ''}`
).join('\n\n');
prompt = `Você é um editor sênior de jornalismo investigativo e executivo.
As ${items.length} fontes abaixo cobrem o MESMO ACONTECIMENTO ("${topic || 'História Consolidada'}"):
${sourcesText}
Gere um resumo jornalístico consolidado em português (PT-BR) contendo:
1. **Resumo Consolidado**: Síntese direta e objetiva dos fatos centrais confirmados pelas fontes (2 a 3 frases).
2. **Fatos Compartilhados**: Pontos comuns acordados pelas diferentes coberturas.
3. **Nuances e Detalhes Específicos**: Destaques, dados ou declarações adicionais trazidos por fontes específicas (mencione os veículos correspondentes).
4. **Veredito do Radar**: Avaliação neutra e conclusiva do impacto do acontecimento.
IMPORTANTE: Limite-se aos fatos citados pelas fontes acima. Não invente informações.`;
} else {
const headlinesText = items.slice(0, 10).map((it: any, idx: number) =>
`${idx + 1}. [${it.sourceName}] "${it.title}" - ${it.contentSnippet}`
).join('\n');
prompt = `Você é um editor sênior de jornalismo executivo.
Analise as manchetes abaixo do tópico "${topic || 'Geral'}" e gere um briefing em português (PT-BR) contendo:
1. Um resumo executivo direto de 2 a 3 frases destacando os pontos principais.
2. 3 a 5 tópicos em bullet points com as tendências ou acontecimentos mais relevantes.
3. Uma linha final com o "Veredito do Radar".
Manchetes:
${headlinesText}`;
}
const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: prompt,
});
res.json({ success: true, summary: response.text });
} catch (err: any) {
console.error('[Gemini Summarize Error]:', err);
res.status(500).json({ success: false, error: err.message || 'Erro ao gerar resumo da IA' });
}
});
// 5. AI Smart Block Curator Endpoint
app.post('/api/gemini/curate', async (req: Request, res: Response) => {
try {
const { userPrompt } = req.body;
if (!userPrompt) return res.status(400).json({ success: false, error: 'Prompt é obrigatório' });
const ai = getGeminiClient();
const prompt = `Crie uma configuração de Bloco Dinâmico de Notícias baseado no desejo do usuário: "${userPrompt}".
Responda APENAS em formato JSON válido com este esquema:
{
"title": "Título criativo e com emoji para o bloco",
"filterKeyword": "palavras-chave separadas por barra vertical | para filtrar",
"categoryFilter": "tech" | "brazil" | "world" | "finance" | "sports" | "entertainment" | "ai" | "all",
"layout": "hero" | "grid" | "compact" | "list" | "media-wall" | "ticker",
"itemCount": 5,
"explanation": "Breve motivo do porquê esse bloco foi estruturado assim"
}`;
const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: prompt,
config: {
responseMimeType: 'application/json',
},
});
const parsed = JSON.parse(response.text || '{}');
res.json({ success: true, blockConfig: parsed });
} catch (err: any) {
console.error('[Gemini Curate Error]:', err);
res.status(500).json({ success: false, error: err.message || 'Erro ao curar bloco com IA' });
}
});
// 6. AI Translation Endpoint
app.post('/api/gemini/translate', async (req: Request, res: Response) => {
try {
const { title, content } = req.body;
const ai = getGeminiClient();
const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
contents: `Traduz o título e conteúdo a seguir de forma fluida para o português do Brasil:\nTítulo: ${title}\nConteúdo: ${content}`,
});
res.json({ success: true, translation: response.text });
} catch (err: any) {
res.status(500).json({ success: false, error: err.message });
}
});
// Vite Development or Production Server configuration
async function startServer() {
if (process.env.NODE_ENV !== 'production') {
// Lazy-loaded so the production bundle never needs vite at runtime.
const { createServer } = await import('vite');
const vite = await createServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
} else {
// Resolve static assets across dev, standard electron, and portable electron
const candidatePaths = [
process.env.DIST_PATH,
__dirname,
path.join(process.cwd(), 'dist'),
path.join(__dirname, '..', 'dist'),
].filter(Boolean) as string[];
const distPath = candidatePaths.find((p) => {
try {
return require('fs').existsSync(path.join(p, 'index.html'));
} catch {
return false;
}
}) || candidatePaths[0] || path.join(process.cwd(), 'dist');
console.log(`[RSS Realtime News Server] Serving static files from: ${distPath}`);
app.use(express.static(distPath));
app.get('*', (_req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`[RSS Realtime News Server] Server running on http://0.0.0.0:${PORT}`);
});
}
startServer();