UNPKG

raw-google-geocoder

Version:

raw google geocode any address without an api or key

63 lines (57 loc) 2.13 kB
// Generated by CoffeeScript 2.2.2 var enc, parse, serialize; enc = require('urlencode'); // serialize address as a query serialize = function(address, use_https) { var protocol, query, url; query = enc(address); if (use_https) { protocol = 'https'; } else { protocol = 'http'; } url = `https://www.google.com/search?tbm=map&q=${query}&tch=0`; return { method: 'get', url: url, gzip: true, encoding: 'utf8', headers: { 'x-chrome-uma-enabled': 0, //do not report anything to google, the google AI will analyze you if this is set to 1. 'accept': '*/*', 'referer': 'https://www.google.com/', //pretend we are google //google AI monitors all requests that dont match a typical user profile and will block you if you dont pretend to be a human, so make sure your agent is always spoofed! 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36' } }; }; // parse the google response body and try and choose the right address. parse = function(body) { var addr, addr1, addr2, e, i, lat, len, lon, match, pickone; addr = lat = lon = addr1 = addr2 = null; try { [lat, lon] = body.match(/null,((?:\-|\+)?\d?\d?\d\.\d+\,(?:\-|\+)?\d\d?\d?.\d+)]/)[1].split(','); lat = Number(lat); lon = Number(lon); } catch (error) { e = error; throw new Error('RawGGeocoderError: could not parse lat/lon ' + e.message); } try { match = body.match(/,\\"([A-Za-z\u00C0-\u00FF\u2000-\u206F\u2E00-\u2E7F#\-,. \d]+, [A-Za-z\u00C0-\u00FF\u2000-\u206F\u2E00-\u2E7F#,. \-\d]+)\\"/g); pickone = null; for (i = 0, len = match.length; i < len; i++) { addr = match[i]; if (!pickone || addr.length > pickone.length) { pickone = addr; } } addr = pickone.match(/([A-Za-z\u00C0-\u00FF\u2000-\u206F\u2E00-\u2E7F#\-,. \d]+)/g)[1]; } catch (error) { e = error; throw new Error('RawGGeocoderError: could not parse address'); } return {addr, lat, lon}; }; module.exports.parse = parse; module.exports.serialize = serialize;