-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
215 lines (184 loc) · 6.57 KB
/
server.js
File metadata and controls
215 lines (184 loc) · 6.57 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
import express from 'express';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import * as cheerio from 'cheerio';
dotenv.config();
const app = express();
// Add CORS headers
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
app.use(express.json());
app.post('/api/gemini-chat', async (req, res) => {
const { message, roadmapData } = req.body;
const apiKey = process.env.GEMINI_CHAT_API_KEY;
if (!apiKey) return res.status(500).json({ message: 'Gemini chat API key not set.' });
const prompt = `
You are an AI assistant for learning roadmaps.
Roadmap data: ${JSON.stringify(roadmapData)}
User question: ${message}
Please answer based on the roadmap content.
`;
try {
const geminiRes = await fetch('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + apiKey, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: prompt }] }]
}),
});
const data = await geminiRes.json();
const aiMessage = data?.candidates?.[0]?.content?.parts?.[0]?.text || 'Sorry, I could not generate a response.';
res.status(200).json({ message: aiMessage });
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Error contacting Gemini API.' });
}
});
app.post('/api/extract-content', async (req, res) => {
try {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}
// Validate URL format
try {
new URL(url);
} catch {
return res.status(400).json({ error: 'Invalid URL format' });
}
// Fetch the webpage content
const response = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
},
});
if (!response.ok) {
return res.status(response.status).json({
error: `Failed to fetch URL: ${response.status} ${response.statusText}`
});
}
const html = await response.text();
const $ = cheerio.load(html);
// Remove unwanted elements
$('script, style, nav, header, footer, aside, .ad, .ads, .advertisement, .sidebar, .menu, .navigation, .breadcrumb, .pagination, .comments, .social-share, .related-posts').remove();
// Extract title
const title = $('title').text().trim() ||
$('h1').first().text().trim() ||
$('meta[property="og:title"]').attr('content') ||
'Web Content';
// Try to find the main content area
const contentSelectors = [
'main',
'article',
'.content',
'.post-content',
'.entry-content',
'.article-content',
'.main-content',
'#content',
'#main',
'.post',
'.article'
];
let contentElement = null;
for (const selector of contentSelectors) {
contentElement = $(selector).first();
if (contentElement.length > 0) {
break;
}
}
// If no specific content area found, use body
if (!contentElement || contentElement.length === 0) {
contentElement = $('body');
}
// Extract text content with structure
const structuredContent = [];
// Add title as heading
if (title) {
structuredContent.push({ type: 'heading', text: title });
}
// Extract headings and paragraphs
contentElement.find('h1, h2, h3, h4, h5, h6, p, li, blockquote').each((index, element) => {
const $element = $(element);
const tagName = element.tagName.toLowerCase();
const text = $element.text().trim();
if (text && text.length > 10) { // Only include substantial content
if (tagName.startsWith('h')) {
structuredContent.push({ type: 'heading', text });
} else {
structuredContent.push({ type: 'paragraph', text });
}
}
});
// If we didn't get enough structured content, fall back to plain text
if (structuredContent.length < 3) {
const plainText = contentElement.text()
.replace(/\s+/g, ' ')
.trim()
.substring(0, 5000); // Limit to 5000 characters
if (plainText) {
structuredContent.push({ type: 'paragraph', text: plainText });
}
}
// If still no content, return a basic structure
if (structuredContent.length === 0) {
structuredContent.push(
{ type: 'heading', text: 'Web Content' },
{ type: 'paragraph', text: `Content extracted from: ${url}` }
);
}
return res.status(200).json({
success: true,
url,
title,
content: structuredContent,
contentLength: structuredContent.reduce((acc, item) => acc + item.text.length, 0)
});
} catch (error) {
console.error('Error extracting content:', error);
return res.status(500).json({
error: 'Failed to extract content from URL',
details: error instanceof Error ? error.message : 'Unknown error'
});
}
});
app.post('/api/image-search', async (req, res) => {
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
const GOOGLE_CSE_ID = process.env.GOOGLE_CSE_ID;
const { query } = req.body;
if (!GOOGLE_API_KEY || !GOOGLE_CSE_ID) {
return res.status(500).json({ error: 'Google API credentials not set.' });
}
if (!query || typeof query !== 'string') {
return res.status(400).json({ error: 'Missing or invalid query.' });
}
try {
const apiUrl = `https://www.googleapis.com/customsearch/v1?key=${GOOGLE_API_KEY}&cx=${GOOGLE_CSE_ID}&q=${encodeURIComponent(query)}&searchType=image&num=6`;
const response = await fetch(apiUrl);
const data = await response.json();
console.log('Google API response:', JSON.stringify(data, null, 2));
const images = (data.items || []).map((item) => ({
url: item.link,
thumbnailUrl: item.image?.thumbnailLink || item.link,
title: item.title,
alt: item.title,
source: item.displayLink,
width: item.image?.width,
height: item.image?.height,
}));
res.status(200).json({ images });
} catch (error) {
console.error('Image search error:', error);
res.status(500).json({ error: 'Failed to fetch images.' });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));