Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function weatherLogger() {
var url = "http://api.wunderground.com/api/XXXXXXXXXXX/forecast/q/STATE/CITY.json"
var response = UrlFetchApp.fetch(url);
var weather = JSON.parse(response);
var summary = weather.forecast.simpleforecast.forecastday[0];
var newFileData = summary["date"]["pretty"] + "\n";
newFileData += "\n";
newFileData += "High temp: " + summary["high"]["fahrenheit"] + "°F/" + summary["high"]["celsius"] + "°C";
newFileData += "\n";
newFileData += "Low temp: " + summary["low"]["fahrenheit"] + "°F/" + summary["low"]["celsius"] + "°C";
newFileData += "\n";
newFileData += "Conditions: " + summary["conditions"];
newFileData += "\n";
newFileData += "Precipitation: " + summary["qpf_allday"]["in"];
newFileData += "\n";
newFileData += "Snow: " + summary["snow_allday"]["in"];
newFileData += "\n";
newFileData += "Max Wind Speed: " + summary["maxwind"]["mph"] + "mph " + summary["maxwind"]["dir"];
newFileData += "\n";
newFileData += "Avg Wind Speed: " + summary["avewind"]["mph"] + "mph " + summary["avewind"]["dir"];
newFileData += "\n";
newFileData += "Humidity: " + summary["avehumidity"];
newFileData += "\n";
newFileData += "\n";
newFileData += "Raw Data:";
for (var key in summary) {
var dataToLog = key + ": " + JSON.stringify(summary[key]);
if (newFileData == "") {
newFileData = dataToLog;
}
else {
newFileData += "\n";
newFileData += dataToLog;
};
};
var today = new Date();
var d = new Date(today);
var y = d.getFullYear();
var m = d.getMonth();
m = m + 1
m = "00" + m
m = m.substr(m.length - 2);
var day = d.getDate();
day = "00" + day;
day = day.substr(day.length - 2);
var dateString = y + m + day;
DriveApp.createFile("Weatherx_Todayx_" + dateString + ".txt", newFileData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment