add web crawl

This commit is contained in:
chungyau97
2023-07-05 16:47:01 +08:00
parent 7608d74676
commit 92e50a676c
3 changed files with 113 additions and 20 deletions
+84
View File
@@ -2,6 +2,7 @@ import axios from 'axios'
import { load } from 'cheerio'
import * as fs from 'fs'
import * as path from 'path'
import { JSDOM } from 'jsdom'
import { BaseCallbackHandler } from 'langchain/callbacks'
import { Server } from 'socket.io'
import { ChainValues } from 'langchain/dist/schema'
@@ -201,6 +202,89 @@ export const getAvailableURLs = async (url: string, limit: number) => {
}
}
function getURLsFromHTML(htmlBody: string, baseURL: string): string[] {
const dom = new JSDOM(htmlBody)
const linkElements = dom.window.document.querySelectorAll('a')
const urls: string[] = []
for (const linkElement of linkElements) {
if (linkElement.href.slice(0, 1) === '/') {
try {
const urlObj = new URL(baseURL + linkElement.href)
urls.push(urlObj.href) //relative
} catch (err) {
if (process.env.DEBUG === 'true') console.error(`error with relative url: ${err.message}`)
continue
}
} else {
try {
const urlObj = new URL(linkElement.href)
urls.push(urlObj.href) //absolute
} catch (err) {
if (process.env.DEBUG === 'true') console.error(`error with absolute url: ${err.message}`)
continue
}
}
}
return urls
}
function normalizeURL(urlString: string): string {
const urlObj = new URL(urlString)
const hostPath = urlObj.hostname + urlObj.pathname
if (hostPath.length > 0 && hostPath.slice(-1) == '/') {
// handling trailing slash
return hostPath.slice(0, -1)
}
return hostPath
}
export async function crawl(baseURL: string, currentURL: string, pages: string[], limit: number): Promise<string[]> {
const baseURLObj = new URL(baseURL)
const currentURLObj = new URL(currentURL)
if (limit !== 0) if (pages.length === limit) return pages
if (baseURLObj.hostname !== currentURLObj.hostname) return pages
const normalizeCurrentURL = baseURLObj.protocol + '//' + normalizeURL(currentURL)
if (pages.includes(normalizeCurrentURL)) {
return pages
}
pages.push(normalizeCurrentURL)
if (process.env.DEBUG === 'true') console.info(`actively crawling ${currentURL}`)
try {
const resp = await fetch(currentURL)
if (resp.status > 399) {
if (process.env.DEBUG === 'true') console.error(`error in fetch with status code: ${resp.status}, on page: ${currentURL}`)
return pages
}
const contentType: string | null = resp.headers.get('content-type')
if ((contentType && !contentType.includes('text/html')) || !contentType) {
if (process.env.DEBUG === 'true') console.error(`non html response, content type: ${contentType}, on page: ${currentURL}`)
return pages
}
const htmlBody = await resp.text()
const nextURLs = getURLsFromHTML(htmlBody, baseURL)
for (const nextURL of nextURLs) {
pages = await crawl(baseURL, nextURL, pages, limit)
}
} catch (err) {
if (process.env.DEBUG === 'true') console.error(`error in fetch url: ${err.message}, on page: ${currentURL}`)
}
return pages
}
export async function webCrawl(stringURL: string, limit: number): Promise<string[]> {
const URLObj = new URL(stringURL)
const modifyURL = stringURL.slice(-1) === '/' ? stringURL.slice(0, -1) : stringURL
return await crawl(URLObj.protocol + '//' + URLObj.hostname, modifyURL, [], limit)
}
/**
* Custom chain handler class
*/