2024-04-15 17:01:47 -04:00
|
|
|
import * as cheerio from "cheerio";
|
|
|
|
import { extractMetadata } from "./utils/metadata";
|
|
|
|
import dotenv from "dotenv";
|
2024-07-03 18:01:17 -03:00
|
|
|
import {
|
|
|
|
Document,
|
|
|
|
PageOptions,
|
|
|
|
FireEngineResponse,
|
|
|
|
ExtractorOptions,
|
|
|
|
} from "../../lib/entities";
|
2024-04-15 17:01:47 -04:00
|
|
|
import { parseMarkdown } from "../../lib/html-to-markdown";
|
2024-04-28 11:34:25 -07:00
|
|
|
import { urlSpecificParams } from "./utils/custom/website_params";
|
2024-05-13 09:13:42 -03:00
|
|
|
import { fetchAndProcessPdf } from "./utils/pdfProcessor";
|
2024-06-04 12:15:39 -07:00
|
|
|
import { handleCustomScraping } from "./custom/handleCustomScraping";
|
2024-06-18 09:46:42 -03:00
|
|
|
import { removeUnwantedElements } from "./utils/removeUnwantedElements";
|
2024-07-03 18:01:17 -03:00
|
|
|
import { scrapWithFetch } from "./scrapers/fetch";
|
|
|
|
import { scrapWithFireEngine } from "./scrapers/fireEngine";
|
|
|
|
import { scrapWithPlaywright } from "./scrapers/playwright";
|
|
|
|
import { scrapWithScrapingBee } from "./scrapers/scrapingBee";
|
2024-04-15 17:01:47 -04:00
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
2024-05-21 18:34:23 -07:00
|
|
|
const baseScrapers = [
|
|
|
|
"fire-engine",
|
|
|
|
"scrapingBee",
|
|
|
|
"playwright",
|
|
|
|
"scrapingBeeLoad",
|
|
|
|
"fetch",
|
|
|
|
] as const;
|
|
|
|
|
2024-04-28 11:34:25 -07:00
|
|
|
export async function generateRequestParams(
|
|
|
|
url: string,
|
|
|
|
wait_browser: string = "domcontentloaded",
|
|
|
|
timeout: number = 15000
|
|
|
|
): Promise<any> {
|
|
|
|
const defaultParams = {
|
|
|
|
url: url,
|
|
|
|
params: { timeout: timeout, wait_browser: wait_browser },
|
|
|
|
headers: { "ScrapingService-Request": "TRUE" },
|
|
|
|
};
|
|
|
|
|
2024-04-28 12:44:00 -07:00
|
|
|
try {
|
2024-05-09 17:45:16 -07:00
|
|
|
const urlKey = new URL(url).hostname.replace(/^www\./, "");
|
2024-04-28 12:44:00 -07:00
|
|
|
if (urlSpecificParams.hasOwnProperty(urlKey)) {
|
|
|
|
return { ...defaultParams, ...urlSpecificParams[urlKey] };
|
|
|
|
} else {
|
|
|
|
return defaultParams;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error generating URL key: ${error}`);
|
2024-04-28 11:34:25 -07:00
|
|
|
return defaultParams;
|
|
|
|
}
|
|
|
|
}
|
2024-05-21 18:50:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the order of scrapers to be used for scraping a URL
|
|
|
|
* If the user doesn't have envs set for a specific scraper, it will be removed from the order.
|
|
|
|
* @param defaultScraper The default scraper to use if the URL does not have a specific scraper order defined
|
|
|
|
* @returns The order of scrapers to be used for scraping a URL
|
|
|
|
*/
|
2024-05-31 15:39:54 -07:00
|
|
|
function getScrapingFallbackOrder(
|
|
|
|
defaultScraper?: string,
|
|
|
|
isWaitPresent: boolean = false,
|
|
|
|
isScreenshotPresent: boolean = false,
|
|
|
|
isHeadersPresent: boolean = false
|
|
|
|
) {
|
|
|
|
const availableScrapers = baseScrapers.filter((scraper) => {
|
2024-05-21 18:50:42 -07:00
|
|
|
switch (scraper) {
|
|
|
|
case "scrapingBee":
|
|
|
|
case "scrapingBeeLoad":
|
|
|
|
return !!process.env.SCRAPING_BEE_API_KEY;
|
|
|
|
case "fire-engine":
|
|
|
|
return !!process.env.FIRE_ENGINE_BETA_URL;
|
|
|
|
case "playwright":
|
|
|
|
return !!process.env.PLAYWRIGHT_MICROSERVICE_URL;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-05-31 15:39:54 -07:00
|
|
|
let defaultOrder = [
|
|
|
|
"scrapingBee",
|
|
|
|
"fire-engine",
|
|
|
|
"playwright",
|
|
|
|
"scrapingBeeLoad",
|
|
|
|
"fetch",
|
|
|
|
];
|
|
|
|
|
|
|
|
if (isWaitPresent || isScreenshotPresent || isHeadersPresent) {
|
|
|
|
defaultOrder = [
|
|
|
|
"fire-engine",
|
|
|
|
"playwright",
|
|
|
|
...defaultOrder.filter(
|
|
|
|
(scraper) => scraper !== "fire-engine" && scraper !== "playwright"
|
|
|
|
),
|
|
|
|
];
|
2024-05-28 12:56:24 -07:00
|
|
|
}
|
|
|
|
|
2024-05-31 15:39:54 -07:00
|
|
|
const filteredDefaultOrder = defaultOrder.filter(
|
|
|
|
(scraper: (typeof baseScrapers)[number]) =>
|
|
|
|
availableScrapers.includes(scraper)
|
|
|
|
);
|
|
|
|
const uniqueScrapers = new Set(
|
|
|
|
defaultScraper
|
|
|
|
? [defaultScraper, ...filteredDefaultOrder, ...availableScrapers]
|
|
|
|
: [...filteredDefaultOrder, ...availableScrapers]
|
|
|
|
);
|
2024-06-14 15:14:01 -03:00
|
|
|
|
2024-05-21 18:34:23 -07:00
|
|
|
const scrapersInOrder = Array.from(uniqueScrapers);
|
2024-05-31 15:39:54 -07:00
|
|
|
return scrapersInOrder as (typeof baseScrapers)[number][];
|
2024-05-21 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2024-07-16 18:38:03 -07:00
|
|
|
function extractLinks(html: string, baseUrl: string): string[] {
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
const links: string[] = [];
|
|
|
|
|
|
|
|
// Parse the base URL to get the origin
|
|
|
|
const urlObject = new URL(baseUrl);
|
|
|
|
const origin = urlObject.origin;
|
|
|
|
|
|
|
|
$('a').each((_, element) => {
|
|
|
|
const href = $(element).attr('href');
|
|
|
|
if (href) {
|
|
|
|
if (href.startsWith('http://') || href.startsWith('https://')) {
|
|
|
|
// Absolute URL, add as is
|
|
|
|
links.push(href);
|
|
|
|
} else if (href.startsWith('/')) {
|
|
|
|
// Relative URL starting with '/', append to origin
|
|
|
|
links.push(`${origin}${href}`);
|
|
|
|
} else if (!href.startsWith('#') && !href.startsWith('mailto:')) {
|
|
|
|
// Relative URL not starting with '/', append to base URL
|
|
|
|
links.push(`${baseUrl}/${href}`);
|
|
|
|
} else if (href.startsWith('mailto:')) {
|
|
|
|
// mailto: links, add as is
|
|
|
|
links.push(href);
|
|
|
|
}
|
|
|
|
// Fragment-only links (#) are ignored
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove duplicates and return
|
|
|
|
return [...new Set(links)];
|
|
|
|
}
|
|
|
|
|
2024-04-15 17:01:47 -04:00
|
|
|
export async function scrapSingleUrl(
|
|
|
|
urlToScrap: string,
|
2024-05-31 15:39:54 -07:00
|
|
|
pageOptions: PageOptions = {
|
|
|
|
onlyMainContent: true,
|
|
|
|
includeHtml: false,
|
2024-06-28 17:07:47 -04:00
|
|
|
includeRawHtml: false,
|
2024-05-31 15:39:54 -07:00
|
|
|
waitFor: 0,
|
|
|
|
screenshot: false,
|
2024-06-28 15:51:18 -03:00
|
|
|
headers: undefined,
|
2024-05-31 15:39:54 -07:00
|
|
|
},
|
2024-06-28 16:39:09 -04:00
|
|
|
extractorOptions: ExtractorOptions = {
|
2024-07-03 18:01:17 -03:00
|
|
|
mode: "llm-extraction-from-markdown",
|
2024-06-28 16:39:09 -04:00
|
|
|
},
|
2024-05-15 11:28:20 -07:00
|
|
|
existingHtml: string = ""
|
2024-04-15 17:01:47 -04:00
|
|
|
): Promise<Document> {
|
|
|
|
urlToScrap = urlToScrap.trim();
|
|
|
|
|
2024-04-16 12:06:46 -04:00
|
|
|
const attemptScraping = async (
|
|
|
|
url: string,
|
2024-05-31 15:39:54 -07:00
|
|
|
method: (typeof baseScrapers)[number]
|
2024-06-28 15:51:18 -03:00
|
|
|
) => {
|
|
|
|
let scraperResponse: {
|
|
|
|
text: string;
|
|
|
|
screenshot: string;
|
|
|
|
metadata: { pageStatusCode?: number; pageError?: string | null };
|
|
|
|
} = { text: "", screenshot: "", metadata: {} };
|
2024-05-29 18:56:57 -04:00
|
|
|
let screenshot = "";
|
2024-04-15 17:01:47 -04:00
|
|
|
switch (method) {
|
2024-05-21 18:34:23 -07:00
|
|
|
case "fire-engine":
|
2024-05-21 18:50:42 -07:00
|
|
|
if (process.env.FIRE_ENGINE_BETA_URL) {
|
2024-05-28 12:56:24 -07:00
|
|
|
console.log(`Scraping ${url} with Fire Engine`);
|
2024-06-28 15:45:16 -03:00
|
|
|
const response = await scrapWithFireEngine({
|
2024-05-31 15:39:54 -07:00
|
|
|
url,
|
2024-06-28 15:45:16 -03:00
|
|
|
waitFor: pageOptions.waitFor,
|
|
|
|
screenshot: pageOptions.screenshot,
|
|
|
|
pageOptions: pageOptions,
|
2024-06-28 15:51:18 -03:00
|
|
|
headers: pageOptions.headers,
|
|
|
|
});
|
2024-06-13 17:08:40 -03:00
|
|
|
scraperResponse.text = response.html;
|
|
|
|
scraperResponse.screenshot = response.screenshot;
|
|
|
|
scraperResponse.metadata.pageStatusCode = response.pageStatusCode;
|
|
|
|
scraperResponse.metadata.pageError = response.pageError;
|
2024-05-21 18:50:42 -07:00
|
|
|
}
|
2024-04-16 12:06:46 -04:00
|
|
|
break;
|
|
|
|
case "scrapingBee":
|
2024-04-15 17:01:47 -04:00
|
|
|
if (process.env.SCRAPING_BEE_API_KEY) {
|
2024-06-13 17:08:40 -03:00
|
|
|
const response = await scrapWithScrapingBee(
|
2024-04-28 11:34:25 -07:00
|
|
|
url,
|
|
|
|
"domcontentloaded",
|
|
|
|
pageOptions.fallback === false ? 7000 : 15000
|
|
|
|
);
|
2024-06-13 17:08:40 -03:00
|
|
|
scraperResponse.text = response.content;
|
|
|
|
scraperResponse.metadata.pageStatusCode = response.pageStatusCode;
|
|
|
|
scraperResponse.metadata.pageError = response.pageError;
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|
|
|
|
break;
|
2024-04-16 12:06:46 -04:00
|
|
|
case "playwright":
|
2024-04-15 17:01:47 -04:00
|
|
|
if (process.env.PLAYWRIGHT_MICROSERVICE_URL) {
|
2024-06-28 15:51:18 -03:00
|
|
|
const response = await scrapWithPlaywright(
|
|
|
|
url,
|
|
|
|
pageOptions.waitFor,
|
|
|
|
pageOptions.headers
|
|
|
|
);
|
2024-06-13 17:08:40 -03:00
|
|
|
scraperResponse.text = response.content;
|
|
|
|
scraperResponse.metadata.pageStatusCode = response.pageStatusCode;
|
|
|
|
scraperResponse.metadata.pageError = response.pageError;
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|
|
|
|
break;
|
2024-04-16 12:06:46 -04:00
|
|
|
case "scrapingBeeLoad":
|
2024-04-15 17:01:47 -04:00
|
|
|
if (process.env.SCRAPING_BEE_API_KEY) {
|
2024-06-13 17:08:40 -03:00
|
|
|
const response = await scrapWithScrapingBee(url, "networkidle2");
|
|
|
|
scraperResponse.text = response.content;
|
|
|
|
scraperResponse.metadata.pageStatusCode = response.pageStatusCode;
|
|
|
|
scraperResponse.metadata.pageError = response.pageError;
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|
|
|
|
break;
|
2024-04-16 12:06:46 -04:00
|
|
|
case "fetch":
|
2024-06-13 17:08:40 -03:00
|
|
|
const response = await scrapWithFetch(url);
|
|
|
|
scraperResponse.text = response.content;
|
|
|
|
scraperResponse.metadata.pageStatusCode = response.pageStatusCode;
|
|
|
|
scraperResponse.metadata.pageError = response.pageError;
|
2024-04-15 17:01:47 -04:00
|
|
|
break;
|
|
|
|
}
|
2024-04-28 13:59:35 -07:00
|
|
|
|
2024-06-28 15:51:18 -03:00
|
|
|
let customScrapedContent: FireEngineResponse | null = null;
|
2024-06-04 12:15:39 -07:00
|
|
|
|
2024-05-29 13:39:43 -03:00
|
|
|
// Check for custom scraping conditions
|
2024-06-28 15:51:18 -03:00
|
|
|
const customScraperResult = await handleCustomScraping(
|
|
|
|
scraperResponse.text,
|
|
|
|
url
|
|
|
|
);
|
2024-06-04 12:15:39 -07:00
|
|
|
|
2024-06-28 15:51:18 -03:00
|
|
|
if (customScraperResult) {
|
2024-06-04 17:47:28 -03:00
|
|
|
switch (customScraperResult.scraper) {
|
|
|
|
case "fire-engine":
|
2024-06-28 15:51:18 -03:00
|
|
|
customScrapedContent = await scrapWithFireEngine({
|
|
|
|
url: customScraperResult.url,
|
|
|
|
waitFor: customScraperResult.waitAfterLoad,
|
|
|
|
screenshot: false,
|
|
|
|
pageOptions: customScraperResult.pageOptions,
|
|
|
|
});
|
2024-06-05 15:34:42 -03:00
|
|
|
if (screenshot) {
|
|
|
|
customScrapedContent.screenshot = screenshot;
|
|
|
|
}
|
2024-06-05 15:02:28 -03:00
|
|
|
break;
|
2024-06-04 17:47:28 -03:00
|
|
|
case "pdf":
|
2024-06-28 15:51:18 -03:00
|
|
|
const { content, pageStatusCode, pageError } =
|
|
|
|
await fetchAndProcessPdf(
|
|
|
|
customScraperResult.url,
|
|
|
|
pageOptions?.parsePDF
|
|
|
|
);
|
|
|
|
customScrapedContent = {
|
|
|
|
html: content,
|
|
|
|
screenshot,
|
|
|
|
pageStatusCode,
|
|
|
|
pageError,
|
|
|
|
};
|
2024-06-05 15:02:28 -03:00
|
|
|
break;
|
2024-06-04 17:47:28 -03:00
|
|
|
}
|
2024-06-04 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
2024-05-29 13:39:43 -03:00
|
|
|
if (customScrapedContent) {
|
2024-06-13 17:08:40 -03:00
|
|
|
scraperResponse.text = customScrapedContent.html;
|
2024-06-03 15:24:40 -03:00
|
|
|
screenshot = customScrapedContent.screenshot;
|
2024-05-29 13:39:43 -03:00
|
|
|
}
|
2024-05-09 17:45:16 -07:00
|
|
|
//* TODO: add an optional to return markdown or structured/extracted content
|
2024-06-13 17:08:40 -03:00
|
|
|
let cleanedHtml = removeUnwantedElements(scraperResponse.text, pageOptions);
|
|
|
|
return {
|
|
|
|
text: await parseMarkdown(cleanedHtml),
|
2024-06-18 16:26:54 -03:00
|
|
|
html: cleanedHtml,
|
2024-06-25 15:21:14 -03:00
|
|
|
rawHtml: scraperResponse.text,
|
2024-06-13 17:08:40 -03:00
|
|
|
screenshot: scraperResponse.screenshot,
|
|
|
|
pageStatusCode: scraperResponse.metadata.pageStatusCode,
|
2024-06-28 15:51:18 -03:00
|
|
|
pageError: scraperResponse.metadata.pageError || undefined,
|
2024-06-13 17:08:40 -03:00
|
|
|
};
|
2024-04-15 17:01:47 -04:00
|
|
|
};
|
2024-06-25 15:21:14 -03:00
|
|
|
|
2024-06-28 15:51:18 -03:00
|
|
|
let { text, html, rawHtml, screenshot, pageStatusCode, pageError } = {
|
|
|
|
text: "",
|
|
|
|
html: "",
|
|
|
|
rawHtml: "",
|
|
|
|
screenshot: "",
|
|
|
|
pageStatusCode: 200,
|
|
|
|
pageError: undefined,
|
|
|
|
};
|
2024-04-15 17:01:47 -04:00
|
|
|
try {
|
2024-05-09 17:45:16 -07:00
|
|
|
let urlKey = urlToScrap;
|
|
|
|
try {
|
|
|
|
urlKey = new URL(urlToScrap).hostname.replace(/^www\./, "");
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Invalid URL key, trying: ${urlToScrap}`);
|
2024-04-23 15:28:32 -07:00
|
|
|
}
|
2024-05-09 17:45:16 -07:00
|
|
|
const defaultScraper = urlSpecificParams[urlKey]?.defaultScraper ?? "";
|
2024-05-31 15:39:54 -07:00
|
|
|
const scrapersInOrder = getScrapingFallbackOrder(
|
|
|
|
defaultScraper,
|
|
|
|
pageOptions && pageOptions.waitFor && pageOptions.waitFor > 0,
|
|
|
|
pageOptions && pageOptions.screenshot && pageOptions.screenshot === true,
|
|
|
|
pageOptions && pageOptions.headers && pageOptions.headers !== undefined
|
|
|
|
);
|
2024-05-09 17:45:16 -07:00
|
|
|
|
|
|
|
for (const scraper of scrapersInOrder) {
|
2024-05-13 20:45:11 -07:00
|
|
|
// If exists text coming from crawler, use it
|
2024-05-15 11:28:20 -07:00
|
|
|
if (existingHtml && existingHtml.trim().length >= 100) {
|
|
|
|
let cleanedHtml = removeUnwantedElements(existingHtml, pageOptions);
|
|
|
|
text = await parseMarkdown(cleanedHtml);
|
2024-06-18 16:26:54 -03:00
|
|
|
html = cleanedHtml;
|
2024-05-13 20:45:11 -07:00
|
|
|
break;
|
|
|
|
}
|
2024-06-13 17:08:40 -03:00
|
|
|
|
|
|
|
const attempt = await attemptScraping(urlToScrap, scraper);
|
2024-06-28 15:51:18 -03:00
|
|
|
text = attempt.text ?? "";
|
|
|
|
html = attempt.html ?? "";
|
|
|
|
rawHtml = attempt.rawHtml ?? "";
|
|
|
|
screenshot = attempt.screenshot ?? "";
|
2024-07-03 18:01:17 -03:00
|
|
|
|
2024-06-14 09:46:55 -03:00
|
|
|
if (attempt.pageStatusCode) {
|
|
|
|
pageStatusCode = attempt.pageStatusCode;
|
|
|
|
}
|
2024-07-02 10:51:35 -03:00
|
|
|
if (attempt.pageError && attempt.pageStatusCode >= 400) {
|
2024-06-14 09:46:55 -03:00
|
|
|
pageError = attempt.pageError;
|
2024-07-03 18:38:17 -03:00
|
|
|
} else if (attempt && attempt.pageStatusCode && attempt.pageStatusCode < 400) {
|
2024-07-01 18:21:15 -03:00
|
|
|
pageError = undefined;
|
2024-06-14 09:46:55 -03:00
|
|
|
}
|
2024-06-28 15:51:18 -03:00
|
|
|
|
2024-05-13 20:45:11 -07:00
|
|
|
if (text && text.trim().length >= 100) break;
|
2024-06-14 09:46:55 -03:00
|
|
|
if (pageStatusCode && pageStatusCode == 404) break;
|
2024-05-21 18:34:23 -07:00
|
|
|
const nextScraperIndex = scrapersInOrder.indexOf(scraper) + 1;
|
|
|
|
if (nextScraperIndex < scrapersInOrder.length) {
|
|
|
|
console.info(`Falling back to ${scrapersInOrder[nextScraperIndex]}`);
|
|
|
|
}
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|
|
|
|
|
2024-05-09 17:52:46 -07:00
|
|
|
if (!text) {
|
2024-05-09 17:45:16 -07:00
|
|
|
throw new Error(`All scraping methods failed for URL: ${urlToScrap}`);
|
2024-04-15 17:01:47 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 15:21:14 -03:00
|
|
|
const soup = cheerio.load(rawHtml);
|
2024-04-15 17:01:47 -04:00
|
|
|
const metadata = extractMetadata(soup, urlToScrap);
|
2024-05-29 18:56:57 -04:00
|
|
|
|
2024-07-16 18:38:03 -07:00
|
|
|
let linksOnPage: string[] | undefined;
|
|
|
|
|
|
|
|
linksOnPage = extractLinks(rawHtml, urlToScrap);
|
|
|
|
|
2024-05-29 18:56:57 -04:00
|
|
|
let document: Document;
|
2024-05-31 15:39:54 -07:00
|
|
|
if (screenshot && screenshot.length > 0) {
|
2024-05-29 18:56:57 -04:00
|
|
|
document = {
|
|
|
|
content: text,
|
|
|
|
markdown: text,
|
|
|
|
html: pageOptions.includeHtml ? html : undefined,
|
2024-07-03 18:01:17 -03:00
|
|
|
rawHtml:
|
|
|
|
pageOptions.includeRawHtml ||
|
2024-07-16 18:38:03 -07:00
|
|
|
extractorOptions.mode === "llm-extraction-from-raw-html"
|
2024-07-03 18:01:17 -03:00
|
|
|
? rawHtml
|
|
|
|
: undefined,
|
2024-07-16 18:38:03 -07:00
|
|
|
linksOnPage,
|
2024-05-31 15:39:54 -07:00
|
|
|
metadata: {
|
|
|
|
...metadata,
|
|
|
|
screenshot: screenshot,
|
|
|
|
sourceURL: urlToScrap,
|
2024-06-13 17:08:40 -03:00
|
|
|
pageStatusCode: pageStatusCode,
|
2024-06-28 15:51:18 -03:00
|
|
|
pageError: pageError,
|
2024-05-31 15:39:54 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
2024-05-29 18:56:57 -04:00
|
|
|
document = {
|
|
|
|
content: text,
|
|
|
|
markdown: text,
|
|
|
|
html: pageOptions.includeHtml ? html : undefined,
|
2024-07-03 18:01:17 -03:00
|
|
|
rawHtml:
|
|
|
|
pageOptions.includeRawHtml ||
|
2024-07-16 18:38:03 -07:00
|
|
|
extractorOptions.mode === "llm-extraction-from-raw-html"
|
2024-07-03 18:01:17 -03:00
|
|
|
? rawHtml
|
|
|
|
: undefined,
|
2024-06-13 17:08:40 -03:00
|
|
|
metadata: {
|
|
|
|
...metadata,
|
|
|
|
sourceURL: urlToScrap,
|
|
|
|
pageStatusCode: pageStatusCode,
|
2024-06-28 15:51:18 -03:00
|
|
|
pageError: pageError,
|
2024-06-13 17:08:40 -03:00
|
|
|
},
|
2024-07-16 18:38:03 -07:00
|
|
|
linksOnPage,
|
2024-05-31 15:39:54 -07:00
|
|
|
};
|
2024-05-29 18:56:57 -04:00
|
|
|
}
|
2024-05-09 17:45:16 -07:00
|
|
|
|
|
|
|
return document;
|
2024-04-15 17:01:47 -04:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error: ${error} - Failed to fetch URL: ${urlToScrap}`);
|
|
|
|
return {
|
|
|
|
content: "",
|
2024-05-06 19:45:56 -03:00
|
|
|
markdown: "",
|
|
|
|
html: "",
|
2024-07-16 18:38:03 -07:00
|
|
|
linksOnPage: [],
|
2024-06-14 09:46:55 -03:00
|
|
|
metadata: {
|
|
|
|
sourceURL: urlToScrap,
|
|
|
|
pageStatusCode: pageStatusCode,
|
2024-06-28 15:51:18 -03:00
|
|
|
pageError: pageError,
|
2024-06-14 09:46:55 -03:00
|
|
|
},
|
2024-04-15 17:01:47 -04:00
|
|
|
} as Document;
|
|
|
|
}
|
|
|
|
}
|