mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
modify puppeteer web crawl
This commit is contained in:
@@ -2,7 +2,7 @@ import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
|||||||
import { TextSplitter } from 'langchain/text_splitter'
|
import { TextSplitter } from 'langchain/text_splitter'
|
||||||
import { PuppeteerWebBaseLoader } from 'langchain/document_loaders/web/puppeteer'
|
import { PuppeteerWebBaseLoader } from 'langchain/document_loaders/web/puppeteer'
|
||||||
import { test } from 'linkifyjs'
|
import { test } from 'linkifyjs'
|
||||||
import { getAvailableURLs } from '../../../src'
|
import { webCrawl } from '../../../src'
|
||||||
|
|
||||||
class Puppeteer_DocumentLoaders implements INode {
|
class Puppeteer_DocumentLoaders implements INode {
|
||||||
label: string
|
label: string
|
||||||
@@ -35,19 +35,20 @@ class Puppeteer_DocumentLoaders implements INode {
|
|||||||
optional: true
|
optional: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Web Scrape for Relative Links',
|
label: 'Web Crawl for Relative Links',
|
||||||
name: 'webScrape',
|
name: 'boolWebCrawl',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
optional: true,
|
optional: true,
|
||||||
additionalParams: true
|
additionalParams: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Web Scrape Links Limit',
|
label: 'Web Crawl Links Limit',
|
||||||
name: 'limit',
|
name: 'limit',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
default: 10,
|
default: 10,
|
||||||
optional: true,
|
optional: true,
|
||||||
additionalParams: true
|
additionalParams: true,
|
||||||
|
description: 'Set 0 to crawl all relative links'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Metadata',
|
label: 'Metadata',
|
||||||
@@ -62,7 +63,7 @@ class Puppeteer_DocumentLoaders implements INode {
|
|||||||
async init(nodeData: INodeData): Promise<any> {
|
async init(nodeData: INodeData): Promise<any> {
|
||||||
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
|
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
|
||||||
const metadata = nodeData.inputs?.metadata
|
const metadata = nodeData.inputs?.metadata
|
||||||
const webScrape = nodeData.inputs?.webScrape as boolean
|
const boolWebCrawl = nodeData.inputs?.boolWebCrawl as boolean
|
||||||
let limit = nodeData.inputs?.limit as string
|
let limit = nodeData.inputs?.limit as string
|
||||||
|
|
||||||
let url = nodeData.inputs?.url as string
|
let url = nodeData.inputs?.url as string
|
||||||
@@ -71,30 +72,32 @@ class Puppeteer_DocumentLoaders implements INode {
|
|||||||
throw new Error('Invalid URL')
|
throw new Error('Invalid URL')
|
||||||
}
|
}
|
||||||
|
|
||||||
const puppeteerLoader = async (url: string): Promise<any> => {
|
async function puppeteerLoader(url: string): Promise<any> {
|
||||||
let docs = []
|
try {
|
||||||
const loader = new PuppeteerWebBaseLoader(url)
|
let docs = []
|
||||||
if (textSplitter) {
|
const loader = new PuppeteerWebBaseLoader(url)
|
||||||
docs = await loader.loadAndSplit(textSplitter)
|
if (textSplitter) {
|
||||||
} else {
|
docs = await loader.loadAndSplit(textSplitter)
|
||||||
docs = await loader.load()
|
} else {
|
||||||
|
docs = await loader.load()
|
||||||
|
}
|
||||||
|
return docs
|
||||||
|
} catch (err) {
|
||||||
|
if (process.env.DEBUG === 'true') console.error(`error in CheerioWebBaseLoader: ${err.message}, on page: ${url}`)
|
||||||
}
|
}
|
||||||
return docs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let availableUrls: string[]
|
|
||||||
let docs = []
|
let docs = []
|
||||||
if (webScrape) {
|
if (boolWebCrawl) {
|
||||||
if (!limit) limit = '10'
|
if (process.env.DEBUG === 'true') console.info('Start Web Crawl')
|
||||||
availableUrls = await getAvailableURLs(url, parseInt(limit))
|
if (!limit) throw new Error('Please set a limit to crawl')
|
||||||
for (let i = 0; i < availableUrls.length; i++) {
|
else if (parseInt(limit) < 0) throw new Error('Limit cannot be less than 0')
|
||||||
try {
|
const pages: string[] = await webCrawl(url, parseInt(limit))
|
||||||
docs.push(...(await puppeteerLoader(availableUrls[i])))
|
if (process.env.DEBUG === 'true') console.info(`pages: ${JSON.stringify(pages)}, length: ${pages.length}`)
|
||||||
} catch (error) {
|
for (const page of pages) {
|
||||||
console.error('Error loading url with puppeteer. URL: ', availableUrls[i], 'Error: ', error)
|
docs.push(...(await puppeteerLoader(page)))
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (process.env.DEBUG === 'true') console.info('Finish Web Crawl')
|
||||||
} else {
|
} else {
|
||||||
docs = await puppeteerLoader(url)
|
docs = await puppeteerLoader(url)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ export async function crawl(baseURL: string, currentURL: string, pages: string[]
|
|||||||
const baseURLObj = new URL(baseURL)
|
const baseURLObj = new URL(baseURL)
|
||||||
const currentURLObj = new URL(currentURL)
|
const currentURLObj = new URL(currentURL)
|
||||||
|
|
||||||
if (limit !== 0) if (pages.length === limit) return pages
|
if (limit !== 0 && pages.length === limit) return pages
|
||||||
|
|
||||||
if (baseURLObj.hostname !== currentURLObj.hostname) return pages
|
if (baseURLObj.hostname !== currentURLObj.hostname) return pages
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user