const request = require('request'); const logger = require('log4js').getLogger('Directions'); logger.level = 'debug'; const { reduceEstDirections } = require('./reducers/directions'); module.exports = { 'getTraffic': doGetEstDirections }; // // https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=AIzaSyBl7O9LHIthCagcqIaDkQ4um_hghYG5reE function doGetEstDirections(olat, olon, dlat, dlon) { logger.debug('doGetEstDirections'); const url = `https://sgws2.maps.yahoo.com/Directions?time=now&cache=n&flags=J&olat=${olat}&olon=${olon}&dlat=${dlat}&dlon=${dlon}`; logger.debug(url); return new Promise(function(resolve, reject) { request(url, function(err, resp, body) { if (err) // Logger.error(err); return reject(err); // Throw err; const output = reduceEstDirections(body); output.fullBody = JSON.parse(body); output.timestamp = new Date().getTime(); console.log(output); return resolve(output); }, function(error, response, body) { console.log(response); if (response.statusCode !== 200) { logger.error(response.statusCode); logger.error(body); return reject(error); } }); }); }