VinOS Agent commited on
Commit
3af90a1
·
1 Parent(s): 06fe38b

fix: AI scraping fallback + init sheet url click

Browse files
Files changed (3) hide show
  1. public/social-dashboard.html +11 -2
  2. research.js +40 -2
  3. sheets.js +4 -1
public/social-dashboard.html CHANGED
@@ -447,8 +447,17 @@
447
  document.getElementById('btn-sync-sheets').onclick = async () => {
448
  const res = await fetch('/api/social/init-sheets');
449
  const data = await res.json();
450
- if (data.success) alert('Google Sheet updated with new tabs!');
451
- else alert('Error: ' + data.error);
 
 
 
 
 
 
 
 
 
452
  };
453
 
454
  // Init Chart
 
447
  document.getElementById('btn-sync-sheets').onclick = async () => {
448
  const res = await fetch('/api/social/init-sheets');
449
  const data = await res.json();
450
+ if (data.success) {
451
+ if(data.url) {
452
+ if(confirm('Google Sheet is ready! Click OK to open it now.\n' + data.url)) {
453
+ window.open(data.url, '_blank');
454
+ }
455
+ } else {
456
+ alert('Google Sheet updated with new tabs!');
457
+ }
458
+ } else {
459
+ alert('Failed to initialize sheets: ' + data.error);
460
+ }
461
  };
462
 
463
  // Init Chart
research.js CHANGED
@@ -1,4 +1,5 @@
1
  const axios = require('axios');
 
2
 
3
  const APIFY_TOKEN = process.env.APIFY_API_KEY;
4
 
@@ -83,8 +84,45 @@ class ResearchModule {
83
  return { success: true, data: result };
84
 
85
  } catch (error) {
86
- console.error('[Research] Apify error:', error.response?.data || error.message);
87
- return { success: false, error: error.message };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  }
89
  }
90
  }
 
1
  const axios = require('axios');
2
+ const apiCaller = require('./skills/api_caller');
3
 
4
  const APIFY_TOKEN = process.env.APIFY_API_KEY;
5
 
 
84
  return { success: true, data: result };
85
 
86
  } catch (error) {
87
+ console.log('[Research] Apify failed or timed out. Falling back to AI Scraper (Perplexity)...');
88
+ return await this.fallbackAIScraper(url, platform);
89
+ }
90
+ }
91
+
92
+ async fallbackAIScraper(url, platform) {
93
+ try {
94
+ console.log(`[Research] AI Scraping via Perplexity: ${url}`);
95
+ const prompt = `You are a web scraping AI. Visit this URL: ${url}
96
+ Analyze the page content (it is a social media post from ${platform}).
97
+ Return ONLY a valid JSON object with EXACTLY this structure, no markdown formatting or extra text:
98
+ {
99
+ "platform": "${platform}",
100
+ "originalUrl": "${url}",
101
+ "text": "The full main caption or text of the post.",
102
+ "author": "The username or name of the creator",
103
+ "engagement": { "likes": 0, "shares": 0, "comments": 0 },
104
+ "mediaUrls": ["url1", "url2"]
105
+ }
106
+ If you cannot see the exact engagement numbers, just guess 0. If you cannot extract media URLs, return an empty array. Do your best to extract the main text.`;
107
+
108
+ // Using perplexity sonar online model via OpenRouter for live web access
109
+ const aiRes = await apiCaller.callOpenRouter([{ role: 'user', content: prompt }], 'perplexity/llama-3.1-sonar-large-128k-online');
110
+
111
+ if (aiRes.success) {
112
+ // Clean markdown if perplexity added any
113
+ const jsonStr = aiRes.data.replace(/```json|```/g, '').trim();
114
+ const data = JSON.parse(jsonStr);
115
+
116
+ // Ensure mediaUrls is an array
117
+ if (!Array.isArray(data.mediaUrls)) data.mediaUrls = [];
118
+
119
+ return { success: true, data };
120
+ } else {
121
+ return { success: false, error: 'AI Scrape failed: ' + aiRes.error };
122
+ }
123
+ } catch (e) {
124
+ console.error('[Research] AI Fallback error:', e.message);
125
+ return { success: false, error: 'Both Apify and AI fallback failed.' };
126
  }
127
  }
128
  }
sheets.js CHANGED
@@ -68,7 +68,10 @@ class SheetsModule {
68
  });
69
  }
70
 
71
- return { success: true };
 
 
 
72
  } catch (error) {
73
  console.error('[Sheets] Init failed:', error.message);
74
  return { success: false, error: error.message };
 
68
  });
69
  }
70
 
71
+ return {
72
+ success: true,
73
+ url: `https://docs.google.com/spreadsheets/d/${this.sheetId}/edit`
74
+ };
75
  } catch (error) {
76
  console.error('[Sheets] Init failed:', error.message);
77
  return { success: false, error: error.message };