116 lines
2.5 KiB
TypeScript
116 lines
2.5 KiB
TypeScript
namespace _munin.sources.ical_feed
|
|
{
|
|
|
|
/**
|
|
*/
|
|
export type type_parameters = {
|
|
url : string;
|
|
filtration : {
|
|
category_blacklist : Array<string>;
|
|
title_blacklist : Array<string>;
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
*/
|
|
async function fetch(
|
|
parameters : type_parameters
|
|
) : Promise<Array<_munin.type_event>>
|
|
{
|
|
const url : lib_plankton.url.type_url = lib_plankton.url.decode(parameters.url);
|
|
const http_request : lib_plankton.http.type_request = {
|
|
"scheme": (url.scheme as ("http" | "https")),
|
|
"host": url.host,
|
|
"path": url.path,
|
|
"version": "HTTP/2",
|
|
"method": lib_plankton.http.enum_method.get,
|
|
"query": ("?" + url.query),
|
|
"headers": {},
|
|
"body": null,
|
|
};
|
|
const http_response : lib_plankton.http.type_response = await lib_plankton.http.call(http_request);
|
|
const ics : string = http_response.body.toString();
|
|
const vcalendar : lib_plankton.ical.type_vcalendar = lib_plankton.ical.ics_decode(ics);
|
|
const events : Array<_munin.type_event> = (
|
|
vcalendar.vevents
|
|
.filter(
|
|
vevent => (
|
|
// category
|
|
(
|
|
vevent.categories.every(
|
|
category => (
|
|
! (
|
|
parameters.filtration.category_blacklist
|
|
.map(category_ => category_.toLowerCase())
|
|
.includes(category.toLowerCase())
|
|
)
|
|
)
|
|
)
|
|
)
|
|
&&
|
|
// title
|
|
(
|
|
parameters.filtration.title_blacklist.every(
|
|
title => (
|
|
! (
|
|
vevent.summary.toLowerCase()
|
|
.includes(title.toLowerCase())
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
.map(
|
|
vevent => {
|
|
/**
|
|
* @todo würg!
|
|
*/
|
|
const timezone_shift : int = lib_plankton.pit.timezone_shift_ce(
|
|
lib_plankton.pit.from_datetime(
|
|
{
|
|
"timezone_shift": 0,
|
|
"date": vevent.dtstart.value.date,
|
|
"time": vevent.dtstart.value.time,
|
|
}
|
|
)
|
|
);
|
|
const begin : lib_plankton.pit.type_datetime = {
|
|
"timezone_shift": timezone_shift,
|
|
"date": vevent.dtstart.value.date,
|
|
"time": vevent.dtstart.value.time,
|
|
};
|
|
const end : lib_plankton.pit.type_datetime = {
|
|
"timezone_shift": timezone_shift,
|
|
"date": vevent.dtend.value.date,
|
|
"time": vevent.dtend.value.time,
|
|
};
|
|
const event : _munin.type_event = {
|
|
"title": vevent.summary,
|
|
"begin": begin,
|
|
"end": end,
|
|
"location": (vevent.location ?? null),
|
|
};
|
|
return event;
|
|
}
|
|
)
|
|
);
|
|
return events;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export function implementation_source(
|
|
parameters : type_parameters
|
|
) : _munin.type_source
|
|
{
|
|
return {
|
|
"fetch": () => fetch(parameters),
|
|
};
|
|
}
|
|
|
|
}
|
|
|