Skip to content

Instantly share code, notes, and snippets.

@Bubblemelon
Last active June 14, 2022 03:35
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 Bubblemelon/00235e5ac95732e3881e747db4cd4c7d to your computer and use it in GitHub Desktop.
Save Bubblemelon/00235e5ac95732e3881e747db4cd4c7d to your computer and use it in GitHub Desktop.
Populates a Google calendar titled "Rise and Fall", using the a free sunrise and sunset API from a specified date range for San Francisco.
/**
* EXAMPLE: https://developers.google.com/calendar/api/quickstart/apps-script
* @see https://developers.google.com/calendar/api/v3/reference/events/list
*
*
* Sun rise and set API: https://sunrise-sunset.org/api
*
*/
function populateCalSunRiseNSet() {
/*
* === GLOBAL VARIABLES ===
*/
// The Calendar to set the events in:
// This is an ARRAY, that should only have one element! i.e. one Calendar
var riseFallCal = CalendarApp.getCalendarsByName('Rise and Fall');
// Logger.log('Found %s matching calendars.', riseFallCal.length);
// Logger.log(riseFallCal[0].getName());
// Sun Rise and Set Location
// location reference
// https://sunrise-sunset.org/search?location=San+Francisco
var latitude = '37.7790262';
var longitude = '-122.419906';
var today = new Date();
Logger.log('The date is ' + Utilities.formatDate(today, 'America/Los_Angeles', 'yyyy-MM-dd'));
// https://developers.google.com/google-ads/scripts/docs/features/dates
// The last day where for the event to be set
// The monthIndex for december is 11
var endDate = new Date(2022, 11, 32);
/*
* ^^^ GLOBAL VARIABLES ^^^
*/
/*
Takes a date object
Makes a call to sunset/sunrise API
retrive JSON
parse JSON into usable object
Returns JSON as parsed data
*/
function getDayData(date){
var url = 'https://api.sunrise-sunset.org/json'
+ '?lat=' + latitude
+ '&lng=' + longitude
+ '&date=' + Utilities.formatDate(date, 'America/Los_Angeles', 'yyyy-MM-dd')
+ '&formatted=0';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
Logger.log("getDayData json call: \n" + response);
var json = response.getContentText();
return JSON.parse(json); //data
// Logger.log(data.results.sunrise);
}
/*
This is to split the date from the time without the timezone
Appends UTC at the end of string
*/
function timeExtract(input) {
var timeWithZone = input.split("T");
var justTime = timeWithZone[1].split("+");
// Logger.log(justTime[0]);
return justTime[0];
}
/*
* Where actual work is done!
*/
try {
for (var i = today; i < endDate; i.setDate(i.getDate() + 1)) {
Logger.log("Setting the event for: "+ i + '\n');
var data = getDayData(i);
// Redudant variables:
// var nauticalTwilightBegin = data.results.nautical_twilight_begin;
// var sunriseTime = data.results.sunrise;
// This is to split the date from the time without the timezone
var nauticalTwilightBeginExtract = timeExtract(data.results.nautical_twilight_begin);
var sunriseTimeExtract = timeExtract(data.results.sunrise);
// THIS part should be a function
//
var riseStartTime = Utilities.formatDate(today, 'America/Los_Angeles', 'MMMM dd, yyyy ') + nauticalTwilightBeginExtract + ' UTC' ;
var riseEndTime = Utilities.formatDate(today, 'America/Los_Angeles', 'MMMM dd, yyyy ') + sunriseTimeExtract + ' UTC' ;
// Logger.log('Start ' + riseStartTime + ' End ' + riseEndTime);
//
//
// Logger.log(riseFallCal[0].getName());
// Logger.log(riseFallCal[0].getId());
// CREATE SUNRISE EVENT! (THIS SHOULD BE A FUNCTION -- to avoid redundancy sake ... )
var event = CalendarApp.getCalendarById(riseFallCal[0].getId()).createEvent('Sunrise 🌅',
new Date(riseStartTime),
new Date(riseEndTime));
Logger.log('SUNRISE Event ID: ' + event.getId());
// Redudant variables:
// var sunsetTime = data.results.sunset;
// var nauticalTwilightEnd = data.results.nautical_twilight_end;
// This is to split the date from the time without the timezone
var sunsetTimeExtract = timeExtract(data.results.sunset);
var nauticalTwilightEndExtract = timeExtract(data.results.nautical_twilight_end);
var settingStartTime = Utilities.formatDate(today, 'America/Los_Angeles', 'MMMM dd, yyyy ') + sunsetTimeExtract + ' UTC' ;
var settingEndTime = Utilities.formatDate(today, 'America/Los_Angeles', 'MMMM dd, yyyy ') + nauticalTwilightEndExtract + ' UTC' ;
Logger.log('Start ' + riseStartTime + ' End ' + riseEndTime);
// CREATE SUNSET EVENT!
var event = CalendarApp.getCalendarById(riseFallCal[0].getId()).createEvent('Sunset 🌇',
new Date(settingStartTime),
new Date(settingEndTime));
Logger.log('SUNSET Event ID: ' + event.getId());
}
} catch (err) {
// TODO (developer) - Handle exception from Calendar API
Logger.log('Failed with error %s', err.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment