Skip to content

Instantly share code, notes, and snippets.

@HaydenElza HaydenElza/hooferflag.gs
Last active Dec 5, 2019

Embed
What would you like to do?
Tracks changes in UW Madison Hoofer Sailing Club lake conditions flag. Flag status is saved in a google sheets to display the history later. Visit site at: https://elza.me/hooferflag
function fetchData() {
// Fetch data
//var url = 'http://testout.hoofersailing.org/feed.php'
//var url = 'https://ehs.wisc.edu/current-flag.php'
var url = 'https://endpoints.wams.doit.wisc.edu/ehs.wisc.edu/LakeConditions/Api/'
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
// Process response to object
var json = response.getContentText().replace(/\(|\)|;/g, '');
var data = JSON.parse(json);
// Return the flag color and date updated
return {
retrieved: data.Retrieved.substring(0,19).replace('T', ' '),
foundBuoy: data.FoundBuoy,
foundFlag: data.FoundFlag,
foundWeather: data.FoundWeather,
waterTemp: data.WaterTemp,
flag: data.Flag,
sunset: data.SunsetHour + "." + data.SunsetMinute,
windDir: data.WindDir,
windDegrees: data.WindDegrees,
windMph: data.WindMph,
//wind: data.WindMph + "mph " + data.WindDir + " (" + data.WindDegrees + "°)",
tempF: data.TempF,
tempC: data.TempC,
forecast: data.Forecast,
forecastIcon: data.ForecastIcon
//temp: data.TempF + "°F (" + data.TempC + "°C)"
};
};
function isNewFlag (flagHistorySpreadsheetId,data) {
// Get sheet
var ss = SpreadsheetApp.openById(flagHistorySpreadsheetId);
var sheet = ss.getSheets()[0]
// Get last recorded flag
var lastRow = sheet.getLastRow();
var lastFlag = sheet.getRange(lastRow,2).getValue();
// Return true if flag has change, false if not
return data.flag!=lastFlag ? true:false;
};
function writeFlagToLog(flagHistorySpreadsheetId,data) {
// Get sheet
var ss = SpreadsheetApp.openById(flagHistorySpreadsheetId);
var sheet = ss.getSheets()[0]
// Add new data to sheet
// var t = new Date( parseInt(data.retrieved) );
// var formatted = t.toISOString().replace(/T/g, ' ').slice(0,-5);
// sheet.appendRow([Date.now(), data.flag, formatted, data.foundBuoy, data.foundFlag, data.foundWeather,
// data.waterTemp, data.sunset, data.windDir, data.windDegrees, data.windMph, data.tempF,
// data.tempC, data.forecast, data.forecastIcon]);
sheet.appendRow([Date.now(), data.flag, data.retrieved, data.foundBuoy, data.foundFlag, data.foundWeather,
data.waterTemp, data.sunset, data.windDir, data.windDegrees, data.windMph, data.tempF,
data.tempC, data.forecast, data.forecastIcon]);
};
function sendNotifications(subscriptionSpreadsheetId,data) {
// Get sheet
var ss = SpreadsheetApp.openById(subscriptionSpreadsheetId);
var sheet = ss.getSheets()[1]
// Get subscriptions
var subs = sheet.getDataRange().getValues().join();
if (subs == '#N/A') {return}
// Prep message
var fro = 'Hoofer Flag Subs<notify.hayden.elza@gmail.com>';
var msg = data.flag!='None' ? data.flag+' flag is now in effect':'Lifesaving Station is Closed';
// Send email
GmailApp.sendEmail(fro, msg, '', {
bcc: subs,
name: 'Hoofer Flag Update'
});
};
function main() {
var flagHistorySpreadsheetId = '1gtrfO23I4Ph_byR8x0QiJi1QmyFofVsK5LuAE-6TF1A';
var subscriptionSpreadsheetId = '1J7z9Ra7fKtiwzxxd1Rkj2u5E9tC2vkqyCJqo5auDKRo';
var data = fetchData();
if(isNewFlag(flagHistorySpreadsheetId,data)) {
writeFlagToLog(flagHistorySpreadsheetId,data);
sendNotifications(subscriptionSpreadsheetId,data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.