Last active
September 25, 2019 19:15
-
-
Save auryn31/30c970952f048934757feb54390a7a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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