Files
firecrawl/apps/api/src/scraper/WebScraper/sitemap.ts
T

127 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-04-15 17:01:47 -04:00
import axios from "axios";
2024-06-24 16:33:07 -03:00
import { axiosTimeout } from "../../lib/timeout";
2024-04-15 17:01:47 -04:00
import { parseStringPromise } from "xml2js";
import { WebCrawler } from "./crawler";
2024-11-07 20:57:33 +01:00
import { scrapeURL } from "../scrapeURL";
import { scrapeOptions } from "../../controllers/v1/types";
import type { Logger } from "winston";
2024-04-15 17:01:47 -04:00
export async function getLinksFromSitemap(
{
sitemapUrl,
allUrls = [],
2024-12-11 19:46:11 -03:00
mode = "axios"
}: {
2024-12-11 19:46:11 -03:00
sitemapUrl: string;
allUrls?: string[];
mode?: "axios" | "fire-engine";
},
2024-12-11 19:46:11 -03:00
logger: Logger
2024-04-15 17:01:47 -04:00
): Promise<string[]> {
try {
2024-11-07 20:57:33 +01:00
let content: string = "";
2024-04-15 17:01:47 -04:00
try {
2024-12-11 19:46:11 -03:00
if (mode === "axios" || process.env.FIRE_ENGINE_BETA_URL === "") {
const response = await axios.get(sitemapUrl, { timeout: axiosTimeout });
content = response.data;
2024-12-11 19:46:11 -03:00
} else if (mode === "fire-engine") {
const response = await scrapeURL(
"sitemap",
sitemapUrl,
scrapeOptions.parse({ formats: ["rawHtml"] }),
{ forceEngine: "fire-engine;tlsclient", v0DisableJsDom: true }
);
2024-11-07 20:57:33 +01:00
if (!response.success) {
throw response.error;
}
content = response.document.rawHtml!;
}
2024-04-15 17:01:47 -04:00
} catch (error) {
2024-12-11 19:46:11 -03:00
logger.error(`Request failed for ${sitemapUrl}`, {
method: "getLinksFromSitemap",
mode,
sitemapUrl,
error
});
2024-06-10 16:27:10 -07:00
2024-04-15 17:01:47 -04:00
return allUrls;
}
const parsed = await parseStringPromise(content);
const root = parsed.urlset || parsed.sitemapindex;
if (root && root.sitemap) {
2024-09-05 17:52:27 -03:00
const sitemapPromises = root.sitemap
2024-12-11 19:46:11 -03:00
.filter((sitemap) => sitemap.loc && sitemap.loc.length > 0)
.map((sitemap) =>
getLinksFromSitemap(
{ sitemapUrl: sitemap.loc[0], allUrls, mode },
logger
)
);
2024-09-05 17:52:27 -03:00
await Promise.all(sitemapPromises);
2024-04-15 17:01:47 -04:00
} else if (root && root.url) {
2024-09-05 17:52:27 -03:00
const validUrls = root.url
2024-12-11 19:46:11 -03:00
.filter(
(url) =>
url.loc &&
url.loc.length > 0 &&
!WebCrawler.prototype.isFile(url.loc[0])
)
.map((url) => url.loc[0]);
2024-09-05 17:52:27 -03:00
allUrls.push(...validUrls);
2024-04-15 17:01:47 -04:00
}
} catch (error) {
2024-12-11 19:46:11 -03:00
logger.debug(`Error processing sitemapUrl: ${sitemapUrl}`, {
method: "getLinksFromSitemap",
mode,
sitemapUrl,
error
});
2024-04-15 17:01:47 -04:00
}
return allUrls;
}
2024-12-11 19:46:11 -03:00
export const fetchSitemapData = async (
url: string,
timeout?: number
): Promise<SitemapEntry[] | null> => {
2024-04-15 17:01:47 -04:00
const sitemapUrl = url.endsWith("/sitemap.xml") ? url : `${url}/sitemap.xml`;
try {
2024-12-11 19:46:11 -03:00
const response = await axios.get(sitemapUrl, {
timeout: timeout || axiosTimeout
});
2024-04-15 17:01:47 -04:00
if (response.status === 200) {
const xml = response.data;
const parsedXml = await parseStringPromise(xml);
const sitemapData: SitemapEntry[] = [];
if (parsedXml.urlset && parsedXml.urlset.url) {
for (const urlElement of parsedXml.urlset.url) {
const sitemapEntry: SitemapEntry = { loc: urlElement.loc[0] };
if (urlElement.lastmod) sitemapEntry.lastmod = urlElement.lastmod[0];
2024-12-11 19:46:11 -03:00
if (urlElement.changefreq)
sitemapEntry.changefreq = urlElement.changefreq[0];
if (urlElement.priority)
sitemapEntry.priority = Number(urlElement.priority[0]);
2024-04-15 17:01:47 -04:00
sitemapData.push(sitemapEntry);
}
}
return sitemapData;
}
return null;
} catch (error) {
// Error handling for failed sitemap fetch
}
return [];
2024-12-11 19:46:11 -03:00
};
2024-04-15 17:01:47 -04:00
export interface SitemapEntry {
loc: string;
lastmod?: string;
changefreq?: string;
priority?: number;
2024-12-11 19:46:11 -03:00
}