Files
holybar/handlers/refresh.mjs
T
2026-04-17 15:36:11 +02:00

44 lines
1.3 KiB
JavaScript

import { format, startOfToday } from "date-fns";
import * as cheerio from "cheerio";
import { error, reading } from "../helpers/html.mjs";
import { log } from "../helpers/logger.mjs";
/**
* Fetches the daily Bible reading from the USCCB website and updates the app's HTML.
*
* @param {Object} app - The application instance to update with the fetched reading.
*/
export default async function refresh(app) {
const key = format(startOfToday(), "MMddyy");
log("Fetching reading for date:", key);
const response = await fetch(
`https://bible.usccb.org/bible/readings/${key}.cfm`,
);
if (!response.ok) {
log("Error fetching reading:", response.status);
app.setHTML(error(response.status));
return;
}
const html = await response.text();
const $ = cheerio.load(html);
const title = $(".b-lectionary h2").first().text().trim();
const verses = $(".b-verse")
.toArray()
.map((el) => {
const title = $(el).find(".address").first().text().trim();
const content = $(el).find(".content-body").first().html();
const url = $(el).find(".address a").first().attr("href");
return { title, content, url };
});
log("Fetched readings:", title, verses.map((v) => v.title).join(", "));
app.setHTML(reading(title, verses));
}