Skip to content

Instantly share code, notes, and snippets.

@auryn31
Last active September 25, 2019 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auryn31/30c970952f048934757feb54390a7a88 to your computer and use it in GitHub Desktop.
Save auryn31/30c970952f048934757feb54390a7a88 to your computer and use it in GitHub Desktop.
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