Last active
July 16, 2023 03:46
-
-
Save tai-sho/32ea89f0be8b595f2e616542e39b6d69 to your computer and use it in GitHub Desktop.
Using GoogleAppsScript to link OuraRing's sleep score to the calendar
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
/** | |
* Refer to https://tech.affordigitalife.com/oura-api-sleep-log/ | |
*/ | |
function main() { | |
const days = 10; | |
for (let i = 0; i < days; i++) { | |
let today = new Date() | |
today.setDate(today.getDate() - i) | |
let yesterday = new Date() | |
yesterday.setDate(yesterday.getDate() - (i + 1)) | |
setSleepForDay(today, yesterday) | |
} | |
} | |
function connectOuraAPI(api, parameters) { | |
const token = getProperty('OURA_API_KEY') | |
const baseUrl = 'https://api.ouraring.com/v2/usercollection/' | |
let url = `${baseUrl}${api}?` | |
for (let parameter in parameters){ | |
url += `${parameter}=${parameters[parameter]}&` | |
if(Object.keys(parameters).slice(-1)[0] === parameter){ | |
url = url.slice(0, -1) | |
} | |
} | |
let requestOptions = { | |
muteHttpExceptions: true, | |
method: 'GET', | |
headers: { | |
'Authorization': `Bearer ${token}` | |
} | |
} | |
let res = UrlFetchApp.fetch(url, requestOptions).getContentText() | |
return JSON.parse(res) | |
} | |
function getSleep(parameters) { | |
return connectOuraAPI('sleep', parameters) | |
} | |
function getSleepScore(parameters) { | |
return connectOuraAPI('daily_sleep', parameters) | |
} | |
function addEventGoogleCalendar(score, bedtime, awakeTime, description = ''){ | |
const CALENDAR_ID = getProperty('GOOGLE_CALENDAR_ID_OURA') | |
const calendar = CalendarApp.getCalendarById(CALENDAR_ID) | |
calendar.createEvent(`睡眠スコア:${score}`, new Date(bedtime), new Date(awakeTime), { | |
description: description | |
}) | |
} | |
function getEventForDayGoogleCalendar(day) { | |
const CALENDAR_ID = getProperty('GOOGLE_CALENDAR_ID_OURA') | |
const calendar = CalendarApp.getCalendarById(CALENDAR_ID) | |
return calendar.getEventsForDay(day) | |
} | |
function setSleepForDay(today, yesterday) { | |
let events = getEventForDayGoogleCalendar(today) | |
console.log(events) | |
if (events.length > 0) { | |
return 0; | |
} | |
let parameters = { | |
start_date: formatDate(yesterday), | |
end_date: formatDate(today) | |
} | |
let sleep = getSleep(parameters) | |
parameters.start_date = formatDate(today) | |
let dairySleep = getSleepScore(parameters) | |
if (!dairySleep.data[0]) { | |
console.log('data not found'); | |
return 0; | |
} | |
let dairySleepScore = dairySleep.data[0].score | |
for (let s of sleep.data){ | |
console.log(s) | |
addEventGoogleCalendar(dairySleepScore, s.bedtime_start, s.bedtime_end, JSON.stringify(sleep) + '\n' + JSON.stringify(dairySleep)) | |
} | |
return sleep.data.length; | |
} | |
function getProperty(key) { | |
const prop = PropertiesService.getScriptProperties(); | |
return prop.getProperty(key); | |
} | |
function formatDate(date) { | |
return Utilities.formatDate(date, 'JST', 'yyyy-MM-dd'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment