forked from ryanmwangi/CalMerger
feat(smart url): add feature to try alternate url versions with and without .ics suffix
This commit is contained in:
parent
48548dadc8
commit
30622c611a
1 changed files with 25 additions and 10 deletions
|
@ -15,18 +15,33 @@ export const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g,
|
|||
// Fetch calendar data from URL or file
|
||||
export async function fetchCalendarData(calendar) {
|
||||
const isFilePath = !calendar.url.startsWith('http');
|
||||
if (isFilePath) {
|
||||
// logger.debug(`Reading calendar from file: ${calendar.url}`);
|
||||
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar };
|
||||
}
|
||||
try {
|
||||
if (isFilePath) {
|
||||
// logger.debug(`Reading calendar from file: ${calendar.url}`);
|
||||
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar };
|
||||
} else {
|
||||
// logger.debug(`Fetching calendar from URL: ${calendar.url}`);
|
||||
const response = await axios.get(calendar.url);
|
||||
return { data: response.data, ...calendar };
|
||||
// First try the original URL
|
||||
const initialResponse = await axios.get(calendar.url);
|
||||
return { data: initialResponse.data, ...calendar };
|
||||
} catch (initialError) {
|
||||
logger.debug(`Initial fetch failed, trying extension adjustment for: ${calendar.url}`);
|
||||
|
||||
// Determine alternate URL version
|
||||
const altUrl = calendar.url.endsWith('.ics')
|
||||
? calendar.url.slice(0, -4) // Remove .ics
|
||||
: calendar.url + '.ics'; // Add .ics
|
||||
|
||||
try {
|
||||
// Try the alternate version
|
||||
const altResponse = await axios.get(altUrl);
|
||||
logger.debug(`Success with adjusted URL: ${altUrl}`);
|
||||
return { data: altResponse.data, ...calendar };
|
||||
} catch (altError) {
|
||||
logger.error(`Both URL versions failed:
|
||||
Original: ${calendar.url}
|
||||
Adjusted: ${altUrl}`);
|
||||
throw new Error(`Calendar fetch failed for both URL versions`);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
|
||||
throw new Error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue