Skip to content

Commit

Permalink
Infer RSS fields from options more intelligently
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Dec 15, 2021
1 parent ff5434f commit 7c93bf0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
26 changes: 19 additions & 7 deletions src/rss.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
Expand All @@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${title}</title>
<link>${mainUrlEsc}</link>
<atom:link href="${selfUrlEsc}" rel="self" type="application/rss+xml" />
<description>${options.description}</description>
<language>${options.lang}</language>
<description>${description}</description>
<language>${lang}</language>
<copyright>Copyright (c) ${thisYear} Michael J. Radwin. All rights reserved.</copyright>
<lastBuildDate>${lastBuildDate}</lastBuildDate>
`;
Expand Down
57 changes: 56 additions & 1 deletion src/rss.spec.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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 = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:atom="http://www.w3.org/2005/Atom">',
'<channel>',
'<title>Hebcal Chicago April 1990</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;utm_source=undefined&amp;utm_medium=undefined</link>',
'<atom:link href="https://www.hebcal.com/shabbat?cfg=r&amp;geonameid=4887398&amp;m=50&amp;lg=s&amp;pubDate=1" rel="self" type="application/rss+xml" />',
'<description>The quick brown fox</description>',
'<language>en-US</language>',
'<copyright>Copyright (c) 2021 Michael J. Radwin. All rights reserved.</copyright>',
'<lastBuildDate>Wed, 15 Dec 2021 20:34:56 GMT</lastBuildDate>',
'<item>',
'<title>Candle lighting: 7:03pm</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-06&amp;utm_source=shabbat1c&amp;utm_medium=rss#19900406-candle-lighting</link>',
'<guid isPermaLink="false">https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-06#19900406-candle-lighting</guid>',
'<description>Friday, April 06, 1990</description>',
'<category>candles</category>',
'<pubDate>Wed, 15 Dec 2021 20:34:56 GMT</pubDate>',
'<geo:lat>41.85003</geo:lat>',
'<geo:long>-87.65005</geo:long>',
'</item>',
'<item>',
'<title>Havdalah (50 min): 8:13pm</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-07&amp;utm_source=shabbat1c&amp;utm_medium=rss#19900407-havdalah</link>',
'<guid isPermaLink="false">https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-07#19900407-havdalah</guid>',
'<description>Saturday, April 07, 1990</description>',
'<category>havdalah</category>',
'<pubDate>Wed, 15 Dec 2021 20:34:56 GMT</pubDate>',
'</item>',
'</channel>',
'</rss>',
'',
];
t.deepEqual(rss, expected);
});


test('eventToRssItem', (t) => {
const location = Location.lookup('Eilat');
const options = {
Expand Down

0 comments on commit 7c93bf0

Please sign in to comment.