-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaction-network-event-data.js
More file actions
64 lines (55 loc) · 2.35 KB
/
action-network-event-data.js
File metadata and controls
64 lines (55 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* eslint-disable no-unused-vars */
// This function gets the start time of an event, based on the event's location time zone.
function getStartTime (event) {
const startDate = new Date(event.start_date)
return new Date(`${startDate.toUTCString()} ${dstOffset(startDate)}`)
}
// This function gets the end time of an event, based on the event's location time zone.
function getEndTime (event) {
const startDate = getStartTime(event)
const endDate = new Date(event.end_date)
const outputDate = new Date(endDate.toUTCString() + ' ' + dstOffset(endDate))
if (isNaN(outputDate.getUTCFullYear())) {
return new Date(startDate.getTime() + 60 * 1000 * defultLengthMinutes)
}
return outputDate
}
function sortEventByDate (eventFirst, eventSecond) {
const startTimeFirst = getStartTime(eventFirst)
const startTimeSecond = getStartTime(eventSecond)
return startTimeFirst - startTimeSecond
}
// This function returns the requested event ID if it is found in the Action Network event data.
function getEventIDFromAN (contentJSON, searchID) {
const identifiers = contentJSON.identifiers
const fullSearchID = `${searchID}:[^,]*`
const regexID = new RegExp(fullSearchID).exec(identifiers)
if (!regexID) {
console.warn(`${searchID} not found in Action Network event identifiers.`)
return null
}
const foundID = regexID[0].substring(fullSearchID.indexOf('['))
console.log(`${searchID} found in Action Network event identifiers: ${foundID}`)
return foundID
}
// This function tags an Action Network event with the Google ID for its corresponding Google Calendar event
function tagANEvent (actionNetworkURL, googleID, apiKey) {
// Check if the "AN_API_KEY" property is null
if (!apiKey) {
console.error('No Action Network API Key "AN_API_KEY" provided, cannot continue.')
return
}
// Set the options for the request and send it to Action Network, logging the response
const options = {
method: 'put',
payload: JSON.stringify({
identifiers: [`google_id_${scriptProperties.getProperty('GCAL_ID').replace(/[&/\\#, +()$~%.'":*?<>{}]/g, '_')}:${googleID}`]
}),
headers: {
'Content-Type': 'application/json',
'OSDI-API-Token': apiKey
}
}
console.log(`Tagging Action Network event ${actionNetworkURL} with Google Calendar event ID ${googleID}`)
UrlFetchApp.fetch(`${apiUrlAn}events/${actionNetworkURL}`, options)
}