-
-
Save jakepeyser/5c50c5d06106d54eb058 to your computer and use it in GitHub Desktop.
Weather Channel service gists
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
// Checks if the API response contains an error and adds an error parameter | |
// Also logs to error stream | |
function checkError(result) { | |
var resultJSON = JSON.parse(result); | |
var errors = resultJSON.errors; | |
if (errors !== null && errors !== undefined) { | |
// Create log entries for each returned error | |
if (errors.length > 0) { | |
for (var i=0; i < errors.length; i++) { | |
console.error("(" + errors[i].error.code + "): " + errors[i].error.message); | |
} | |
} | |
resultJSON.error = true; | |
return JSON.stringify(resultJSON); | |
} | |
else { | |
resultJSON.error = false; | |
return JSON.stringify(resultJSON); | |
} | |
} |
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
// Gets weather data for lat/lon on current date | |
function getCurrentConditions(lat, lon, cb) { | |
// Builds REST URL for requesting current weather | |
var url, requestOpts; | |
// Request for the full TWC API | |
if (this.full) { | |
console.log("Getting current conditions from TWC"); | |
url = "http://api.weather.com/v1/geocode//" + | |
lat + "/" + lon + | |
"/observations/current.json"; | |
requestOpts = { | |
url: url, | |
qs: { | |
apiKey: apiKey, | |
language: "en-US", | |
units: "e" | |
} | |
}; | |
} | |
// Request for the Insights for Weather service | |
else { | |
console.log("Getting current conditions from Weather Insights"); | |
url = "https://" + username + ":" + password + "@twcservice.mybluemix.net/api/weather/v1/geocode" + | |
lat + "/" + lon + | |
"/observations.json?language=en-US&units=e"; | |
requestOpts = { | |
url: url | |
}; | |
} | |
// Returns current weather data for input lat/lon | |
request(requestOpts, function(err, message, result) { | |
result = checkError(result); | |
cb(result); | |
}) | |
} |
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
// Gets weather data for lat/lon on past 10 years on current date | |
function getHistoricConditions(lat, lon, cb) { | |
var dates = []; | |
var date = new Date(); | |
var year = date.getYear() + 1900; | |
var mon = date.getMonth() + 1; | |
var day = date.getDate(); | |
// Adjusts date if Feb 28 | |
if ((mon == 2) && (day == 29)) day = 28; | |
// Adds dates from past 10 years to dates[] array | |
for (var i=1; i<11; i++) { | |
var hDate = "" + (year-i) + "" + right(mon,0,2) + "" + right(day,0,2); | |
dates.push(hDate); | |
} | |
var dateReqs = dates.map(function(date, i) { | |
return function(cb) { getOneHC(lat, lon, dates[i], cb); }; | |
}); | |
// Makes all REST calls for historical weather data in parallel | |
async.parallel(dateReqs, function(err, results) { | |
results = results.map(function(result) { | |
return JSON.parse(result); | |
}) | |
cb(results); | |
}) | |
} |
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
// Gets weather for lat/lon on a single historical date | |
function getOneHC(lat, lon, date, cb) { | |
// Builds REST URL for requesting historical weather | |
var url = "http://api.weather.com/v1/geocode/" + | |
lat + "/" + lon + | |
"/observations/historical.json"; | |
var requestOpts = { | |
url: url, | |
qs: { | |
apiKey: this.apiKey, // Your API key here | |
language: "en-US", | |
units: "e", | |
startDate: date, // 201406153 | |
} | |
}; | |
// Returns historical weather data for input lat/lon | |
request(requestOpts, function(err, message, result) { | |
result = checkError(result); | |
var resultJSON = JSON.parse(result); | |
if (!resultJSON.error) { | |
// Ensure observations are available, otherwise mark an error | |
if (resultJSON.observations.length > 0) { | |
// Get midday recording and replace array with this observation | |
var midIndex = Math.floor(resultJSON.observations.length / 2); | |
resultJSON.observations = [ resultJSON.observations[midIndex] ]; | |
} | |
else | |
resultJSON.error = true; | |
result = JSON.stringify(resultJSON); | |
} | |
cb(null,result); | |
}) | |
} |
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
// Gets weather data for lat/lon on input date | |
function getPastConditions(lat, lon, month, day, year, cb) { | |
// Adjusts date if Feb 28 | |
if ((month == 2) && (day == 29)) day = 28; | |
// Create date string from input parameters | |
date = "" + (year) + "" + right(month,0,2) + "" + right(day,0,2); | |
getOneHC(lat, lon, date, function(err, result) { | |
cb(result) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment