learndb/netlify/functions/handleMetadata.js

111 wiersze
4.3 KiB
JavaScript
Czysty Zwykły widok Historia

const https = require('https'); // Import for webscraping (fetchContentFromURL(url) function
2023-11-24 17:40:49 +00:00
import { OpenAIApi, Configuration } from 'openai';
2023-11-24 17:52:38 +00:00
import { fetch } from 'node-fetch';
2023-11-23 16:32:58 +00:00
// Function to fetch content from URL using a web scraping service
async function fetchContentFromURL(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.text();
} catch (error) {
console.error(`Could not fetch content from URL: ${error}`);
throw error;
}
2023-11-22 12:33:41 +00:00
}
function simplifyContent(content) {
2023-11-24 14:34:13 +00:00
// Remove HTML tags
let simplifiedContent = content.replace(/<[^>]*>/g, '');
2023-11-24 14:34:13 +00:00
// Remove CSS within style tags
simplifiedContent = simplifiedContent.replace(/<style[^>]*>.*<\/style>/gms, '');
2023-11-24 14:34:13 +00:00
// Remove inline CSS and JavaScript within script tags
simplifiedContent = simplifiedContent.replace(/<script[^>]*>.*<\/script>/gms, '');
// Remove special characters and HTML entities
simplifiedContent = simplifiedContent.replace(/[^\w\s]/gi, '').replace(/&[a-z]+;/gi, '');
// Remove URLs
simplifiedContent = simplifiedContent.replace(/https?:\/\/[^\s]+/gi, '');
2023-11-24 14:34:13 +00:00
// Normalize whitespace
simplifiedContent = simplifiedContent.replace(/\s+/g, ' ').trim();
2023-11-24 14:34:13 +00:00
// Basic language simplification
simplifiedContent = simplifiedContent.toLowerCase();
2023-11-24 15:39:31 +00:00
// // Simple summarization: taking the first few sentences
// const sentences = simplifiedContent.split('. ');
// const summarizedContent = sentences.slice(0, Math.min(5, sentences.length)).join('. ');
2023-11-24 15:40:13 +00:00
return simplifiedContent;
2023-11-22 12:33:41 +00:00
}
// Placeholder function to perform GPT analysis for media type and topics using Mistral-7b via OpenRouter
async function performGPTAnalysis(content) {
// Implement logic to send content to Mistral-7b via OpenRouter for GPT analysis
// Send content and receive GPT analysis response
// Placeholder code
const inferredMediaType = "article";
const extractedTopics = ["topic1", "topic2"];
return { inferredMediaType, extractedTopics };
2023-11-22 12:33:41 +00:00
}
// Placeholder function to map inferred values to predefined formats and topics
function mapInferredValues(mediaType, topics) {
// Implement logic to map inferred media type and topics to predefined formats and topics
// Match inferred values with predefined taxonomy
// Placeholder code
const predefinedMediaType = "Article";
const predefinedTopics = ["Topic 1", "Topic 2"];
return { predefinedMediaType, predefinedTopics };
2023-11-22 12:33:41 +00:00
}
// Placeholder function to format the response
function formatResponse(predefinedMediaType, predefinedTopics) {
// Implement logic to format the extracted metadata into the desired response structure
// Construct the response object
// Placeholder code
const response = {
2023-11-22 20:44:21 +00:00
format: predefinedMediaType,
2023-11-23 15:42:06 +00:00
topics: predefinedTopics,
};
return response;
2023-11-22 12:33:41 +00:00
}
export async function handler(event) {
try {
2023-11-22 12:33:41 +00:00
// Extract URL and API Key from the request body
const { url, apiKey } = JSON.parse(event.body);
// Validate if URL and API Key are present
if (!url || !apiKey) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'URL and API Key are required' }),
};
}
// Step 1: Fetch content from the URL using a web scraping service
const fetchedContent = await fetchContentFromURL(url);
// Step 2: Simplify the fetched content for GPT analysis
const simplifiedContent = simplifyContent(fetchedContent);
// Step 3: Perform GPT analysis for media type and topics
const { inferredMediaType, extractedTopics } = await performGPTAnalysis(simplifiedContent);
// Step 4: Map inferred values to predefined formats and topics
const { predefinedMediaType, predefinedTopics } = mapInferredValues(inferredMediaType, extractedTopics);
// Step 5: Format the response
const formattedResponse = formatResponse(predefinedMediaType, predefinedTopics);
// Return the formatted response
return {
statusCode: 200,
body: JSON.stringify(fetchedContent),
2023-11-22 12:33:41 +00:00
};
} catch (error) {
2023-11-22 12:33:41 +00:00
return {
statusCode: 500,
body: JSON.stringify({ error: 'Something went wrong' }),
};
}
2023-11-23 16:26:25 +00:00
}