Files
fredy/lib/services/extractor/extractor.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-01-07 12:25:19 +01:00
import { setDebug } from './utils.js';
import puppeteerExtractor from './puppeteerExtractor.js';
2025-01-07 12:25:19 +01:00
import { loadParser, parse } from './parser/parser.js';
2025-09-13 18:57:56 +02:00
import logger from '../logger.js';
const DEFAULT_OPTIONS = {
2025-01-07 12:25:19 +01:00
debug: false,
puppeteerTimeout: 60_000,
puppeteerHeadless: true,
};
export default class Extractor {
2025-01-07 12:25:19 +01:00
constructor(options) {
this.options = {
...DEFAULT_OPTIONS,
...options,
};
2025-01-07 12:25:19 +01:00
this.responseText = null;
setDebug(this.options);
}
2025-01-07 12:25:19 +01:00
/**
* if you are extracting data from a SPA, you must provide a selector, otherwise
* your response will never contain what you are really looking for
* @param url
* @param waitForSelector
*/
execute = async (url, waitForSelector = null) => {
this.responseText = null;
try {
this.responseText = await puppeteerExtractor(url, waitForSelector, this.options);
if (this.responseText != null) {
loadParser(this.responseText);
}
} catch (error) {
2025-09-13 18:57:56 +02:00
logger.error('Error trying to load page.', error);
2025-01-07 12:25:19 +01:00
}
return this;
};
2025-01-07 12:25:19 +01:00
parseResponseText = (crawlContainer, crawlFields, url) => {
return parse(crawlContainer, crawlFields, this.responseText, url);
};
}