Created
March 22, 2016 20:35
-
-
Save swbuehler/6dae80e88036b1ca340d to your computer and use it in GitHub Desktop.
Google Apps Script for
This file contains hidden or 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
function myFunction() { | |
var moment = Moment.load(); | |
var cal = CalendarApp.getCalendarById('[[CALENDAR ID GOES HERE]]'); | |
var token = '[[YOUR AUTOMATIC DEVELOPER TOKEN]]'; | |
var now = new Date(); | |
var start = moment(now).startOf('day').subtract(1,'days').utc().format('X'); | |
var end = moment(now).endOf('day').subtract(1,'days').utc().format('X'); | |
// var start = moment('2015-09-13T00:00:00-04:00').utc().format('X'); | |
// var end = moment('2015-10-16T16:45:59-05:00').utc().format('X'); | |
var url = ('https://api.automatic.com/trip/?limit=250&ended_at__gte=' + start + '&ended_at__lte=' + end); | |
var input = UrlFetchApp.fetch(url, { | |
headers : { | |
'Authorization' : 'Bearer ' + token | |
} | |
}); | |
var json = JSON.parse(input); | |
var trips = json.results; | |
for (i = 0; i < trips.length; i++){ | |
var calstart = moment(trips[i].started_at).toDate(); | |
var calend = moment(trips[i].ended_at).toDate(); | |
var distance = trips[i].distance_m * 0.000621371; | |
var gallons = trips[i].fuel_volume_l * 0.264172; | |
var mpg = trips[i].average_kmpl * 2.35215; | |
var summary = 'Drove ' + distance.toFixed(2) + ' miles from ' + trips[i].start_address.display_name + ' to ' + trips[i].end_address.display_name; | |
var desc = 'Fuel Cost: $' + trips[i].fuel_cost_usd + '\n'; | |
desc += 'Fuel Volume: ' + trips[i].fuel_volume_l + ' litres (' + gallons.toFixed(2) + ' U.S. gallons)\n'; | |
desc += 'Average Mileage: ' + trips[i].average_kmpl + ' km/l (' + mpg.toFixed(2) + ' MPG)\n'; | |
desc += 'Hard Brakes: ' + trips[i].hard_brakes + '\n'; | |
desc += 'Hard Accelerations: ' + trips[i].hard_accels + '\n'; | |
desc += 'Time over 70 MPH: ' + trips[i].duration_over_70_s + ' seconds\n'; | |
desc += 'Time over 75 MPH: ' + trips[i].duration_over_75_s + ' seconds\n'; | |
desc += 'Time over 80 MPH: ' + trips[i].duration_over_80_s + ' seconds\n'; | |
desc += 'Amount of City/Hwy/Night Driving: ' + trips[i].city_fraction * 100 + '%/' + trips[i].highway_fraction * 100 + '%/' + trips[i].night_driving_fraction * 100 + '%\n'; | |
desc += 'Seconds Idle: ' + trips[i].idling_time_s + ' seconds'; | |
var newEvent = cal.createEvent( | |
summary, | |
calstart, | |
calend, | |
{ | |
description : desc | |
} | |
); | |
Utilities.sleep(250); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment