Skip to content

Commit

Permalink
Remove unnecessary lines in app.js file.
Browse files Browse the repository at this point in the history
  • Loading branch information
tf7software committed Sep 28, 2024
1 parent cda1596 commit a77aae2
Showing 1 changed file with 16 additions and 85 deletions.
101 changes: 16 additions & 85 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ app.get('/view', (req, res) => {
res.sendFile(path.join(__dirname, 'views/view.html'));
});



// Serve homepage
app.get('/snake', (req, res) => {
res.sendFile(path.join(__dirname, 'views/snake.html'));
Expand Down Expand Up @@ -73,26 +71,25 @@ const sanitizeScrapedData = (text) => {
return text.replace(/[\n\r]/g, ' ').trim(); // Remove newlines, trim whitespace
};

// Function to scrape search results from SerpAPI
const scrapeSerpApiSearch = async (query) => {
// Function to scrape search results from ScrAPI
const scrapeScrAPI = async (query) => {
if (searchCache.has(query)) {
console.log("Serving from cache");
return searchCache.get(query);
}

const apiKey = process.env.SERPAPI_API_KEY;
const formattedQuery = encodeURIComponent(query);
const url = `https://serpapi.com/search.json?q=${formattedQuery}&api_key=${apiKey}`;
const url = `https://scrapi.pythonanywhere.com/search?q=${formattedQuery}&n=10`;

try {
const { data } = await axios.get(url);

if (!data.organic_results || !Array.isArray(data.organic_results)) {
console.error("No organic results found in the response.");
if (!Array.isArray(data.results)) {
console.error("No results found in the response.");
return [];
}

const links = data.organic_results.map(result => result.link).filter(link => link && link.startsWith('http'));
const links = data.results.map(result => result.link).filter(link => link && link.startsWith('http'));
console.log("Collected URLs:", links);

// Cache the result for 24 hours
Expand All @@ -101,35 +98,7 @@ const scrapeSerpApiSearch = async (query) => {

return links;
} catch (error) {
console.error("Error scraping SerpAPI:", error);
return [];
}
};

// Function to scrape images from SerpAPI
const scrapeSerpApiImages = async (query) => {
if (searchCache.has(query)) {
console.log("Serving images from cache");
return searchCache.get(query);
}

const apiKey = process.env.SERPAPI_API_KEY;
const url = `https://scrapi.pythonanywhere.com/search?q=${query}&n=10`;

try {
const { data } = await axios.get(url);
const images = data.images_results.slice(0, 10).map(img => ({
thumbnail: img.thumbnail,
original: img.original
}));

// Cache the result for 24 hours
searchCache.set(query, images);
setTimeout(() => searchCache.delete(query), 24 * 60 * 60 * 1000);

return images;
} catch (error) {
console.error("Error scraping SerpAPI images:", error);
console.error("Error scraping ScrAPI:", error);
return [];
}
};
Expand All @@ -155,11 +124,11 @@ app.post('/search', limiter, async (req, res) => {
}

try {
const lookupResult = await scrapeSerpApiSearch(query);
const lookupResult = await scrapeScrAPI(query);
console.log("Scraped URLs:", lookupResult);

if (!Array.isArray(lookupResult) || lookupResult.length === 0) {
const errorMsg = "No results found from SerpAPI. Please try a different query.";
const errorMsg = "No results found from ScrAPI. Please try a different query.";
const articleHtml = fs.readFileSync(path.join(__dirname, 'views/template.html'), 'utf8')
.replace(/{{title}}/g, query)
.replace(/{{content}}/g, "No content generated as there were no URLs.")
Expand All @@ -182,20 +151,8 @@ app.post('/search', limiter, async (req, res) => {
console.log("Generated URL List:", urlList);
articleHtml = articleHtml.replace(/{{urls}}/g, urlList);

try {
const images = await scrapeSerpApiImages(query);
const imageGallery = images.length > 0
? images.map(img => `<img src="${img.thumbnail}" alt="${query} image">`).join('')
: "No images available";

articleHtml = articleHtml.replace(/{{imageGallery}}/g, imageGallery);

fs.writeFileSync(filePath, articleHtml);
res.redirect(`/articles/${sanitizedQuery}`);
} catch (imageError) {
console.error("Error generating the image gallery:", imageError);
res.status(500).send("Error generating the image gallery.");
}
fs.writeFileSync(filePath, articleHtml);
res.redirect(`/articles/${sanitizedQuery}`);
} catch (error) {
console.error("Error during the search process:", error.message);
res.status(500).send("An unexpected error occurred: " + error.message);
Expand Down Expand Up @@ -223,7 +180,6 @@ app.get('/suggest', (req, res) => {
});
});

// Serve the generated article pages or create them if they don't exist
// Serve the generated article pages or create them if they don't exist
app.get('/articles/:article', async (req, res) => {
const article = req.params.article;
Expand All @@ -235,13 +191,10 @@ app.get('/articles/:article', async (req, res) => {
}

try {
// Convert the article name back to a readable format
const query = article.replace(/-/g, ' ');
const query = article.replace(/-/g, ' ');

// Scrape information from SerpAPI
const lookupResult = await scrapeSerpApiSearch(query);
const lookupResult = await scrapeScrAPI(query);

// Check if any results were found
if (!Array.isArray(lookupResult) || lookupResult.length === 0) {
const errorMsg = "No content found for this article.";
const articleHtml = fs.readFileSync(path.join(__dirname, 'views/template.html'), 'utf8')
Expand All @@ -252,49 +205,27 @@ app.get('/articles/:article', async (req, res) => {
return res.sendFile(filePath);
}

// Generate a prompt for the AI content generation
const prompt = `You are Infintium. You have two purposes. If the user prompt is a math problem, solve it until it is COMPLETELY simplified. If it is a question, answer it with your own knowledge. If it is an item, such as a toaster, song, or anything that is a statement, act like Wikipedia and provide as much information as possible. USER PROMPT: ${query}`;

// Generate AI content using the prompt
const result = await model.generateContent(prompt);
const markdownContent = markdown.render(result.response.text());

// Load the HTML template
let articleHtml = fs.readFileSync(path.join(__dirname, 'views/template.html'), 'utf8');

// Replace placeholders with the search query and AI content
articleHtml = articleHtml.replace(/{{title}}/g, query);
articleHtml = articleHtml.replace(/{{content}}/g, markdownContent);

// Create a list of URLs for the article
const urlList = lookupResult.map(url => `<li><a href="${url}" target="_blank">${url}</a></li>`).join('');
articleHtml = articleHtml.replace(/{{urls}}/g, urlList);

// Generate the image gallery in the article HTML
try {
const images = await scrapeSerpApiImages(query);

// Check if images were fetched successfully
const imageGallery = images.length > 0
? images.map(img => `<img src="${img.original}" alt="${query} image" style="width: 200px; height: auto; margin: 5px;">`).join('')
: '<p>No images available</p>';

articleHtml = articleHtml.replace(/{{imageGallery}}/g, imageGallery);

// Save the generated HTML file
fs.writeFileSync(filePath, articleHtml);
res.sendFile(filePath);
} catch (imageError) {
console.error("Error generating the image gallery:", imageError);
res.status(500).send("Error generating the image gallery.");
}
fs.writeFileSync(filePath, articleHtml);
res.sendFile(filePath);
} catch (error) {
console.error("Error generating the article:", error);
res.status(500).send("An unexpected error occurred: " + error.message);
}
});


app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});

0 comments on commit a77aae2

Please sign in to comment.