Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function retrieveArticleData(article) {
const prepareArticleSpinner = ora('Cargando artículo...');
// Get article data from Mercury
try {
prepareArticleSpinner.start();
const articleData = await Mercury.parse(article);
logger('Article Data: %O', articleData);
prepareArticleSpinner.succeed();
return articleData;
} catch (err) {
prepareArticleSpinner.fail(err.message);
process.exit(1);
}
}
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
// if parser failed, return default description and not report error
try {
const res = await got(link);
const $ = cheerio.load(res.data);
const result = await mercury_parser.parse(link, {
html: $.html(),
});
return result;
} catch (e) {
// no-empty
}
});
app.get('/api/parse/:url', (req, res) => {
const Mercury = require('@postlight/mercury-parser')
const url = decodeURIComponent(req.params.url)
Mercury.parse(url)
.then(result => {
return res.json(result)
})
.catch(err => {
console.log(`Error parsing article at ${url}:`, err)
return res.status(500).json({ error: 'Error parsing article' })
})
})
#!/usr/bin/env node
process.env.UV_THREADPOOL_SIZE = 128;
const Mercury = require('@postlight/mercury-parser');
url = process.argv[2];
Mercury.parse(url).then((result) => {console.log(result)},(error) => {console.log("{'error': 'error'}")});
new validator(user, url, signature).validate().then(result => {
mercury.parse(url).then(result => {
const code = ("error" in result ? 400 : 200);
response.status(code).send(result);
}).catch(function(error) {
response.status(400).json({ error: true, messages: "Cannot extract this URL." });
});
}).catch(function(error) {
response.status(400).json({ error: true, messages: error });
export async function ParseContent(url) {
return await Mercury.parse(url);
}
const parseHtml = async ({ body }, context, cb) => {
const { url, html } = JSON.parse(body);
const result = await Mercury.parse(url, { html });
return cb(
null,
result
? corsSuccessResponse(result)
: corsErrorResponse({ message: 'There was an error parsing that URL.' })
);
};
async function parse(url: string, html: string) {
const result = Mercury.parse(url, {
html,
contentType: 'text',
fetchAllPages: false,
}) as MercuryResult
if (!result.content) {
return left(new Error('Parse error'))
}
return right(result)
}
export async function parseArticle (url) {
const result = await Mercury.parse(url)
return result
}
const parse = async (userUrl, paramsObj) => {
const parseHelper = new ParseHelper(userUrl, paramsObj);
userUrl = parseHelper.link;
const opts = {};
if (parseHelper.custom) {
const html = await parseHelper.fetchHtml();
logger(html, 'fixedFetched.html');
opts.html = Buffer.from(html);
}
const extractor = parseHelper.getExtractor();
if (extractor) {
logger(extractor);
Mercury.addExtractor(extractor);
}
let result = await mercury(userUrl, opts);
logger(result.content, 'mercury.html');
let { content } = result;
const preContent = sanitizeHtml(content).trim();
logger(preContent, 'preContent.html');
if (preContent.length === 0) {
const html = await parseHelper.puppet(userUrl);
if (html) {
logger(html, 'asyncContent.html');
result = await mercury(userUrl, { html: Buffer.from(html) });
logger(result.content, 'mercuryAsyncContent.html');
}
}
let { title = '', url: source, iframe } = result;
logger(iframe, 'iframes.html');