From c1e678860d5dbe283fe1498f5d246a405293dc66 Mon Sep 17 00:00:00 2001 From: "Michael J. Radwin" Date: Mon, 11 Nov 2024 07:42:53 -0800 Subject: [PATCH] Add myEventsToClassicApi for triennial refactor --- package.json | 14 +++++++------- src/hebcal.js | 12 ++++-------- src/myEventsToClassicApi.js | 37 +++++++++++++++++++++++++++++++++++++ src/shabbat.js | 11 +++-------- src/yahrzeit.js | 11 +++-------- 5 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/myEventsToClassicApi.js diff --git a/package.json b/package.json index a9afe7e..8331208 100644 --- a/package.json +++ b/package.json @@ -43,18 +43,18 @@ "eslint": "^9.14.0", "eslint-plugin-n": "^17.13.1", "globals": "^15.12.0", - "rollup": "^4.24.4" + "rollup": "^4.25.0" }, "dependencies": { - "@hebcal/core": "^5.7.7", + "@hebcal/core": "^5.8.0", "@hebcal/geo-sqlite": "^5.0.6", "@hebcal/hdate": "^0.12.0", - "@hebcal/icalendar": "^5.1.1", + "@hebcal/icalendar": "^6.0.2", "@hebcal/learning": "^5.1.2", - "@hebcal/leyning": "^9.0.1", - "@hebcal/locales": "^5.0.1", - "@hebcal/rest-api": "^5.2.0", - "@hebcal/triennial": "^5.1.3", + "@hebcal/leyning": "^9.0.2", + "@hebcal/locales": "^5.0.2", + "@hebcal/rest-api": "^6.0.2", + "@hebcal/triennial": "^6.0.1", "better-sqlite3": "^11.5.0", "dayjs": "^1.11.13", "ejs": "^3.1.10", diff --git a/src/hebcal.js b/src/hebcal.js index c49c442..8885f0d 100644 --- a/src/hebcal.js +++ b/src/hebcal.js @@ -18,7 +18,8 @@ import {makeHebcalOptions, processCookieAndQuery, possiblySetCookie, import {getDefaultYear, getDefaultHebrewYear} from './dateUtil.js'; import {makeDownloadProps} from './makeDownloadProps.js'; import {HDate, Locale} from '@hebcal/core'; -import {eventsToClassicApi, eventToFullCalendar, +import { + eventToFullCalendar, eventToClassicApiObject, locationToPlainObj, getCalendarTitle, @@ -31,12 +32,10 @@ import dayjs from 'dayjs'; import localeData from 'dayjs/plugin/localeData.js'; import './dayjs-locales.js'; import '@hebcal/locales'; -import {readJSON} from './readJSON.js'; +import {myEventsToClassicApi} from './myEventsToClassicApi.js'; dayjs.extend(localeData); -const pkg = readJSON('../package.json'); - export async function hebcalApp(ctx) { if (ctx.method === 'POST') { ctx.set('Allow', 'GET'); @@ -438,10 +437,7 @@ function renderJson(ctx) { if (q.hdp === '1') { options.heDateParts = true; } - let obj = eventsToClassicApi(events, options, !leyningOff); - if (typeof obj.version === 'string') { - obj.version += '-' + pkg.version; - } + let obj = myEventsToClassicApi(events, options, leyningOff); const cb = empty(q.callback) ? false : q.callback.replace(/[^\w\.]/g, ''); if (cb) { obj = cb + '(' + JSON.stringify(obj) + ')\n'; diff --git a/src/myEventsToClassicApi.js b/src/myEventsToClassicApi.js new file mode 100644 index 0000000..65acbc4 --- /dev/null +++ b/src/myEventsToClassicApi.js @@ -0,0 +1,37 @@ +import {flags} from '@hebcal/core'; +import {formatAliyahWithBook} from '@hebcal/leyning'; +import { + eventsToClassicApiHeader, eventToClassicApiObject, +} from '@hebcal/rest-api'; +import {getTriennialForParshaHaShavua} from '@hebcal/triennial'; +import {readJSON} from './readJSON.js'; + +export const pkg = readJSON('../package.json'); + +export function myEventsToClassicApi(events, options, leyningOff) { + const obj = eventsToClassicApiHeader(events, options); + if (typeof obj.version === 'string') { + obj.version += '-' + pkg.version; + } + obj.items = events.map((ev) => { + const apiObj = eventToClassicApiObject(ev, options, !leyningOff); + if (!leyningOff && + (ev.getFlags() & flags.PARSHA_HASHAVUA) && + ev.getDate().getFullYear() >= 5745) { + const triReading = getTriennialForParshaHaShavua(ev, options.il); + const aliyot = triReading?.aliyot; + if (aliyot) { + const triAliyot = {}; + for (const [num, aliyah] of Object.entries(aliyot)) { + if (typeof aliyah !== 'undefined') { + const k = num === 'M' ? 'maftir' : num; + triAliyot[k] = formatAliyahWithBook(aliyah); + } + } + apiObj.leyning.triennial = triAliyot; + } + } + return apiObj; + }); + return obj; +} diff --git a/src/shabbat.js b/src/shabbat.js index a5b55bb..f257398 100644 --- a/src/shabbat.js +++ b/src/shabbat.js @@ -16,18 +16,16 @@ import {makeDownloadProps} from './makeDownloadProps.js'; import '@hebcal/locales'; import dayjs from 'dayjs'; import {countryNames, getEventCategories, renderTitleWithoutTime, makeAnchor, - eventsToRss2, eventsToClassicApi, appendIsraelAndTracking} from '@hebcal/rest-api'; + eventsToRss2, appendIsraelAndTracking} from '@hebcal/rest-api'; import utc from 'dayjs/plugin/utc.js'; import timezone from 'dayjs/plugin/timezone.js'; import './dayjs-locales.js'; import {GeoDb} from '@hebcal/geo-sqlite'; -import {readJSON} from './readJSON.js'; +import {myEventsToClassicApi} from './myEventsToClassicApi.js'; dayjs.extend(utc); dayjs.extend(timezone); -const pkg = readJSON('../package.json'); - const BASE_URL = 'https://www.hebcal.com/shabbat'; export async function shabbatApp(ctx) { @@ -84,10 +82,7 @@ export async function shabbatApp(ctx) { if (q.hdp === '1') { ctx.state.options.heDateParts = true; } - let obj = eventsToClassicApi(ctx.state.events, ctx.state.options, !leyningOff); - if (typeof obj.version === 'string') { - obj.version += '-' + pkg.version; - } + let obj = myEventsToClassicApi(ctx.state.events, ctx.state.options, !leyningOff); const cb = empty(q.callback) ? false : q.callback.replace(/[^\w\.]/g, ''); if (cb) { obj = cb + '(' + JSON.stringify(obj) + ')\n'; diff --git a/src/yahrzeit.js b/src/yahrzeit.js index 5d71c4c..8858e4b 100644 --- a/src/yahrzeit.js +++ b/src/yahrzeit.js @@ -2,7 +2,8 @@ import {Event, HDate, HebrewCalendar, Locale, Location, flags, gematriya, months} from '@hebcal/core'; import {IcalEvent, icalEventsToString} from '@hebcal/icalendar'; -import {eventsToCsv, eventsToClassicApi, eventToFullCalendar} from '@hebcal/rest-api'; +import {eventsToCsv, eventToFullCalendar} from '@hebcal/rest-api'; +import {myEventsToClassicApi} from './myEventsToClassicApi.js'; import {isoDateStringToDate} from './dateUtil.js'; import dayjs from 'dayjs'; import {basename} from 'path'; @@ -24,12 +25,9 @@ import {getMaxYahrzeitId, isNumKey, summarizeAnniversaryTypes, import {makeLogInfo} from './logger.js'; import {isDeepStrictEqual} from 'node:util'; import {murmur128HexSync, murmur32HexSync} from 'murmurhash3'; -import {readJSON} from './readJSON.js'; const urlPrefix = process.env.NODE_ENV == 'production' ? 'https://download.hebcal.com' : 'http://127.0.0.1:8081'; -const pkg = readJSON('../package.json'); - /** * @param {*} ctx * @return {*} @@ -213,10 +211,7 @@ async function renderJson(maxId, q) { if (q.hdp === '1') { options.heDateParts = true; } - const results = eventsToClassicApi(events, options, false); - if (typeof results.version === 'string') { - results.version += '-' + pkg.version; - } + const results = myEventsToClassicApi(events, options, false); for (const item of results.items) { delete item.hebrew; delete item.category;