Last active
November 2, 2016 19:39
-
-
Save siberex/8427e3a41148330fcb74dfad9d3d9802 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
"use strict"; | |
// node --harmony test-googleapi-async.js | |
const rp = require('request-promise'); | |
const util = require('util'); | |
const https = require('https'); | |
// https://developers.google.com/maps/documentation/timezone/start#get-a-key | |
let apiKey = 'AIzaSyCFljyx_4s3t6ldM5alxXPqcD7P9X6TYUM'; | |
let coords = [[-22.681019,14.522226],[2.5724996,-27.8163698],[4.557567,4.6163235],[4.4254036,8.790334],[0.344051,9.4742473],[0.3834557,9.4475747],[-20.4457012,57.7176283],[0.3757385,9.4549369],[3.0552875716039662,9.964265981535153],[0.3546091,9.4734645],[89.9999999999996,0],[0.4093177,9.4320854],[0.470328,9.398915],[0.391981,9.4431912],[-1.1482112,8.6987966],[7.3679758,-24.2785494],[11.0904651,-28.864397],[0.40260293,9.43583089],[4.2393411,8.405862],[-22.008171379566193,35.3210552316159],[0.3718994,9.4592005],[6.3413884,3.3973382],[4.0709123,9.7037695],[4.9128139,-1.737505],[-20.174795,57.770134],[0.3711196,9.4582866],[0.3836682,9.4472672],[26.2327904,50.5381572],[-20.17276879,57.77063021],[-20.0052654,57.5698832],[4.443256666666667,8.027361666666668],[-20.28077,57.36534333333334],[41.40668487548828,2.2636919021606445],[-8.8092976,13.2176158],[5.9280853,4.89449403],[36.8870158,30.6961332],[-14.5560979,40.6389315],[0.4547487,9.4103139],[4.508393333333333,5.731486666666666],[0.4078287,9.4343803],[4.2394848,8.4058434],[-3.6318184,39.8528284],[0.3891227,9.4469423],[4.0224434,9.674068],[-20.161862,57.48276],[-25.661147,-3.5760599],[-20.194346734117662,57.77485045873086],[4.0452856,9.6853579],[4.0242022,9.6716989],[14.6703958,-17.4291602],[0.41337556,9.43007892],[38.6658891,-13.4166882],[-0.7155480486466558,8.785899534354341],[-6.8008208,39.2749647],[4.0278608,9.6690458],[34.7044382,11.2019092],[34.6989665,11.1837711],[4.0233697,9.6738892],[8.2671178,52.6161306],[14.6937045,-17.431749],[28.100398,-15.3330299],[0.3921684,9.4436481],[0.3852091,9.4466032],[0.4086274,9.4340278],[4.2393934,8.4058525],[4.0691126,9.6970186],[-20.149807,57.7452947],[0.373739,9.4585504],[4.0128018,9.1371751],[0.3891284,9.4441872],[-6.808211,39.274361],[-5.2457709,39.7676054],[6.3467699,3.3495372],[25.2553837,55.3005037],[5.3794716666666655,5.315219999999999],[4.0548401,9.663435],[11.546972,-28.2245931],[0.4503322,9.4119827],[4.059188,9.6730819],[4.6633832,8.5155406],[0.3993948,9.4409172],[-12.3401546,13.5603234],[26.22901,50.5444221],[4.5514947,8.4970228],[21.4848376,39.1613988],[24.4882985,54.4082796],[5.009851666666666,-1.6291966666666666],[33.2696554,35.2008451],[-4.073196666666666,39.66549166666667],[24.9847396,55.0207251],[-6.4107884,39.5731029],[-6.805693333333333,39.27234333333333],[-6.803351666666667,39.271715],[-6.0596716,12.2016703],[-4.0471078,39.6410887],[-6.8673871,39.2997569],[16.0418411,-16.5069434],[6.34557968,3.40957257],[0.3360811,9.4815019],[-4.03753,39.6797424],[-6.8067694,39.2752028],[8.4593067,-13.2780257],[-4.0421369,39.6178924],[6.1814557,-16.1641515],[-3.9529978,39.7474184],[40.5726049,-73.9970661],[-22.95328833333333,14.481209999999999],[5.4114908,5.3115299],[-4.0418166,39.6378197],[-6.8139711,39.2675653],[14.68594028,-17.43241637],[-20.0011347,57.662519],[26.227989,50.5434412],[4.5514889,8.4970345],[4.5514889,8.4970345],[23.688982268817433,58.16882835706525],[26.2146536,50.6053513],[24.1766799,52.5748924],[8.4930552,-13.2744074],[-4.0798705,39.6637405],[41.31028412,-72.0987622],[52.5444439,5.8562449],[59.3677767,18.0519377],[-6.80164494,39.28008441],[-4.0498288,39.6480635],[-4.0567674,39.6476442],[5.88176,4.8210659],[-6.802338,39.2766027],[-4.0414436,39.6357391],[23.8877406,58.7857982],[-5.3583123,39.6476788],[24.4423779,54.4260018],[-6.816799,39.2931032],[-22.9640454,14.4870015],[26.2309582,50.6533526],[-4.0744762,39.6668544],[-3.9523789,39.7429926],[33.673452,-78.866932],[-22.9667882,14.4863482],[-9.9724208,39.7082453],[-6.8198238,39.2969495],[2.4566998,-32.2471149],[4.9394168,-1.7050851],[-33.0181663,27.9155551],[4.89297,-1.7467916666666667],[25.485429999999997,-80.33752333333334],[30.6248865,-87.0316136],[5.0228111,118.3306736],[27.7738656,-97.3943785],[40.682154,-74.030346],[26.2173601,50.6025563],[29.2647605,-89.9564343],[27.96322246,-89.04858805],[29.3035231,-94.8193417],[30.5172703,-87.1727836],[45.0775226,-83.4741029],[30.27374171591796,-85.64833899467668],[43.6018807,-83.8811475],[-6.816315,39.2937071],[41.9094096,-83.3890141],[44.5176211,-68.8069158],[41.61600778,-71.41143862],[41.4840932,-71.3478555],[41.4271554,-82.3509127],[40.8100003,-73.8763014],[36.00427554,-75.65144892],[41.7661212,-71.3930253],[10.1975742,-64.6619967],[20.7494792,-105.445214],[59.9325929,30.2129843],[36.8867706,30.6963243],[-34.5360605,-58.4681889],[18.5607053,-93.4070011],[9.9796588,-83.0320322],[10.947393,108.241228]]; // end | |
let tsNow = new Date/1e3|0; | |
//let longjohn = require('longjohn'); | |
//longjohn.async_trace_limit = 5; | |
(async () => { | |
for (let i in coords) { | |
let [lat, lng] = coords[i]; | |
let result = await getTz(lat, lng); | |
console.log( | |
"%s\t%s", | |
result.status, | |
result.status === 'OK' ? result.timeZoneId : '' | |
); | |
//await sleep(1000); | |
} | |
})(); | |
function getTz(lat, lng) { | |
let url = 'https://maps.googleapis.com/maps/api/timezone/json' | |
+ '?location=' + lat + ',' + lng | |
+ '×tamp=' + tsNow | |
+ '&key=' + apiKey; | |
return fetch(url).then(tzData => { | |
return tzData; | |
}).catch(err => { | |
return {status: err.message}; | |
}); | |
} | |
function getTzRp(lat, lng) { | |
return rp({ | |
uri : 'https://maps.googleapis.com/maps/api/timezone/json', | |
qs : { | |
location : lat + ',' + lng, | |
timestamp : tsNow, | |
key : apiKey | |
}, | |
json : true | |
}).then(tzData => { | |
return tzData; | |
}).catch(err => { | |
return {status: err.message}; | |
}); | |
} | |
function fetch(url) { | |
// https://nodejs.org/api/https.html | |
return new Promise(function(resolve, reject) { | |
https.get(url, (res) => { | |
const statusCode = res.statusCode; | |
const contentType = res.headers['content-type']; | |
let error; | |
if (statusCode !== 200) { | |
error = new Error(`Request Failed.\n` + | |
`Status Code: ${statusCode}`); | |
} else if (!/^application\/json/.test(contentType)) { | |
error = new Error(`Invalid content-type.\n` + | |
`Expected application/json but received ${contentType}`); | |
} | |
if (error) { | |
// consume response data to free up memory | |
res.resume(); | |
reject(error); | |
return; | |
} | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', (chunk) => rawData += chunk); | |
res.on('end', () => { | |
try { | |
let parsedData = JSON.parse(rawData); | |
resolve(parsedData); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
}).on('error', (e) => { | |
reject( new Error(`Got error: ${e.message}`) ); | |
}); | |
}); | |
} | |
function sleep(ms = 0) { | |
return new Promise(r => setTimeout(r, ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment