forked from GitHubMirrors/silverbullet-icalendar
Robust fetch with User-Agent and URL encoding
Some checks failed
Build SilverBullet Plug / build (push) Failing after 26s
Some checks failed
Build SilverBullet Plug / build (push) Failing after 26s
This commit is contained in:
28
icalendar.ts
28
icalendar.ts
@@ -154,14 +154,32 @@ async function getSources(): Promise<Source[]> {
|
||||
* Fetches and parses events from a single calendar source
|
||||
*/
|
||||
async function fetchAndParseCalendar(source: Source): Promise<CalendarEvent[]> {
|
||||
const url = source.url.trim();
|
||||
let url = source.url.trim();
|
||||
|
||||
// Handle internal spaces which are common in copy-pasted URLs
|
||||
if (url.includes(" ")) {
|
||||
console.log(`[iCalendar] URL contains spaces, encoding: "${url}"`);
|
||||
url = encodeURI(url);
|
||||
}
|
||||
|
||||
console.log(`[iCalendar] Fetching: ${url}`);
|
||||
const response = await fetch(url);
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
console.error(`[iCalendar] HTTP error:`, { source, status: response.status, statusText: response.statusText });
|
||||
throw error;
|
||||
const body = await response.text();
|
||||
const errorDetail = body.slice(0, 200).replace(/\n/g, " ");
|
||||
console.error(`[iCalendar] HTTP ${response.status} Error:`, {
|
||||
url,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
bodyPreview: errorDetail
|
||||
});
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText || 'Error'} - ${errorDetail}`);
|
||||
}
|
||||
|
||||
const icsData = await response.text();
|
||||
|
||||
Reference in New Issue
Block a user