Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created May 19, 2016 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrrgn/f3b6615f2047eaa137771454ef0ff537 to your computer and use it in GitHub Desktop.
Save dhrrgn/f3b6615f2047eaa137771454ef0ff537 to your computer and use it in GitHub Desktop.
// Require lodash library
var _ = require('lodash');
// Require dateformat library
var dateFormat = require('dateformat');
//require the icons map script
var icons = require('./icons');
// Current weather root url for the actual data
var currentWeatherRootUrl = 'http://api.openweathermap.org/data/2.5/weather?APPID=575d6a8a0eda57506aefa0a327af4b19';
// Weather forecast root url for the graph data
var weatherForecastRootUrl = 'http://api.openweathermap.org/data/2.5/forecast?APPID=575d6a8a0eda57506aefa0a327af4b19';
// Weather daily forecast root url fot the min/max temperature values
var weatherForecastDailyRootUrl = 'http://api.openweathermap.org/data/2.5/forecast/daily?APPID=575d6a8a0eda57506aefa0a327af4b19';
// Convert degree Kelvin to degree Celsius
var kelvinToC = function (kelvin) {
return kelvin - 273;
}
// Get date from Unix timestamp
var dateFromTimestamp = function (timestamp) {
var date = new Date(timestamp * 1000);
return dateFormat(date, "dddd, mmmm d");
}
// Get time from Unix timestamp
var timeFromTimestamp = function (timestamp) {
var date = new Date(timestamp * 1000);
return dateFormat(date, "HH:MM");
}
module.exports = function (latitude, longitude) {
var currentWeatherUrl = `${currentWeatherRootUrl}&lat=${latitude}&lon=${longitude}`;
var weatherForecastUrl = `${weatherForecastRootUrl}&lat=${latitude}&lon=${longitude}`;
var weatherForecastDailyUrl = `${weatherForecastDailyRootUrl}&lat=${latitude}&lon=${longitude}`;
let currentWeather = fetch(currentWeatherUrl)
.then((response) => response.json())
.then((json) => {
return {
actualTemperature: kelvinToC(json.main.temp).toFixed(0),
date: dateFromTimestamp(json.dt),
cityName: json.name,
windSpeed: json.wind.speed,
humidity: json.main.humidity.toFixed(0),
pressure: json.main.pressure.toFixed(0),
weatherDescription: _.toUpper(json.weather[0].description),
weatherIcon: icons(json.weather[0].icon),
minTemperature: '',
maxTemperature: '',
temperaturesForecast: [],
temperaturesForecastLabels: []
}
});
let weatherForecastDaily = fetch(weatherForecastDailyUrl)
.then((response) => response.json())
.then((json) => {
return {
maxTemperature: kelvinToC(json.list[0].temp.max).toFixed(1),
minTemperature: kelvinToC(json.list[0].temp.min).toFixed(1),
};
});
let weatherForecast = fetch(weatherForecastUrl)
.then((response) => response.json())
.then((json) => {
let temperaturesForecast = [],
temperaturesForecastLabels = [];
for (var i = 0; i < 9; i++) {
temperaturesForecast[i] = Number(kelvinToC(json.list[i].main.temp).toFixed(1));
temperaturesForecastLabels[i] = timeFromTimestamp(json.list[i].dt);
}
return {temperaturesForecast, temperaturesForecastLabels};
});
return Promise.all([currentWeather, weatherForecastDaily, weatherForecast])
.then((responses) => {
let result = {};
responses.forEach((response) => {
result = Object.assign(result, response);
});
return result;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment