From 7c93bf0aaa018b09c9692efd04fb652127c2f7b0 Mon Sep 17 00:00:00 2001 From: "Michael J. Radwin" Date: Wed, 15 Dec 2021 12:08:24 -0800 Subject: [PATCH] Infer RSS fields from options more intelligently --- package.json | 2 +- src/rss.js | 26 ++++++++++++++++------ src/rss.spec.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 7561d4c..43ea5e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hebcal/rest-api", - "version": "3.10.0", + "version": "3.10.1", "author": "Michael J. Radwin (https://github.com/mjradwin)", "keywords": [ "hebcal" diff --git a/src/rss.js b/src/rss.js index fbfee56..c513744 100644 --- a/src/rss.js +++ b/src/rss.js @@ -1,4 +1,5 @@ -import {getEventCategories, makeAnchor, appendIsraelAndTracking, makeTorahMemoText} from './common'; +import {getEventCategories, makeAnchor, appendIsraelAndTracking, + makeTorahMemoText, getCalendarTitle} from './common'; import {Locale, HebrewCalendar, Zmanim, flags} from '@hebcal/core'; import holidayDescription from './holidays.json'; @@ -57,15 +58,21 @@ export function eventsToRss(events, location, mainUrl, selfUrl, lang='en-US', ev return eventsToRss2(events, options); } +const localeToLg = { + 's': 'en', + 'a': 'en', + 'he-x-NoNikud': 'he', + 'h': 'he', + 'ah': 'en', + 'sh': 'en', +}; + /** * @param {Event[]} events * @param {HebrewCalendar.Options} options * @return {string} */ export function eventsToRss2(events, options) { - const now = new Date(); - const thisYear = now.getFullYear(); - const lastBuildDate = now.toUTCString(); const dayFormat = new Intl.DateTimeFormat('en-US', { weekday: 'long', day: '2-digit', @@ -75,19 +82,24 @@ export function eventsToRss2(events, options) { const location = options.location; const mainUrl = options.mainUrl; const evPubDate = options.evPubDate; - const title = options.title; + const buildDate = options.buildDate || new Date(); + const thisYear = buildDate.getFullYear(); + const lastBuildDate = buildDate.toUTCString(); + const title = options.title || getCalendarTitle(events, options); + const description = options.description || title; const mainUrlEsc = appendIsraelAndTracking(mainUrl, location && location.getIsrael(), options.utmSource, options.utmMedium, options.utmCampaign).replace(/&/g, '&'); const selfUrlEsc = options.selfUrl.replace(/&/g, '&'); + const lang = options.lang || localeToLg[options.locale] || options.locale || 'en-US'; let str = ` ${title} ${mainUrlEsc} -${options.description} -${options.lang} +${description} +${lang} Copyright (c) ${thisYear} Michael J. Radwin. All rights reserved. ${lastBuildDate} `; diff --git a/src/rss.spec.js b/src/rss.spec.js index b5912f2..021b07e 100644 --- a/src/rss.spec.js +++ b/src/rss.spec.js @@ -1,7 +1,7 @@ /* eslint-disable max-len */ import test from 'ava'; import {HebrewCalendar, Location} from '@hebcal/core'; -import {eventsToRss, eventToRssItem} from './rss'; +import {eventsToRss, eventToRssItem, eventsToRss2} from './rss'; const dayFormat = new Intl.DateTimeFormat('en-US', { weekday: 'long', @@ -35,6 +35,61 @@ test('eventsToRss', (t) => { t.is(rss[rss.length - 1], ''); }); +test('eventsToRss2', (t) => { + const location = new Location(41.85003, -87.65005, false, 'America/Chicago', 'Chicago', 'US', 4887398); + const options = { + year: 1990, + month: 4, + noMinorFast: true, + noRoshChodesh: true, + noSpecialShabbat: true, + candlelighting: true, + havdalahMins: 50, + location: location, + }; + const events = HebrewCalendar.calendar(options).slice(0, 2); + options.mainUrl = 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s'; + options.selfUrl = 'https://www.hebcal.com/shabbat?cfg=r&geonameid=4887398&m=50&lg=s&pubDate=1'; + options.buildDate = new Date(2021, 11, 15, 12, 34, 56); + options.description = 'The quick brown fox'; + const rss = eventsToRss2(events, options).split('\n'); + const expected = [ + '', + '', + '', + 'Hebcal Chicago April 1990', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&utm_source=undefined&utm_medium=undefined', + '', + 'The quick brown fox', + 'en-US', + 'Copyright (c) 2021 Michael J. Radwin. All rights reserved.', + 'Wed, 15 Dec 2021 20:34:56 GMT', + '', + 'Candle lighting: 7:03pm', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-06&utm_source=shabbat1c&utm_medium=rss#19900406-candle-lighting', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-06#19900406-candle-lighting', + 'Friday, April 06, 1990', + 'candles', + 'Wed, 15 Dec 2021 20:34:56 GMT', + '41.85003', + '-87.65005', + '', + '', + 'Havdalah (50 min): 8:13pm', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-07&utm_source=shabbat1c&utm_medium=rss#19900407-havdalah', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-07#19900407-havdalah', + 'Saturday, April 07, 1990', + 'havdalah', + 'Wed, 15 Dec 2021 20:34:56 GMT', + '', + '', + '', + '', + ]; + t.deepEqual(rss, expected); +}); + + test('eventToRssItem', (t) => { const location = Location.lookup('Eilat'); const options = {