Skip to content

Instantly share code, notes, and snippets.

@auryn31
Last active Sep 25, 2019
Embed
What would you like to do?
const axios = require('axios').default;
const JSDOM = require('jsdom').JSDOM;
module.exports.price = async event => {
if(event.queryStringParameters == undefined || event.queryStringParameters.url == undefined) {
return {
statusCode: 404,
body: JSON.stringify({
message: "You forgot the URL parameter!"
})
}
}
const url = event.queryStringParameters.url
var encoded_url = encodeURI(url);
const res = await axios.get(encoded_url);
const document = new JSDOM(res.data).window.document;
const title_with_whitespace = document.querySelector("#productTitle").textContent;
const title = title_with_whitespace.replace(/(?:\r\n|\r|\n)/g, '').trim();
const price_with_ending = document.querySelector("#priceblock_ourprice").textContent;
const price = price_with_ending.substring(0, price_with_ending.length - 2).replace(',', '.');
return {
statusCode: res.status,
body: JSON.stringify({
title: title,
price: price
})
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment