Skip to content

Commit

Permalink
Updated scripts to handle overwriting and not erasing when bad data
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoabernier authored Apr 13, 2024
1 parent c1bdce5 commit 237808c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions .github/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@ function ensureDirectoryExists(filePath) {
const dirname = path.dirname(filePath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
console.log(`Directory ${dirname} created.`);
}
}

async function fetchICS() {
const response = await fetch(ICS_URL);
const data = await response.text();
return data;
try {
const response = await fetch(ICS_URL);
if (!response.ok) {
throw new Error(`Failed to fetch ICS file: ${response.statusText}`);
}
const data = await response.text();
return data;
} catch (error) {
console.error('Fetch ICS Error:', error);
return null;
}
}

function parseAndConvertICALToJSON(icsData) {
try {
const jcal = ICAL.parse(icsData);
const comp = new ICAL.Component(jcal);
const events = comp.getAllSubcomponents('vevent');
const jsonData = events.map(event => {
return events.map(event => {
const vevent = new ICAL.Event(event);
return {
summary: vevent.summary,
Expand All @@ -39,10 +48,9 @@ function parseAndConvertICALToJSON(icsData) {
recurrenceId: vevent.component.getFirstPropertyValue('recurrence-id') ? vevent.component.getFirstPropertyValue('recurrence-id').toString() : null
};
});
return jsonData;
} catch (error) {
console.error('Error converting ICAL to JSON:', error);
return null; // Return null to indicate failure
return null;
}
}

Expand All @@ -56,18 +64,24 @@ async function main() {
ensureDirectoryExists(ICS_OUTPUT_FILE);

const icsData = await fetchICS();
if (!icsData) {
console.log('Failed to download ICS data. Exiting.');
return;
}

fs.writeFileSync(ICS_OUTPUT_FILE, icsData);
console.log('ICS file has been downloaded and saved.');

const jsonData = {
lastRetrieved: new Date().toISOString(),
events: parseAndConvertICALToJSON(icsData)
};

if (jsonData.events) {
fs.writeFileSync(JSON_OUTPUT_FILE, JSON.stringify(jsonData, null, 2));
console.log('ICS data has been converted to JSON and saved.');
} else {
console.log('Failed to convert ICAL to JSON.');
console.log('Failed to convert ICAL to JSON or JSON is empty.');
}
} catch (error) {
console.error('Error processing ICS file:', error);
Expand Down

0 comments on commit 237808c

Please sign in to comment.