Update timezone lookup to use offline method instead of Google API

This commit is contained in:
Samer Albahra
2018-05-19 09:53:22 -05:00
parent bf538ab085
commit f69fef232a
4 changed files with 3137 additions and 39 deletions

3111
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -7,23 +7,23 @@
"test": "mocha test"
},
"dependencies": {
"cron": "^1.0.9",
"cron": "^1.3.0",
"dotenv": "^2.0.0",
"express": "^4.13.0",
"grunt": "^1.0.1",
"moment-timezone": "^0.5.4",
"mongoose": "^4.0.6",
"suncalc": "^1.6.0",
"timezoner": "^0.1.9",
"xml2js": "^0.4.9"
"express": "^4.16.3",
"geo-tz": "^4.0.1",
"grunt": "^1.0.2",
"moment-timezone": "^0.5.17",
"mongoose": "^4.13.13",
"suncalc": "^1.8.0",
"xml2js": "^0.4.19"
},
"devDependencies": {
"chai": "^3.0.0",
"codecov.io": "^0.1.5",
"grunt-contrib-jshint": "^1.0.0",
"hippie": "^0.5.0",
"istanbul": "^0.4.3",
"mocha": "^2.2.5",
"nock": "^8.0.0"
"grunt-contrib-jshint": "^1.1.0",
"hippie": "^0.5.1",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"nock": "^9.2.6"
}
}

View File

@@ -3,7 +3,7 @@ var http = require( "http" ),
Cache = require( "../models/Cache" ),
SunCalc = require( "suncalc" ),
moment = require( "moment-timezone" ),
timezoner = require( "timezoner" ),
geoTZ = require( "geo-tz" ),
// Define regex filters to match against location
filters = {
@@ -220,33 +220,20 @@ function getYesterdayWeatherData( location, callback ) {
// Calculate timezone and sun rise/set information
function getTimeData( location, callback ) {
timezoner.getTimeZone(
location[ 0 ],
location[ 1 ],
function( err, timezone ) {
if ( err || timezone.status !== "OK" ) {
callback( false );
} else {
timezone = ( timezone.rawOffset + timezone.dstOffset ) / 60;
var tzOffset = getTimezone( timezone, "minutes" ),
var timezone = moment().tz( geoTZ( location[ 0 ], location[ 1 ] ) ).utcOffset();
var tzOffset = getTimezone( timezone, "minutes" );
// Calculate sunrise and sunset since Weather Underground does not provide it
sunData = SunCalc.getTimes( new Date(), location[ 0 ], location[ 1 ] );
// Calculate sunrise and sunset since Weather Underground does not provide it
var sunData = SunCalc.getTimes( new Date(), location[ 0 ], location[ 1 ] );
sunData.sunrise.setUTCMinutes( sunData.sunrise.getUTCMinutes() + tzOffset );
sunData.sunset.setUTCMinutes( sunData.sunset.getUTCMinutes() + tzOffset );
sunData.sunrise.setUTCMinutes( sunData.sunrise.getUTCMinutes() + tzOffset );
sunData.sunset.setUTCMinutes( sunData.sunset.getUTCMinutes() + tzOffset );
var weather = {
timezone: timezone,
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() )
};
callback( weather );
}
},
{ key: process.env.GOOGLE_API_KEY }
);
callback( {
timezone: timezone,
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() )
} );
}
// Retrieve cached record for a given location

View File

@@ -14,7 +14,7 @@ if ( !process.env.HOST || !process.env.PORT ) {
}
// Connect to local MongoDB instance
mongoose.connect( "localhost" );
mongoose.connect( "mongodb://localhost", { useMongoClient: true } );
// If the database connection cannot be established, throw an error
mongoose.connection.on( "error", function() {