Last active
July 13, 2020 10:08
-
-
Save Skoatpalace/53b427e9f3b9af44e466aa235f6fe17a to your computer and use it in GitHub Desktop.
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
/** | |
* this function return data from Google calendar | |
* @return(object) return JSON Object with data. | |
*/ | |
const doGet = () => { | |
const events = JSON.stringify(getAllEvents_()); | |
return ContentService.createTextOutput(events).setMimeType(ContentService.MimeType.JSON); | |
} | |
/** | |
* Lists the next 100 upcoming events in the user's default calendar. | |
* @return(object) Custom Object who contains 100 next events from Google Calendar. | |
*/ | |
const getAllEvents_ = () => { | |
const calendarId = 'CalendarID'; // TODO add target calendar ID | |
const now = new Date(); | |
const data = []; | |
const events = Calendar.Events.list(calendarId, { | |
timeMin: now.toISOString(), | |
singleEvents: true, | |
orderBy: 'startTime', | |
maxResults: 100 | |
}); | |
if (events.items && events.items.length > 0) { | |
for (let i = 0; i < events.items.length; i++) { | |
const d = {} | |
const event = events.items[i]; | |
d.podcast_title = event.summary; | |
d.podcast_start = event.start.dateTime; | |
d.podcast_end = event.end.dateTime; | |
d.podcast_animator = event.description ? event.description.split(',') : []; | |
d.image = getPictureForProgram_(event.summary); | |
data.push(d); | |
}; | |
} else { | |
console.error('No events found.'); | |
} | |
return data; | |
} | |
/** | |
* Get picture in Google Drive corresponding to a keyword who is passed. | |
* @param(string) keyword to search. | |
* @return(string) url result from search or default picture url. | |
*/ | |
const getPictureForProgram_ = (programName) => { | |
const folder = DriveApp.getFolderById(FOLDERID) // TODO add folder id where pictures are stored. | |
const files = folder.getFiles(); | |
let url; | |
while (files.hasNext()) { | |
let file = files.next(); | |
if (file.getName().indexOf(programName) !== -1) { | |
url = file.getUrl(); | |
} | |
} | |
if (!url) { | |
programName = 'default'; | |
while (files.hasNext()) { | |
let file = files.next(); | |
if (file.getName().indexOf(programName) !== -1) { | |
url = file.getUrl(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment