generated from maehr/github-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
+server.js
138 lines (124 loc) · 4.56 KB
/
+server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @file This file contains API endpoints for fetching and displaying activities.
*/
import { dev } from '$app/environment';
import * as config from '$lib/config';
import { json } from '@sveltejs/kit';
import createDOMPurify from 'dompurify';
import { promises as fs } from 'fs';
import { JSDOM } from 'jsdom';
import { tmpdir } from 'os';
import { join } from 'path';
import { parseStringPromise } from 'xml2js';
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
/**
* Indicates whether prerendering is enabled.
* @type {boolean}
*/
export const prerender = true;
/**
* Saves data to a file.
* @param {any} data - The data to be saved.
* @param {string} path - The path to save the file.
* @returns {Promise<void>} A promise that resolves when the data is saved.
*/
async function saveToFile(data, path) {
await fs.writeFile(path, JSON.stringify(data));
console.log(`Data saved to: ${path}`);
}
/**
* Returns an array of unique owners from the provided activities.
* @param {Array<any>} activities - The activities array.
* @returns {Array<string>} An array of unique owners.
*/
function getUniqueOwners(activities) {
const allOwners = activities.map((item) => item['$'].owner);
const uniqueOwners = [...new Set(allOwners)];
return uniqueOwners;
}
/**
* Represents an exhibition.
* @typedef {Object} Exhibition
* @property {string} owner - The owner of the exhibition.
* @property {string} title - The title of the exhibition.
* @property {string} shortDescription - The short description of the exhibition.
* @property {string} longDescription - The long description of the exhibition.
* @property {string} originUrl - The origin URL of the exhibition.
*/
/**
* Represents an event with date and time details.
* @typedef {Object} Event
* @property {string} owner - The owner of the event.
* @property {string} title - The title of the event.
* @property {string} shortDescription - The short description of the event.
* @property {string} longDescription - The long description of the event.
* @property {string} originUrl - The origin URL of the event.
* @property {string} startDate - The start date of the event.
* @property {string} endDate - The end date of the event.
* @property {string} startTime - The start time of the event.
* @property {string} endTime - The end time of the event.
* @property {string} ticketURL - The ticket URL of the event.
*/
/**
* Fetches activities data from the XML export and filters them based on configured partners.
* @async
* @returns {Promise<{ events: Array<Event>, exhibitions: Array<Exhibition> }>} A promise that resolves to an object containing the future dates and exhibitions.
*/
async function getActivities() {
const response = await fetch('https://agendabasel.ch/xmlexport/kzexport-basel.xml');
const xml = await response.text();
const data = await parseStringPromise(xml);
const activities = data['kdz:exportActivities']['Activities'][0]['Activity'];
if (dev) {
await saveToFile(activities, join(tmpdir(), 'activities.json'));
console.log(getUniqueOwners(activities));
}
const partners = config.partners;
const parsedActivities = activities
.filter(({ $: { owner } }) => partners.includes(owner))
.map(
({
$: { owner, dauerausstellung },
Title: [title],
ShortDescription: [shortDesc],
LongDescription: [longDesc],
OriginURL: [originUrl],
ActivityDates: [{ ActivityDate: dates = [] } = {}]
}) => ({
owner,
dauerausstellung,
title,
shortDescription: DOMPurify.sanitize(shortDesc, { ALLOWED_TAGS: [] }),
longDescription: DOMPurify.sanitize(longDesc, { ALLOWED_TAGS: [] }),
originUrl,
dates: dates.map(
({ $: { startDate, endDate, startTime, endTime }, TicketURL: [ticketURL] }) => ({
startDate,
endDate,
startTime,
endTime,
ticketURL: ticketURL
})
)
})
);
const exhibitions = parsedActivities
.filter(({ dauerausstellung }) => dauerausstellung === '1')
.sort((a, b) => a.owner.localeCompare(b.owner));
const events = parsedActivities.filter(({ dauerausstellung }) => dauerausstellung === '0');
const flatEvents = events
.flatMap(({ dates, owner, title, shortDescription, originUrl }) =>
dates.map((date) => ({ ...date, owner, title, shortDescription, originUrl }))
)
.sort((a, b) => new Date(a.startDate).getTime() - new Date(b.startDate).getTime());
return { events: flatEvents, exhibitions };
}
/**
* Request handler for the GET method.
* @type {import('./$types').RequestHandler}
*/
export async function GET() {
const activities = await getActivities();
return json(activities);
}