// Scoutworks → hire calendar feed. https://developers.scoutworks.app/recipes/hall-lettings-calendar
const BASE = 'https://api-demo.scoutworks.app/api/v1';
const DAY = 24 * 60 * 60 * 1000;
// Calendar apps call this address. Add ?asset=main-hall for one thing only.
function doGet(e) {
const only = ((e && e.parameter && e.parameter.asset) || '').toLowerCase().slice(0, 80);
// Serve a copy made in the last hour. Repeated requests then cost no API calls.
const cache = CacheService.getScriptCache();
const cached = cache.get('hire:' + only);
if (cached) return ContentService.createTextOutput(cached).setMimeType(ContentService.MimeType.ICAL);
const now = new Date();
const from = new Date(now.getTime() - 30 * DAY).toISOString();
const group = fetchScoutworks(BASE + '/me').data.group.name;
const assets = new Map();
readAll('/hire-assets').forEach(function (a) { assets.set(a.id, a); });
const wanted = function (assetId) {
if (!only) return true;
const asset = assets.get(assetId);
return asset ? asset.slug === only : false;
};
const lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//Scoutworks hire calendar recipe//EN',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'X-WR-CALNAME:' + text(group + ' bookings'),
];
readAll('/hire-bookings').forEach(function (b) {
if (b.status === 'cancelled' || b.endAt < from || !wanted(b.assetId)) return;
const asset = assets.get(b.assetId);
const who = b.hirerType === 'private' || !b.organisationName ? HIRERS[b.hirerType] || 'Hire' : b.organisationName;
lines.push(
'BEGIN:VEVENT',
'UID:' + b.id + '@scoutworks.app',
'DTSTAMP:' + stamp(now),
'DTSTART:' + stamp(new Date(b.startAt)),
'DTEND:' + stamp(new Date(b.endAt)),
'SUMMARY:' + text((b.status === 'pending' ? 'Provisional: ' : '') + who),
'LOCATION:' + text(asset ? asset.name : ''),
'DESCRIPTION:' + text('Booking ' + b.reference + '. ' + STATUSES[b.status] +
(b.expectedAttendees ? '. About ' + b.expectedAttendees + ' people' : '') + '.'),
'STATUS:' + (b.status === 'pending' ? 'TENTATIVE' : 'CONFIRMED'),
'END:VEVENT'
);
});
readAll('/hire-blocked-dates').forEach(function (d) {
// A block with no asset covers everything you hire out.
if (d.endAt < from || (d.assetId !== null && !wanted(d.assetId))) return;
const asset = d.assetId ? assets.get(d.assetId) : null;
lines.push(
'BEGIN:VEVENT',
'UID:' + d.id + '@blocked.scoutworks.app',
'DTSTAMP:' + stamp(now),
'DTSTART:' + stamp(new Date(d.startAt)),
'DTEND:' + stamp(new Date(d.endAt)),
'SUMMARY:' + text('Not available: ' + (asset ? asset.name : 'everything')),
'TRANSP:OPAQUE',
'END:VEVENT'
);
});
lines.push('END:VCALENDAR');
const ics = lines.map(fold).join('\r\n') + '\r\n';
try {
cache.put('hire:' + only, ics, 60 * 60);
} catch (err) {
// A feed over 100 KB is too big to keep. Serve it without the copy.
}
return ContentService.createTextOutput(ics).setMimeType(ContentService.MimeType.ICAL);
}
const STATUSES = {
pending: 'Waiting for you to confirm',
confirmed: 'Confirmed',
awaiting_inspection: 'Ended, to be checked',
completed: 'Completed',
};
const HIRERS = {
scout_group: 'Scout group',
community: 'Community group',
private: 'Private hire',
corporate: 'Business',
other: 'Hire',
};
// 2026-09-24T18:30:00.000Z → 20260924T183000Z
function stamp(date) {
return date.toISOString().replace(/[-:]/g, '').replace(/\.\d{3}/, '');
}
// Escapes the characters that have a meaning in a calendar file.
function text(value) {
return String(value)
.replace(/\\/g, '\\\\')
.replace(/;/g, '\\;')
.replace(/,/g, '\\,')
.replace(/\r?\n/g, '\\n');
}
// Calendar files keep each line short. A long line continues after a space.
function fold(line) {
const parts = [];
while (line.length > 73) {
parts.push(line.slice(0, 73));
line = ' ' + line.slice(73);
}
parts.push(line);
return parts.join('\r\n');
}
// Every item in a list, following nextCursor to the last page.
function readAll(path) {
const items = [];
let cursor = null;
do {
let url = BASE + path + (path.indexOf('?') === -1 ? '?' : '&') + 'limit=200';
if (cursor) url += '&cursor=' + encodeURIComponent(cursor);
const body = fetchScoutworks(url);
items.push.apply(items, body.data);
cursor = body.nextCursor;
} while (cursor);
return items;
}
// Calls the API. Waits and tries again if the rate limit is reached.
function fetchScoutworks(url) {
const apiKey = PropertiesService.getScriptProperties().getProperty('SCOUTWORKS_API_KEY');
if (!apiKey) throw new Error('Add SCOUTWORKS_API_KEY in Project Settings → Script Properties.');
for (let attempt = 0; attempt < 3; attempt++) {
const res = UrlFetchApp.fetch(url, {
headers: { Authorization: 'Bearer ' + apiKey },
muteHttpExceptions: true,
});
const body = JSON.parse(res.getContentText());
if (res.getResponseCode() === 200) return body;
if (res.getResponseCode() === 429) {
Utilities.sleep((body.retryAfter || 30) * 1000);
continue;
}
throw new Error(body.error.code + ': ' + body.error.message);
}
throw new Error('Scoutworks is still busy. Try again in a minute.');
}