Luca448 commited on
Commit
3b94efc
·
1 Parent(s): 80c65d2

Feat: Add Tavily Web Search support to backend News Generation to guarantee API functionality, and decrease generation frequency to 6h

Browse files
index.html CHANGED
@@ -51,7 +51,7 @@
51
  </head>
52
  <body class="dark-theme in-chat-view">
53
 
54
- <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.3.4</div>
55
 
56
  <!-- SVG Filters for Liquid Glass Refraction -->
57
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
 
51
  </head>
52
  <body class="dark-theme in-chat-view">
53
 
54
+ <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.3.5</div>
55
 
56
  <!-- SVG Filters for Liquid Glass Refraction -->
57
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
server/news-service.js CHANGED
@@ -8,7 +8,7 @@ const __filename = fileURLToPath(import.meta.url);
8
  const __dirname = path.dirname(__filename);
9
  const DATA_FILE = path.join(__dirname, '../server_data/news.json');
10
 
11
- const GENERATION_INTERVAL_MS = 3 * 60 * 60 * 1000; // 3 hours
12
 
13
  let data = {
14
  topics: ['KI in der Bildung', 'Abitur Vorbereitung'],
@@ -47,8 +47,43 @@ function saveData() {
47
  }
48
  }
49
 
50
- // Search Web (Google News RSS)
51
- async function searchDDG(query) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  try {
53
  const url = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=de&gl=DE&ceid=DE:de`;
54
  const res = await fetch(url);
@@ -61,7 +96,6 @@ async function searchDDG(query) {
61
  const title = $(el).find('title').text();
62
  const link = $(el).find('link').text();
63
  const description = $(el).find('description').text();
64
- // Strip HTML tags from description snippet
65
  const snippet = cheerio.load(description).text().trim() || title;
66
 
67
  if (title && link) {
@@ -184,7 +218,7 @@ async function runGenerationCycle(simulateError = false) {
184
  for (const topic of topicsToProcess) {
185
  try {
186
  console.log(`[NewsService] Processing topic: ${topic}`);
187
- const results = await searchDDG(topic);
188
  if (results.length === 0) {
189
  console.log(`[NewsService] No search results for ${topic}, falling back to internal knowledge.`);
190
  }
 
8
  const __dirname = path.dirname(__filename);
9
  const DATA_FILE = path.join(__dirname, '../server_data/news.json');
10
 
11
+ const GENERATION_INTERVAL_MS = 6 * 60 * 60 * 1000; // 6 hours
12
 
13
  let data = {
14
  topics: ['KI in der Bildung', 'Abitur Vorbereitung'],
 
47
  }
48
  }
49
 
50
+ // Search Web (Tavily as primary, Google News RSS as fallback)
51
+ async function searchWeb(query) {
52
+ const tavilyKey = process.env.TAVILY_API_KEY;
53
+
54
+ if (tavilyKey) {
55
+ console.log(`[NewsService] Searching with Tavily for: ${query}`);
56
+ try {
57
+ const response = await fetch('https://api.tavily.com/search', {
58
+ method: 'POST',
59
+ headers: { 'Content-Type': 'application/json' },
60
+ body: JSON.stringify({
61
+ api_key: tavilyKey,
62
+ query: query,
63
+ search_depth: 'basic',
64
+ include_answer: false,
65
+ include_raw_content: false,
66
+ max_results: 5,
67
+ }),
68
+ });
69
+
70
+ if (response.ok) {
71
+ const data = await response.json();
72
+ return data.results.map(r => ({
73
+ title: r.title,
74
+ snippet: r.content,
75
+ url: r.url
76
+ }));
77
+ } else {
78
+ console.warn(`[NewsService] Tavily failed with status ${response.status}`);
79
+ }
80
+ } catch (e) {
81
+ console.error('[NewsService] Tavily Error:', e);
82
+ }
83
+ }
84
+
85
+ // Fallback to Google News RSS if Tavily is missing or failed
86
+ console.log(`[NewsService] Searching with Google News RSS for: ${query}`);
87
  try {
88
  const url = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=de&gl=DE&ceid=DE:de`;
89
  const res = await fetch(url);
 
96
  const title = $(el).find('title').text();
97
  const link = $(el).find('link').text();
98
  const description = $(el).find('description').text();
 
99
  const snippet = cheerio.load(description).text().trim() || title;
100
 
101
  if (title && link) {
 
218
  for (const topic of topicsToProcess) {
219
  try {
220
  console.log(`[NewsService] Processing topic: ${topic}`);
221
+ const results = await searchWeb(topic);
222
  if (results.length === 0) {
223
  console.log(`[NewsService] No search results for ${topic}, falling back to internal knowledge.`);
224
  }
test-gemini.js → test-gemini.cjs RENAMED
File without changes