From a0b4bc7879c6c370c2f1c68144ca88642ae7cb16 Mon Sep 17 00:00:00 2001 From: MirnaM Date: Mon, 6 May 2019 15:13:13 +0200 Subject: [PATCH] Scrape lat lng from olx --- app/helpers/scraping.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/helpers/scraping.js diff --git a/app/helpers/scraping.js b/app/helpers/scraping.js new file mode 100644 index 0000000..6ae60cc --- /dev/null +++ b/app/helpers/scraping.js @@ -0,0 +1,27 @@ +let fetch = require("node-fetch"); + +const getRealEstateGeolocation = async (url) => { + let response = await fetch(url); + const body = await response.text(); + let lat, long; + const googleMapRegex = new RegExp(/google.maps.LatLng\((.*?)\)/g); + const googleMapString = body.match(googleMapRegex); + if (googleMapString && googleMapString.length) { + const latLongRegex = new RegExp(/\((.*?)\)/g); + let latLongString = googleMapString[0].match(latLongRegex); + if (latLongString && latLongString.length) { + latLongString = latLongString[0].trim(); + latLongString = latLongString.substr(1, latLongString.length - 2); + const latLongArray = latLongString.split(","); + if (latLongArray.length) { + lat = latLongArray[0]; + long = latLongArray[1]; + } + } + } + return { lat, long }; +} + +module.exports = { + getRealEstateGeolocation +}; -- 2.47.3