UNPKG

@wepublish/api

Version:
101 lines 6.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getImageUrl = exports.upcomingOnly = exports.fetchAndParseKulturagenda = exports.getXMLfromURL = exports.getFallbackDesc = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const date_fns_1 = require("date-fns"); const node_fetch_1 = tslib_1.__importDefault(require("node-fetch")); const slate_serializers_1 = require("slate-serializers"); const xml2js_1 = tslib_1.__importDefault(require("xml2js")); const getFallbackDesc = (source) => `<p>Event imported from ${source}</p>`; exports.getFallbackDesc = getFallbackDesc; function getXMLfromURL(url) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const parser = new xml2js_1.default.Parser(); try { const response = yield (0, node_fetch_1.default)(url); const content = yield response.text(); const data = yield parser.parseStringPromise(content); return data; } catch (e) { throw Error('Unable to get any data from the provided xml.'); } }); } exports.getXMLfromURL = getXMLfromURL; const fetchAndParseKulturagenda = (urlToQuery, source) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b; const eventsParsedXML = yield getXMLfromURL(urlToQuery); const events = (_b = (_a = eventsParsedXML['kdz:exportActivities']) === null || _a === void 0 ? void 0 : _a.Activities[0]) === null || _b === void 0 ? void 0 : _b.Activity; // only take events that take time in the future const upcomingEvents = events.filter((event) => (0, exports.upcomingOnly)(event)); // parse their structure to ours const importedEvents = upcomingEvents === null || upcomingEvents === void 0 ? void 0 : upcomingEvents.map((a) => { return parseXMLEventToWpEvent(a, source); }); return importedEvents; }); exports.fetchAndParseKulturagenda = fetchAndParseKulturagenda; const upcomingOnly = (XMLEvent) => { var _a, _b, _c, _d, _e; const today = (0, date_fns_1.startOfDay)(new Date()); const startDate = XMLEvent && XMLEvent.ActivityDates && typeof XMLEvent.ActivityDates[0] !== 'string' && ((_a = XMLEvent.ActivityDates[0]) === null || _a === void 0 ? void 0 : _a.ActivityDate) && ((_c = (_b = XMLEvent.ActivityDates[0]) === null || _b === void 0 ? void 0 : _b.ActivityDate[0]) === null || _c === void 0 ? void 0 : _c['$']) && ((_e = (_d = XMLEvent.ActivityDates[0]) === null || _d === void 0 ? void 0 : _d.ActivityDate[0]) === null || _e === void 0 ? void 0 : _e['$'].startDate); if (!startDate || (0, date_fns_1.isBefore)(new Date(startDate), today)) { return; } return XMLEvent; }; exports.upcomingOnly = upcomingOnly; const getImageUrl = (event) => { var _a, _b, _c, _d, _e, _f, _g, _h; return ((((_a = event === null || event === void 0 ? void 0 : event.ActivityMultimedia) === null || _a === void 0 ? void 0 : _a.length) && ((_b = event === null || event === void 0 ? void 0 : event.ActivityMultimedia[0]) === null || _b === void 0 ? void 0 : _b.Images.length) && ((_e = (_d = (_c = event === null || event === void 0 ? void 0 : event.ActivityMultimedia[0]) === null || _c === void 0 ? void 0 : _c.Images[0]) === null || _d === void 0 ? void 0 : _d.Image) === null || _e === void 0 ? void 0 : _e.length) && ((_h = (_g = (_f = event === null || event === void 0 ? void 0 : event.ActivityMultimedia[0]) === null || _f === void 0 ? void 0 : _f.Images[0]) === null || _g === void 0 ? void 0 : _g.Image[0].$) === null || _h === void 0 ? void 0 : _h.url)) || null); }; exports.getImageUrl = getImageUrl; const parseXMLEventToWpEvent = (XMLEvent, source) => { var _a, _b, _c, _d, _e, _f; const activityDate = (XMLEvent && XMLEvent.ActivityDates && typeof XMLEvent.ActivityDates[0] !== 'string' && ((_a = XMLEvent.ActivityDates[0]) === null || _a === void 0 ? void 0 : _a.ActivityDate) && ((_c = (_b = XMLEvent.ActivityDates[0]) === null || _b === void 0 ? void 0 : _b.ActivityDate[0]) === null || _c === void 0 ? void 0 : _c['$']) && ((_e = (_d = XMLEvent.ActivityDates[0]) === null || _d === void 0 ? void 0 : _d.ActivityDate[0]) === null || _e === void 0 ? void 0 : _e['$'])) || null; const startDate = activityDate === null || activityDate === void 0 ? void 0 : activityDate.startDate; const startTime = activityDate === null || activityDate === void 0 ? void 0 : activityDate.startTime; const endDate = activityDate === null || activityDate === void 0 ? void 0 : activityDate.endDate; const endTime = activityDate === null || activityDate === void 0 ? void 0 : activityDate.endTime; const start = `${startDate} ${startTime}`; const end = endDate ? `${endDate} ${endTime}` : null; const castInfo = XMLEvent.CastInformation[0].replace(/(<([^>]+)>)/gi, ''); const longDescription = XMLEvent.LongDescription[0].replace(/(<([^>]+)>)/gi, ''); const shortDescription = XMLEvent.ShortDescription[0].replace(/(<([^>]+)>)/gi, ''); const fallbackDescription = (0, exports.getFallbackDesc)(source); const parsedDescription = (0, slate_serializers_1.htmlToSlate)(longDescription || shortDescription || castInfo || fallbackDescription); // we need to add type: 'paragraph' because that's how it was done in WP in the past parsedDescription[0] = Object.assign(Object.assign({}, parsedDescription[0]), { type: 'paragraph' }); const parsedEvent = { id: XMLEvent['$'].originId, modifiedAt: new Date(XMLEvent['$'].lastUpdate || ''), name: XMLEvent.Title[0].replace(/(<([^>]+)>)/gi, ''), description: parsedDescription, status: client_1.EventStatus.Scheduled, imageUrl: (0, exports.getImageUrl)(XMLEvent), externalSourceId: XMLEvent['$'].originId, externalSourceName: source, location: ((_f = XMLEvent.Location[0]) === null || _f === void 0 ? void 0 : _f.LocationAdress[0].replace(/(<([^>]+)>)/gi, '')) || '', startsAt: start ? new Date(start.trim()) : null, endsAt: end ? new Date(end.trim()) : null }; return parsedEvent; }; //# sourceMappingURL=kulturagenda-parser.js.map