Skip to content

Instantly share code, notes, and snippets.

@davidsulpy
Created July 22, 2016 14:23
Show Gist options
  • Save davidsulpy/7b22905f514058c8a8723e84ae9539ed to your computer and use it in GitHub Desktop.
Save davidsulpy/7b22905f514058c8a8723e84ae9539ed to your computer and use it in GitHub Desktop.
An AWS Lambda function for getting data from the Wunderground API and sending it to an Initial State bucket
'use strict';
let https = require('https'),
http = require('http');
const ISAccessKey = 'INITIALSTATE_ACCESS_KEY',
ISBucketKey = 'INITIALSTATE_BUCKET_KEY',
WundergroundApiKey = 'WUNDERGROUND_API_KEY',
WundergroundCity = 'Nashville',
WundergroundState = 'TN';
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`. For more information see the HTTPS module documentation
* at https://nodejs.org/api/https.html.
*
* Will succeed with the response body.
*/
exports.handler = (event, context, callback) => {
getWeatherData(WundergroundCity, WundergroundState, (error, data) => {
if (error) {
context.fail();
} else{
let isRequestBody = [
{
"key": "Temperature (F)",
"value": data.current_observation.temp_f,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Dewpoint (F)",
"value": data.current_observation.dewpoint_f,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Feels Like (F)",
"value": data.current_observation.feelslike_f,
"epoch": data.current_observation.observation_epoch
},
{
"key": "UV",
"value": data.current_observation.UV,
"epoch": data.current_observation.observation_epoch
},
{
"key": "3rd Day Forecast Max Temp (F)",
"value": data.forecast.simpleforecast.forecastday[2].high.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[2].date.epoch
},
{
"key": "3rd Day Forecast Min Temp (F)",
"value": data.forecast.simpleforecast.forecastday[2].low.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[2].date.epoch
},
{
"key": "5th Day Forecast Max Temp (F)",
"value": data.forecast.simpleforecast.forecastday[4].high.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[4].date.epoch
},
{
"key": "5th Day Forecast Min Temp (F)",
"value": data.forecast.simpleforecast.forecastday[4].low.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[4].date.epoch
},
{
"key": "10th Day Forecast Max Temp (F)",
"value": data.forecast.simpleforecast.forecastday[9].high.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[9].date.epoch
},
{
"key": "10th Day Forecast Min Temp (F)",
"value": data.forecast.simpleforecast.forecastday[9].low.fahrenheit,
"epoch": data.forecast.simpleforecast.forecastday[9].date.epoch
},
{
"key": "Relative Humidity",
"value": data.current_observation.relative_humidity,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Wind Degrees",
"value": data.current_observation.wind_degrees,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Wind Direction",
"value": data.current_observation.wind_dir,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Wind Speed (MPH)",
"value": data.current_observation.wind_mph,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Wind Gust (MPH)",
"value": data.current_observation.wind_gust_mph,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Preceipitation Today (in)",
"value": data.current_observation.precip_today_in,
"epoch": data.current_observation.observation_epoch
},
{
"key": "Conditions",
"value": convertConditionToEmojiToken(data.current_observation.icon),
"epoch": data.current_observation.observation_epoch
}
];
sendToInitialState(isRequestBody, callback);
}
});
};
function convertConditionToEmojiToken(condition) {
const conditionMap = {
"clear" : ":sun_with_face:",
"cloudy" : ":cloud:",
"flurries" : ":snowflake:",
"fog" : ":foggy:",
"hazy" : ":foggy:",
"mostlycloudy" : ":cloud:",
"mostlysunny" : ":sun_with_face:",
"partlycloudy" : ":partly_sunny:",
"partlysunny" : ":partly_sunny:",
"sleet" : ":sweat_drops: :snowflake:",
"rain" : ":umbrella:",
"snow" : ":snowflake:",
"sunny" : ":sun_with_face:",
"tstorms" : ":zap: :umbrella:",
"unknown" : ":grey_question:"
};
return conditionMap[condition];
}
function getWeatherData(city, state, callback) {
const req = http.request({
hostname: 'api.wunderground.com',
port: '80',
path: '/api/'+WundergroundApiKey+'/conditions/forecast10day/q/'+state+'/'+city+'.json',
method: 'GET'
}, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully finished processing HTTP response');
body = JSON.parse(body);
callback(null, body);
});
});
req.on('error', callback);
req.end();
}
function sendToInitialState(data, callback) {
const req = https.request({
hostname: 'groker.initialstate.com',
port: '443',
path: '/api/events',
method: 'POST',
headers: {
'X-IS-AccessKey': ISAccessKey,
'X-IS-BucketKey': ISBucketKey,
'Content-Type': 'application/json',
'Accept-Version': '~0'
}
}, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
callback(null, body);
});
});
req.on('error', callback);
req.end(JSON.stringify(data));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment