open-graph-scraper
Version:
Node.js scraper module for Open Graph and Twitter Card info
53 lines (52 loc) • 2.24 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
/* eslint-disable import/no-import-module-exports */
const openGraphScraper_1 = __importDefault(require("./lib/openGraphScraper"));
/**
* `open-graph-scraper` uses [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for http requests
* for scraping Open Graph and Twitter Card info off a website.
*
* @param {object} options - The options used by Open Graph Scraper
* @param {boolean|string[]} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on
* anything else.
* @param {object} [options.customMetaTags] - Here you can define custom meta tags you want to scrape.
* @param {object} [options.fetchOptions] - Sets the options used by fetch for the http requests
* @param {object} [options.urlValidatorSettings] - Sets the options used by validator.js for testing the URL
* @param {string[]} [options.blacklist] - Pass in an array of sites you don't want ogs to run on.
* @param {string} [options.html] - You can pass in an HTML string to run ogs on it. (use without options.url)
* @param {number} [options.timeout] - Number of seconds before the fetch request ends. (default is 10 seconds)
* @param {string} options.url - URL of the site. (Required)
* @returns {Promise} Promise Object with the Open Graph results
*/
async function run(options) {
let results;
try {
results = await (0, openGraphScraper_1.default)(options);
}
catch (error) {
const exception = error;
const returnError = {
error: true,
result: {
success: false,
requestUrl: options.url,
error: exception.message,
errorDetails: exception,
},
response: undefined,
html: undefined,
};
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw returnError;
}
const returnSuccess = {
error: false,
result: results.ogObject,
response: results.response,
html: results.html,
};
return returnSuccess;
}
module.exports = run;
;